Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.
Merged
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
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