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

Avoid Exception-related performance issues in LZ4BlockInputStream when stopOnEmptyBlock == false #143

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/java/net/jpountz/lz4/LZ4BlockInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,11 @@ public long skip(long n) throws IOException {
}

private void refill() throws IOException {
try {
readFully(compressedBuffer, HEADER_LENGTH);
} catch (EOFException e) {
if (!tryReadFully(compressedBuffer, HEADER_LENGTH)) {
if (!stopOnEmptyBlock) {
finished = true;
} else {
throw e;
throw new EOFException("Stream ended prematurely");
}
return;
}
Expand Down Expand Up @@ -263,16 +261,25 @@ private void refill() throws IOException {
o = 0;
}

private void readFully(byte[] b, int len) throws IOException {
// Like readFully(), except it signals incomplete reads by returning
// false instead of throwing EOFException.
private boolean tryReadFully(byte[] b, int len) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: drop throws IOException in the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered this, but it will not work: the underlying read() call declares that it throws IOException so we need to keep this here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, ok.

int read = 0;
while (read < len) {
final int r = in.read(b, read, len - read);
if (r < 0) {
throw new EOFException("Stream ended prematurely");
return false;
}
read += r;
}
assert len == read;
return true;
}

private void readFully(byte[] b, int len) throws IOException {
if (!tryReadFully(b, len)) {
throw new EOFException("Stream ended prematurely");
}
}

@Override
Expand Down