Skip to content

Commit d010601

Browse files
committed
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
1 parent 0c20de6 commit d010601

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/java/net/jpountz/lz4/LZ4BlockInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor decompressor) {
115115
* @see StreamingXXHash32#asChecksum()
116116
*/
117117
public LZ4BlockInputStream(InputStream in, boolean stopOnEmptyBlock) {
118-
this(in, LZ4Factory.fastestInstance().fastDecompressor(), XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum(), stopOnEmptyBlock);
118+
this(in, LZ4Factory.fastestInstance().fastDecompressor(), new LZ4BlockOutputStream.Drop4BitsChecksum(XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum()), stopOnEmptyBlock);
119119
}
120120

121121
/**

src/java/net/jpountz/lz4/LZ4BlockOutputStream.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,34 @@ public class LZ4BlockOutputStream extends FilterOutputStream {
5555

5656
static final int DEFAULT_SEED = 0x9747b28c;
5757

58+
/**
59+
* This class is for the backward-compatibility before https://github.com/lz4/lz4-java/pull/181.
60+
*/
61+
static class Drop4BitsChecksum implements Checksum {
62+
63+
private Checksum checksum;
64+
65+
Drop4BitsChecksum(Checksum checksum) {
66+
this.checksum = checksum;
67+
}
68+
69+
@Override public void update(int b) {
70+
checksum.update(b);
71+
}
72+
73+
@Override public void update(byte[] b, int off, int len) {
74+
checksum.update(b, off, len);
75+
}
76+
77+
@Override public long getValue() {
78+
return checksum.getValue() & 0x0FFFFFFFL;
79+
}
80+
81+
@Override public void reset() {
82+
checksum.reset();
83+
}
84+
}
85+
5886
private static int compressionLevel(int blockSize) {
5987
if (blockSize < MIN_BLOCK_SIZE) {
6088
throw new IllegalArgumentException("blockSize must be >= " + MIN_BLOCK_SIZE + ", got " + blockSize);
@@ -122,7 +150,7 @@ public LZ4BlockOutputStream(OutputStream out, int blockSize, LZ4Compressor compr
122150
* @see StreamingXXHash32#asChecksum()
123151
*/
124152
public LZ4BlockOutputStream(OutputStream out, int blockSize, LZ4Compressor compressor) {
125-
this(out, blockSize, compressor, XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum(), false);
153+
this(out, blockSize, compressor, new Drop4BitsChecksum(XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum()), false);
126154
}
127155

128156
/**

0 commit comments

Comments
 (0)