summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@verizonmedia.com>2021-01-19 13:55:33 +0100
committerBjørn Christian Seime <bjorncs@verizonmedia.com>2021-01-20 12:23:29 +0100
commit1105b88537eb28bdbae23c4d967e2fbfa0ad9857 (patch)
treea59d2334b5bb6cf3e225b01018d3700ef3548253 /vespajlib
parent61bda66d5b7789a1deb580992ff4adf2835def47 (diff)
Extend ZstdCompressor with more low-level API methods
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/compress/ZstdCompressor.java24
-rw-r--r--vespajlib/src/test/java/com/yahoo/compress/ZstdCompressorTest.java17
2 files changed, 36 insertions, 5 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/compress/ZstdCompressor.java b/vespajlib/src/main/java/com/yahoo/compress/ZstdCompressor.java
index 58a01df5b09..5d15b102ad6 100644
--- a/vespajlib/src/main/java/com/yahoo/compress/ZstdCompressor.java
+++ b/vespajlib/src/main/java/com/yahoo/compress/ZstdCompressor.java
@@ -15,16 +15,32 @@ public class ZstdCompressor {
private static final io.airlift.compress.zstd.ZstdDecompressor decompressor = new io.airlift.compress.zstd.ZstdDecompressor();
public byte[] compress(byte[] input, int inputOffset, int inputLength) {
- int maxCompressedLength = compressor.maxCompressedLength(inputLength);
+ int maxCompressedLength = getMaxCompressedLength(inputLength);
byte[] output = new byte[maxCompressedLength];
- int compressedLength = compressor.compress(input, inputOffset, inputLength, output, 0, maxCompressedLength);
+ int compressedLength = compress(input, inputOffset, inputLength, output, 0, maxCompressedLength);
return Arrays.copyOf(output, compressedLength);
}
+ public int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) {
+ return compressor.compress(input, inputOffset, inputLength, output, outputOffset, maxOutputLength);
+ }
+
public byte[] decompress(byte[] input, int inputOffset, int inputLength) {
- int decompressedLength = (int) io.airlift.compress.zstd.ZstdDecompressor.getDecompressedSize(input, inputOffset, inputLength);
+ int decompressedLength = getDecompressedLength(input, inputOffset, inputLength);
byte[] output = new byte[decompressedLength];
- decompressor.decompress(input, inputOffset, inputLength, output, 0, decompressedLength);
+ decompress(input, inputOffset, inputLength, output, 0, decompressedLength);
return output;
}
+
+ public int decompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) {
+ return decompressor.decompress(input, inputOffset, inputLength, output, outputOffset, maxOutputLength);
+ }
+
+ public static int getMaxCompressedLength(int uncompressedLength) {
+ return compressor.maxCompressedLength(uncompressedLength);
+ }
+
+ public static int getDecompressedLength(byte[] input, int inputOffset, int inputLength) {
+ return (int) io.airlift.compress.zstd.ZstdDecompressor.getDecompressedSize(input, inputOffset, inputLength);
+ }
}
diff --git a/vespajlib/src/test/java/com/yahoo/compress/ZstdCompressorTest.java b/vespajlib/src/test/java/com/yahoo/compress/ZstdCompressorTest.java
index 837482a089a..f38ce4ad953 100644
--- a/vespajlib/src/test/java/com/yahoo/compress/ZstdCompressorTest.java
+++ b/vespajlib/src/test/java/com/yahoo/compress/ZstdCompressorTest.java
@@ -4,6 +4,7 @@ package com.yahoo.compress;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author bjorncs
@@ -19,4 +20,18 @@ class ZstdCompressorTest {
assertArrayEquals(inputData, decompressedData);
}
-} \ No newline at end of file
+ @Test
+ void compressed_size_is_less_than_uncompressed() {
+ StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < 100; i++) {
+ builder.append("The quick brown fox jumps over the lazy dog").append('\n');
+ }
+ byte[] inputData = builder.toString().getBytes();
+ ZstdCompressor compressor = new ZstdCompressor();
+ byte[] compressedData = compressor.compress(inputData, 0, inputData.length);
+ assertTrue(
+ compressedData.length < inputData.length,
+ () -> "Compressed size is " + compressedData.length + " while uncompressed size is " + inputData.length);
+ }
+
+}