fixed bug in net.woggioni.jzstd.ZstdInputStream

This commit is contained in:
Walter Oggioni
2020-05-01 21:15:26 +01:00
parent 65929adcd7
commit da433c9d14
4 changed files with 71 additions and 38 deletions

View File

@@ -57,6 +57,7 @@ public class ZstdInputStream extends InputStream {
} else if (input.src.position() == input.src.capacity()) {
input.src.position(0);
}
int toRead = input.src.capacity() - input.src.position();
if (toRead > 0) {
int read = source.read(buffer, 0, toRead);
@@ -68,7 +69,6 @@ public class ZstdInputStream extends InputStream {
}
input.pos = zero;
input.size = new size_t(input.src.position());
output.pos = zero;
}
int rc = ZstdLibrary.decompressStream(ctx.ctx, output, input);
if (rc == 0) {
@@ -96,11 +96,10 @@ public class ZstdInputStream extends InputStream {
if (state == State.CTX_FLUSHED) {
if (totalRead == 0) --totalRead;
break;
}
else fill();
} else fill();
}
int toBeRead = Math.min(len, output.pos.intValue() - output.dst.position());
output.dst.get(arr, off, toBeRead);
output.dst.get(arr, off + totalRead, toBeRead);
totalRead += toBeRead;
}
return totalRead;

View File

@@ -12,7 +12,7 @@ public class ZSTD_inBuffer extends Structure {
public size_t pos;
public ZSTD_inBuffer(int size) {
this.src = ByteBuffer.allocateDirect(size);
this.src = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
this.size = new size_t(this.src.capacity());
this.pos = new size_t(this.src.capacity());
}

View File

@@ -12,7 +12,7 @@ public class ZSTD_outBuffer extends Structure {
public size_t pos;
public ZSTD_outBuffer(int size) {
this.dst = ByteBuffer.allocateDirect(size);
this.dst = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
this.size = new size_t(dst.capacity());
this.pos = new size_t(0);
}

View File

@@ -2,7 +2,8 @@ package net.woggioni.jzstd;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
@@ -15,7 +16,10 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class SplitOutputStream extends OutputStream {
@@ -132,25 +136,36 @@ public class BasicTest {
return new String(hexChars);
}
private static class InputStreamProvider implements ArgumentsProvider {
private static class TestCaseProvider implements ArgumentsProvider {
@Override
@SneakyThrows
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of(getClass().getResourceAsStream("/index.html")),
Arguments.of(new RandomBytesGenerator(123456, 1000)),
Arguments.of(new RandomBytesGenerator(654321, 65536)),
Arguments.of(new RandomBytesGenerator(101325, 12345678)),
Arguments.of(new HexSequenceGenerator(12345678))
);
Supplier<InputStream>[] streams = new Supplier[]{
() -> getClass().getResourceAsStream("/index.html"),
() -> new RandomBytesGenerator(123456, 1000),
() -> new RandomBytesGenerator(654321, 65536),
() -> new RandomBytesGenerator(101325, 12345678),
() -> new HexSequenceGenerator(12345678)
};
int[] bufferSizes = new int[]{
0x100,
0x1000,
0x10000,
0x100000
};
return Arrays.stream(streams)
.flatMap(s -> Arrays.stream(bufferSizes).mapToObj(bufferSize -> Arguments.of(s.get(), bufferSize)));
}
}
private static final Path temporaryDirectory = Paths.get(System.getProperty("java.io.tmpdir"));
@SneakyThrows
@ParameterizedTest
@ArgumentsSource(InputStreamProvider.class)
public void test(InputStream testStream) {
@ArgumentsSource(TestCaseProvider.class)
public void test(InputStream testStream, int bufferSize) {
MessageDigest md5 = MessageDigest.getInstance("MD5");
boolean debug = false;
@@ -161,16 +176,16 @@ public class BasicTest {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
os = baos;
if (debug) {
os = new SplitOutputStream(os, Files.newOutputStream(Paths.get("/tmp/out.bin.zst")));
os = new SplitOutputStream(os, Files.newOutputStream(temporaryDirectory.resolve("out.bin.zst")));
}
os = ZstdOutputStream.from(os);
if (debug) {
new SplitOutputStream(
os = new SplitOutputStream(
os,
Files.newOutputStream(Paths.get("/tmp/out.bin.original")));
Files.newOutputStream(temporaryDirectory.resolve("/tmp/out.bin.original")));
}
try {
byte[] buffer = new byte[0x100000];
byte[] buffer = new byte[bufferSize];
while (true) {
int read = is.read(buffer);
if (read < 0) break;
@@ -188,10 +203,29 @@ public class BasicTest {
md5.reset();
try (InputStream is = new DigestInputStream(
ZstdInputStream.from(compressedStream), md5)) {
byte[] buffer = new byte[0x10000];
OutputStream os;
if(debug) {
os = Files.newOutputStream(temporaryDirectory.resolve("out.bin"));
} else {
os = new OutputStream() {
@Override
public void write(int i) {
}
@Override
public void write(byte[] b, int off, int len) {
}
};
}
try {
byte[] buffer = new byte[bufferSize];
while (true) {
int read = is.read(buffer);
if (read < 0) break;
os.write(buffer, 0, read);
}
} finally {
os.close();
}
}
byte[] roundTripDigest = md5.digest();