Skip to content

Commit

Permalink
Merge pull request #143 from JoshRosen/stopOnEmptyBlock-exception-per…
Browse files Browse the repository at this point in the history
…f-fix

Avoid Exception-related performance issues in LZ4BlockInputStream when stopOnEmptyBlock == false
  • Loading branch information
odaira committed Aug 30, 2019
2 parents e1440ab + 0ab93f1 commit 9441e66
Showing 1 changed file with 13 additions and 6 deletions.
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 {
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

0 comments on commit 9441e66

Please sign in to comment.