GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid reading InputStream when LZ4FrameInputStream is created (#146) · lz4/lz4-java@5e23573

This repository was archived by the owner on Dec 2, 2025. It is now read-only.

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/java/net/jpountz/lz4
    • LZ4FrameInputStream.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/java/net/jpountz/lz4/LZ4FrameInputStream.java‎

Copy file name to clipboardExpand all lines: src/java/net/jpountz/lz4/LZ4FrameInputStream.java
+23-12Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class LZ4FrameInputStream extends FilterInputStream {
5757
private int maxBlockSize = -1;
5858
private long expectedContentSize = -1L;
5959
private long totalContentSize = 0L;
60+
private boolean firstFrameHeaderRead = false;
6061

6162
private LZ4FrameOutputStream.FrameInfo frameInfo = null;
6263

@@ -119,7 +120,6 @@ public LZ4FrameInputStream(InputStream in, LZ4SafeDecompressor decompressor, XX
119120
this.decompressor = decompressor;
120121
this.checksum = checksum;
121122
this.readSingleFrame = readSingleFrame;
122-
nextFrameInfo();
123123
}
124124

125125

@@ -211,6 +211,7 @@ private void readHeader() throws IOException {
211211
rawBuffer = new byte[maxBlockSize];
212212
buffer = ByteBuffer.wrap(rawBuffer);
213213
buffer.limit(0);
214+
firstFrameHeaderRead = true;
214215
}
215216

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

313314
@Override
314315
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) {
318319
return -1;
319320
}
320321
if (!nextFrameInfo()) {
@@ -331,9 +332,9 @@ public int read(byte[] b, int off, int len) throws IOException {
331332
if ((off < 0) || (len < 0) || (off + len > b.length)) {
332333
throw new IndexOutOfBoundsException();
333334
}
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) {
337338
return -1;
338339
}
339340
if (!nextFrameInfo()) {
@@ -352,9 +353,9 @@ public long skip(long n) throws IOException {
352353
if (n <= 0) {
353354
return 0;
354355
}
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) {
358359
return 0;
359360
}
360361
if (!nextFrameInfo()) {
@@ -402,10 +403,15 @@ public boolean markSupported() {
402403
*
403404
* @see #LZ4FrameInputStream(InputStream, LZ4SafeDecompressor, XXHash32, boolean)
404405
*/
405-
public long getExpectedContentSize() {
406+
public long getExpectedContentSize() throws IOException {
406407
if (!readSingleFrame) {
407408
throw new UnsupportedOperationException("Operation not permitted when multiple frames can be read");
408409
}
410+
if (!firstFrameHeaderRead) {
411+
if (!nextFrameInfo()) {
412+
return -1L;
413+
}
414+
}
409415
return expectedContentSize;
410416
}
411417

@@ -414,8 +420,13 @@ public long getExpectedContentSize() {
414420
*
415421
* @return true if this instance is supposed to read only one frame and if the optional content size is set in the frame.
416422
*/
417-
public boolean isExpectedContentSizeDefined() {
423+
public boolean isExpectedContentSizeDefined() throws IOException {
418424
if (readSingleFrame) {
425+
if (!firstFrameHeaderRead) {
426+
if (!nextFrameInfo()) {
427+
return false;
428+
}
429+
}
419430
return expectedContentSize >= 0;
420431
} else {
421432
return false;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.