From 0c20de6d7951ca8c756ce1b4ef17ce08fa8ad15a Mon Sep 17 00:00:00 2001 From: Alexandre Blazart Date: Wed, 25 Aug 2021 20:12:19 +0200 Subject: [PATCH 1/2] Fix the implementation of StreamingXXHash32.asChecksum() In the Checksum view, we only keep the last 28 bits of the hash instead of the 32 bits of the full hash. --- src/java/net/jpountz/xxhash/StreamingXXHash32.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java/net/jpountz/xxhash/StreamingXXHash32.java b/src/java/net/jpountz/xxhash/StreamingXXHash32.java index 8143433..eb5f608 100644 --- a/src/java/net/jpountz/xxhash/StreamingXXHash32.java +++ b/src/java/net/jpountz/xxhash/StreamingXXHash32.java @@ -103,7 +103,7 @@ public final Checksum asChecksum() { @Override public long getValue() { - return StreamingXXHash32.this.getValue() & 0xFFFFFFFL; + return StreamingXXHash32.this.getValue() & 0xFFFFFFFFL; } @Override From d0106013a0e28ad9b8b70216ff8939ffceddc904 Mon Sep 17 00:00:00 2001 From: Alexandre Blazart Date: Thu, 26 Aug 2021 18:22:14 +0200 Subject: [PATCH 2/2] Backward-compatibility code for the default parameters of LZ4Block*Stream Before the PR, the 4 first bits were always set to 0 in LZ4Block*Stream. This commit keeps the current behaviour in LZ4Block*Stream to avoid breaking existing data built with this library --- .../net/jpountz/lz4/LZ4BlockInputStream.java | 2 +- .../net/jpountz/lz4/LZ4BlockOutputStream.java | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/java/net/jpountz/lz4/LZ4BlockInputStream.java b/src/java/net/jpountz/lz4/LZ4BlockInputStream.java index 23fd5e3..53c6b9e 100644 --- a/src/java/net/jpountz/lz4/LZ4BlockInputStream.java +++ b/src/java/net/jpountz/lz4/LZ4BlockInputStream.java @@ -115,7 +115,7 @@ public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor decompressor) { * @see StreamingXXHash32#asChecksum() */ public LZ4BlockInputStream(InputStream in, boolean stopOnEmptyBlock) { - this(in, LZ4Factory.fastestInstance().fastDecompressor(), XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum(), stopOnEmptyBlock); + this(in, LZ4Factory.fastestInstance().fastDecompressor(), new LZ4BlockOutputStream.Drop4BitsChecksum(XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum()), stopOnEmptyBlock); } /** diff --git a/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java b/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java index 623eb1c..898adb2 100644 --- a/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java +++ b/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java @@ -55,6 +55,34 @@ public class LZ4BlockOutputStream extends FilterOutputStream { static final int DEFAULT_SEED = 0x9747b28c; + /** + * This class is for the backward-compatibility before https://github.com/lz4/lz4-java/pull/181. + */ + static class Drop4BitsChecksum implements Checksum { + + private Checksum checksum; + + Drop4BitsChecksum(Checksum checksum) { + this.checksum = checksum; + } + + @Override public void update(int b) { + checksum.update(b); + } + + @Override public void update(byte[] b, int off, int len) { + checksum.update(b, off, len); + } + + @Override public long getValue() { + return checksum.getValue() & 0x0FFFFFFFL; + } + + @Override public void reset() { + checksum.reset(); + } + } + private static int compressionLevel(int blockSize) { if (blockSize < MIN_BLOCK_SIZE) { throw new IllegalArgumentException("blockSize must be >= " + MIN_BLOCK_SIZE + ", got " + blockSize); @@ -122,7 +150,7 @@ public LZ4BlockOutputStream(OutputStream out, int blockSize, LZ4Compressor compr * @see StreamingXXHash32#asChecksum() */ public LZ4BlockOutputStream(OutputStream out, int blockSize, LZ4Compressor compressor) { - this(out, blockSize, compressor, XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum(), false); + this(out, blockSize, compressor, new Drop4BitsChecksum(XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum()), false); } /**