-
Notifications
You must be signed in to change notification settings - Fork 252
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
Read concatenated byte stream in LZ4BlockInputStream #105
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,6 @@ | |
import java.util.zip.Checksum; | ||
|
||
import net.jpountz.util.SafeUtils; | ||
import net.jpountz.util.Utils; | ||
import net.jpountz.xxhash.StreamingXXHash32; | ||
import net.jpountz.xxhash.XXHash32; | ||
import net.jpountz.xxhash.XXHashFactory; | ||
|
@@ -40,7 +39,7 @@ | |
* support {@link #mark(int)}/{@link #reset()}. | ||
* @see LZ4BlockOutputStream | ||
*/ | ||
public final class LZ4BlockInputStream extends FilterInputStream { | ||
public class LZ4BlockInputStream extends FilterInputStream { | ||
|
||
private final LZ4FastDecompressor decompressor; | ||
private final Checksum checksum; | ||
|
@@ -49,6 +48,7 @@ public final class LZ4BlockInputStream extends FilterInputStream { | |
private int originalLen; | ||
private int o; | ||
private boolean finished; | ||
private boolean stopOnEmptyBlock; | ||
|
||
/** | ||
* Create a new {@link InputStream}. | ||
|
@@ -68,6 +68,11 @@ public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor decompressor, Che | |
this.compressedBuffer = new byte[HEADER_LENGTH]; | ||
o = originalLen = 0; | ||
finished = false; | ||
stopOnEmptyBlock = true; | ||
} | ||
|
||
protected void setStopOnEmptyBlock(boolean stopOnEmptyBlock) { | ||
this.stopOnEmptyBlock = stopOnEmptyBlock; | ||
} | ||
|
||
/** | ||
|
@@ -147,7 +152,19 @@ public long skip(long n) throws IOException { | |
} | ||
|
||
private void refill() throws IOException { | ||
readFully(compressedBuffer, HEADER_LENGTH); | ||
if (finished || o < originalLen) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this check necessary? I don't find any execution path on which this check is evaluated to true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, ok. my bad. I forgot removing this. Thanks. |
||
return; | ||
} | ||
try { | ||
readFully(compressedBuffer, HEADER_LENGTH); | ||
} catch (EOFException e) { | ||
if (!stopOnEmptyBlock) { | ||
finished = true; | ||
} else { | ||
throw e; | ||
} | ||
return; | ||
} | ||
for (int i = 0; i < MAGIC_LENGTH; ++i) { | ||
if (compressedBuffer[i] != MAGIC[i]) { | ||
throw new IOException("Stream is corrupted"); | ||
|
@@ -175,7 +192,11 @@ private void refill() throws IOException { | |
if (check != 0) { | ||
throw new IOException("Stream is corrupted"); | ||
} | ||
finished = true; | ||
if (!stopOnEmptyBlock) { | ||
refill(); | ||
} else { | ||
finished = true; | ||
} | ||
return; | ||
} | ||
if (buffer.length < originalLen) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be protected, or public? I guess the use case you assume is to extend this class to enable the new behavior, but isn't it simpler to just make
setStopOnEmptyBlock()
public so that users can just call it if they want the new behavior?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reconsider this and I think how about setting this in the constructor? I couldn't imagine users change this flag during the decompression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. It makes more sense to have new constructors.