@@ -57,6 +57,7 @@ public class LZ4FrameInputStream extends FilterInputStream { |
57 | 57 | private int maxBlockSize = -1; |
58 | 58 | private long expectedContentSize = -1L; |
59 | 59 | private long totalContentSize = 0L; |
| 60 | + private boolean firstFrameHeaderRead = false; |
60 | 61 |
|
61 | 62 | private LZ4FrameOutputStream.FrameInfo frameInfo = null; |
62 | 63 |
|
@@ -119,7 +120,6 @@ public LZ4FrameInputStream(InputStream in, LZ4SafeDecompressor decompressor, XX |
119 | 120 | this.decompressor = decompressor; |
120 | 121 | this.checksum = checksum; |
121 | 122 | this.readSingleFrame = readSingleFrame; |
122 | | - nextFrameInfo(); |
123 | 123 | } |
124 | 124 |
|
125 | 125 |
|
@@ -211,6 +211,7 @@ private void readHeader() throws IOException { |
211 | 211 | rawBuffer = new byte[maxBlockSize]; |
212 | 212 | buffer = ByteBuffer.wrap(rawBuffer); |
213 | 213 | buffer.limit(0); |
| 214 | + firstFrameHeaderRead = true; |
214 | 215 | } |
215 | 216 |
|
216 | 217 | private final ByteBuffer readNumberBuff = ByteBuffer.allocate(LZ4FrameOutputStream.LONG_BYTES).order(ByteOrder.LITTLE_ENDIAN); |
@@ -312,9 +313,9 @@ private void readBlock() throws IOException { |
312 | 313 |
|
313 | 314 | @Override |
314 | 315 | public int read() throws IOException { |
315 | | - while (buffer.remaining() == 0) { |
316 | | - if (frameInfo.isFinished()) { |
317 | | - if (readSingleFrame) { |
| 316 | + while (!firstFrameHeaderRead || buffer.remaining() == 0) { |
| 317 | + if (!firstFrameHeaderRead || frameInfo.isFinished()) { |
| 318 | + if (firstFrameHeaderRead && readSingleFrame) { |
318 | 319 | return -1; |
319 | 320 | } |
320 | 321 | if (!nextFrameInfo()) { |
@@ -331,9 +332,9 @@ public int read(byte[] b, int off, int len) throws IOException { |
331 | 332 | if ((off < 0) || (len < 0) || (off + len > b.length)) { |
332 | 333 | throw new IndexOutOfBoundsException(); |
333 | 334 | } |
334 | | - while (buffer.remaining() == 0) { |
335 | | - if (frameInfo.isFinished()) { |
336 | | - if (readSingleFrame) { |
| 335 | + while (!firstFrameHeaderRead || buffer.remaining() == 0) { |
| 336 | + if (!firstFrameHeaderRead || frameInfo.isFinished()) { |
| 337 | + if (firstFrameHeaderRead && readSingleFrame) { |
337 | 338 | return -1; |
338 | 339 | } |
339 | 340 | if (!nextFrameInfo()) { |
@@ -352,9 +353,9 @@ public long skip(long n) throws IOException { |
352 | 353 | if (n <= 0) { |
353 | 354 | return 0; |
354 | 355 | } |
355 | | - while (buffer.remaining() == 0) { |
356 | | - if (frameInfo.isFinished()) { |
357 | | - if (readSingleFrame) { |
| 356 | + while (!firstFrameHeaderRead || buffer.remaining() == 0) { |
| 357 | + if (!firstFrameHeaderRead || frameInfo.isFinished()) { |
| 358 | + if (firstFrameHeaderRead && readSingleFrame) { |
358 | 359 | return 0; |
359 | 360 | } |
360 | 361 | if (!nextFrameInfo()) { |
@@ -402,10 +403,15 @@ public boolean markSupported() { |
402 | 403 | * |
403 | 404 | * @see #LZ4FrameInputStream(InputStream, LZ4SafeDecompressor, XXHash32, boolean) |
404 | 405 | */ |
405 | | - public long getExpectedContentSize() { |
| 406 | + public long getExpectedContentSize() throws IOException { |
406 | 407 | if (!readSingleFrame) { |
407 | 408 | throw new UnsupportedOperationException("Operation not permitted when multiple frames can be read"); |
408 | 409 | } |
| 410 | + if (!firstFrameHeaderRead) { |
| 411 | + if (!nextFrameInfo()) { |
| 412 | + return -1L; |
| 413 | + } |
| 414 | + } |
409 | 415 | return expectedContentSize; |
410 | 416 | } |
411 | 417 |
|
@@ -414,8 +420,13 @@ public long getExpectedContentSize() { |
414 | 420 | * |
415 | 421 | * @return true if this instance is supposed to read only one frame and if the optional content size is set in the frame. |
416 | 422 | */ |
417 | | - public boolean isExpectedContentSizeDefined() { |
| 423 | + public boolean isExpectedContentSizeDefined() throws IOException { |
418 | 424 | if (readSingleFrame) { |
| 425 | + if (!firstFrameHeaderRead) { |
| 426 | + if (!nextFrameInfo()) { |
| 427 | + return false; |
| 428 | + } |
| 429 | + } |
419 | 430 | return expectedContentSize >= 0; |
420 | 431 | } else { |
421 | 432 | return false; |
|