Skip to content

Commit

Permalink
Backward-compatibility code for the default parameters of LZ4Block*St…
Browse files Browse the repository at this point in the history
…ream

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
  • Loading branch information
trazfr committed Aug 26, 2021
1 parent 0c20de6 commit d010601
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/java/net/jpountz/lz4/LZ4BlockInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
30 changes: 29 additions & 1 deletion src/java/net/jpountz/lz4/LZ4BlockOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit d010601

Please sign in to comment.