Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the implementation of StreamingXXHash32.asChecksum() #181

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/java/net/jpountz/xxhash/StreamingXXHash32.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public final Checksum asChecksum() {

@Override
public long getValue() {
return StreamingXXHash32.this.getValue() & 0xFFFFFFFL;
return StreamingXXHash32.this.getValue() & 0xFFFFFFFFL;
}

@Override
Expand Down