Skip to content

Commit

Permalink
Avoid reading InputStream when LZ4FrameInputStream is created (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
odaira committed Jul 27, 2020
1 parent a1594ed commit 5e23573
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/java/net/jpountz/lz4/LZ4FrameInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class LZ4FrameInputStream extends FilterInputStream {
private int maxBlockSize = -1;
private long expectedContentSize = -1L;
private long totalContentSize = 0L;
private boolean firstFrameHeaderRead = false;

private LZ4FrameOutputStream.FrameInfo frameInfo = null;

Expand Down Expand Up @@ -119,7 +120,6 @@ public LZ4FrameInputStream(InputStream in, LZ4SafeDecompressor decompressor, XX
this.decompressor = decompressor;
this.checksum = checksum;
this.readSingleFrame = readSingleFrame;
nextFrameInfo();
}


Expand Down Expand Up @@ -211,6 +211,7 @@ private void readHeader() throws IOException {
rawBuffer = new byte[maxBlockSize];
buffer = ByteBuffer.wrap(rawBuffer);
buffer.limit(0);
firstFrameHeaderRead = true;
}

private final ByteBuffer readNumberBuff = ByteBuffer.allocate(LZ4FrameOutputStream.LONG_BYTES).order(ByteOrder.LITTLE_ENDIAN);
Expand Down Expand Up @@ -312,9 +313,9 @@ private void readBlock() throws IOException {

@Override
public int read() throws IOException {
while (buffer.remaining() == 0) {
if (frameInfo.isFinished()) {
if (readSingleFrame) {
while (!firstFrameHeaderRead || buffer.remaining() == 0) {
if (!firstFrameHeaderRead || frameInfo.isFinished()) {
if (firstFrameHeaderRead && readSingleFrame) {
return -1;
}
if (!nextFrameInfo()) {
Expand All @@ -331,9 +332,9 @@ public int read(byte[] b, int off, int len) throws IOException {
if ((off < 0) || (len < 0) || (off + len > b.length)) {
throw new IndexOutOfBoundsException();
}
while (buffer.remaining() == 0) {
if (frameInfo.isFinished()) {
if (readSingleFrame) {
while (!firstFrameHeaderRead || buffer.remaining() == 0) {
if (!firstFrameHeaderRead || frameInfo.isFinished()) {
if (firstFrameHeaderRead && readSingleFrame) {
return -1;
}
if (!nextFrameInfo()) {
Expand All @@ -352,9 +353,9 @@ public long skip(long n) throws IOException {
if (n <= 0) {
return 0;
}
while (buffer.remaining() == 0) {
if (frameInfo.isFinished()) {
if (readSingleFrame) {
while (!firstFrameHeaderRead || buffer.remaining() == 0) {
if (!firstFrameHeaderRead || frameInfo.isFinished()) {
if (firstFrameHeaderRead && readSingleFrame) {
return 0;
}
if (!nextFrameInfo()) {
Expand Down Expand Up @@ -402,10 +403,15 @@ public boolean markSupported() {
*
* @see #LZ4FrameInputStream(InputStream, LZ4SafeDecompressor, XXHash32, boolean)
*/
public long getExpectedContentSize() {
public long getExpectedContentSize() throws IOException {
if (!readSingleFrame) {
throw new UnsupportedOperationException("Operation not permitted when multiple frames can be read");
}
if (!firstFrameHeaderRead) {
if (!nextFrameInfo()) {
return -1L;
}
}
return expectedContentSize;
}

Expand All @@ -414,8 +420,13 @@ public long getExpectedContentSize() {
*
* @return true if this instance is supposed to read only one frame and if the optional content size is set in the frame.
*/
public boolean isExpectedContentSizeDefined() {
public boolean isExpectedContentSizeDefined() throws IOException {
if (readSingleFrame) {
if (!firstFrameHeaderRead) {
if (!nextFrameInfo()) {
return false;
}
}
return expectedContentSize >= 0;
} else {
return false;
Expand Down

0 comments on commit 5e23573

Please sign in to comment.