Skip to content

Commit

Permalink
Add more test about ZstdDirectBufferCompressingStream and ZstdDirectB…
Browse files Browse the repository at this point in the history
…ufferDecompressingStream.
  • Loading branch information
linghengqian committed Nov 22, 2022
1 parent da0295a commit c44effd
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
40 changes: 40 additions & 0 deletions metadata/com.github.luben/zstd-jni/1.5.2-5/jni-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@
}
]
},
{
"condition": {
"typeReachable": "com.github.luben.zstd.ZstdDirectBufferCompressingStreamNoFinalizer"
},
"name": "com.github.luben.zstd.ZstdDirectBufferCompressingStream$1"
},
{
"condition": {
"typeReachable": "com.github.luben.zstd.ZstdDirectBufferCompressingStreamNoFinalizer"
},
"name": "com.github.luben.zstd.ZstdDirectBufferCompressingStreamNoFinalizer",
"fields": [
{
"name": "consumed"
},
{
"name": "produced"
}
]
},
{
"condition": {
"typeReachable": "com.github.luben.zstd.ZstdDirectBufferDecompressingStreamNoFinalizer"
},
"name": "com.github.luben.zstd.ZstdDirectBufferDecompressingStream$1"
},
{
"condition": {
"typeReachable": "com.github.luben.zstd.ZstdDirectBufferDecompressingStreamNoFinalizer"
},
"name": "com.github.luben.zstd.ZstdDirectBufferDecompressingStreamNoFinalizer",
"fields": [
{
"name": "consumed"
},
{
"name": "produced"
}
]
},
{
"condition": {
"typeReachable": "com.github.luben.zstd.ZstdInputStreamNoFinalizer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.github.luben.zstd.Zstd;
import com.github.luben.zstd.ZstdDictCompress;
import com.github.luben.zstd.ZstdDictDecompress;
import com.github.luben.zstd.ZstdDirectBufferCompressingStream;
import com.github.luben.zstd.ZstdDirectBufferDecompressingStream;
import com.github.luben.zstd.ZstdInputStream;
import com.github.luben.zstd.ZstdOutputStream;
import org.junit.jupiter.api.Test;
Expand All @@ -24,6 +26,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
Expand All @@ -44,7 +47,7 @@ void testCompressAndDecompress() throws IOException {
}

@Test
void testCompress() throws IOException {
void testCompressFile() throws IOException {
File file = new File("src/test/resources/originTest.txt");
File outFile = new File("src/test/resources/originTest.txt.zs");
long numBytes = 0L;
Expand All @@ -71,7 +74,7 @@ void testCompress() throws IOException {
}

@Test
void testDecompress() {
void testDecompressFileByZstdIOStream() {
File file = new File("src/test/resources/compressTest.zs");
File out = new File("src/test/resources/DecompressTest.txt");
byte[] buffer = new byte[1024 * 1024 * 8];
Expand All @@ -91,4 +94,37 @@ void testDecompress() {
}
assertDoesNotThrow(out::delete);
}

@Test
void testDirectBufferDecompressingStream() throws IOException {
byte[] input = Files.readAllBytes(Paths.get("src/test/resources/originTest.txt"));
final int size = input.length;
ByteBuffer os = ByteBuffer.allocateDirect((int) Zstd.compressBound(size));
final ByteBuffer ib = ByteBuffer.allocateDirect(size);
ib.put(input);
final ZstdDirectBufferCompressingStream osw = new ZstdDirectBufferCompressingStream(os, 9);
ib.flip();
osw.compress(ib);
osw.close();
os.flip();
final byte[] bytes = new byte[os.limit()];
os.get(bytes);
os.rewind();
final ZstdDirectBufferDecompressingStream zis = new ZstdDirectBufferDecompressingStream(os);
final byte[] output = new byte[size];
final ByteBuffer block = ByteBuffer.allocateDirect(128 * 1024);
int offset = 0;
while (zis.hasRemaining()) {
block.clear();
final int read = zis.read(block);
block.flip();
block.get(output, offset, read);
offset = offset + read;
}
zis.close();
String s = "[97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]";
assertThat(Arrays.toString(input)).isEqualTo(s);
assertThat(Arrays.toString(output)).isEqualTo(s);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": [
{"excludeClasses": "**"},
{"includeClasses": "com.github.luben.zstd.**"}
]
}

0 comments on commit c44effd

Please sign in to comment.