Skip to content

Commit af57e23

Browse files
authored
fix NPE in available() (#40)
1 parent 384ade4 commit af57e23

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ public long skip(long n) throws IOException {
383383

384384
@Override
385385
public int available() throws IOException {
386+
if (!firstFrameHeaderRead) {
387+
return 0;
388+
}
386389
return buffer.remaining();
387390
}
388391

src/test/net/jpountz/lz4/LZ4FrameIOStreamTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,4 +551,29 @@ public void testPrematureMagicNb() throws IOException {
551551
lz4File.delete();
552552
}
553553
}
554+
555+
@Test
556+
public void testAvailable() throws IOException {
557+
final File lz4File = Files.createTempFile("lz4test", ".lz4").toFile();
558+
try {
559+
try (OutputStream os = new LZ4FrameOutputStream(new FileOutputStream(lz4File))) {
560+
try (InputStream is = new FileInputStream(tmpFile)) {
561+
copy(is, os);
562+
}
563+
}
564+
565+
try (InputStream is = new LZ4FrameInputStream(new FileInputStream(lz4File))) {
566+
Assert.assertEquals("available() should be 0 before first read", 0, is.available());
567+
568+
if (is.read() != -1 && testSize > 1) {
569+
Assert.assertTrue(
570+
"After reading 1 byte, available() should report > 0 bytes ready in the buffer",
571+
is.available() > 0
572+
);
573+
}
574+
}
575+
} finally {
576+
lz4File.delete();
577+
}
578+
}
554579
}

0 commit comments

Comments
 (0)