Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.
Closed
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
22 changes: 20 additions & 2 deletions src/java/net/jpountz/lz4/LZ4BlockInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class LZ4BlockInputStream extends FilterInputStream {
private int originalLen;
private int o;
private boolean finished;
private boolean stopOnEmptyBlock;

/**
* Create a new {@link InputStream}.
Expand All @@ -68,6 +69,11 @@ public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor decompressor, Che
this.compressedBuffer = new byte[HEADER_LENGTH];
o = originalLen = 0;
finished = false;
stopOnEmptyBlock = true;
}

void setStopOnEmptyBlock(boolean stop) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be a public method?

Copy link
Author

Choose a reason for hiding this comment

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

yes.

stopOnEmptyBlock = stop;
}

/**
Expand All @@ -90,6 +96,9 @@ public LZ4BlockInputStream(InputStream in) {

@Override
public int available() throws IOException {
if (o == originalLen) {
refill();
}
Copy link
Member

Choose a reason for hiding this comment

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

I think typically available() just returns the remaining bytes in the buffer, and it should not call a blocking method like refill(), though it is not strictly prohibited. Do you have any reason you want to call refill() in available()?

Copy link
Author

Choose a reason for hiding this comment

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

I did not remember the the reason, maybe it's OK to not call refill() here.

return originalLen - o;
}

Expand Down Expand Up @@ -147,7 +156,12 @@ public long skip(long n) throws IOException {
}

private void refill() throws IOException {
readFully(compressedBuffer, HEADER_LENGTH);
try {
readFully(compressedBuffer, HEADER_LENGTH);
} catch (EOFException e) {
finished = true;
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

You should re-throw EOFException if stopOnEmptyBlock is true.

Copy link
Author

Choose a reason for hiding this comment

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

yes

for (int i = 0; i < MAGIC_LENGTH; ++i) {
if (compressedBuffer[i] != MAGIC[i]) {
throw new IOException("Stream is corrupted");
Expand Down Expand Up @@ -175,7 +189,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) {
Expand Down