diff --git a/src/java/net/jpountz/lz4/LZ4FrameInputStream.java b/src/java/net/jpountz/lz4/LZ4FrameInputStream.java index a86af76..728524d 100644 --- a/src/java/net/jpountz/lz4/LZ4FrameInputStream.java +++ b/src/java/net/jpountz/lz4/LZ4FrameInputStream.java @@ -383,6 +383,9 @@ public long skip(long n) throws IOException { @Override public int available() throws IOException { + if (!firstFrameHeaderRead) { + return 0; + } return buffer.remaining(); } diff --git a/src/test/net/jpountz/lz4/LZ4FrameIOStreamTest.java b/src/test/net/jpountz/lz4/LZ4FrameIOStreamTest.java index 7c14488..dcd6607 100644 --- a/src/test/net/jpountz/lz4/LZ4FrameIOStreamTest.java +++ b/src/test/net/jpountz/lz4/LZ4FrameIOStreamTest.java @@ -551,4 +551,29 @@ public void testPrematureMagicNb() throws IOException { lz4File.delete(); } } + + @Test + public void testAvailable() throws IOException { + final File lz4File = Files.createTempFile("lz4test", ".lz4").toFile(); + try { + try (OutputStream os = new LZ4FrameOutputStream(new FileOutputStream(lz4File))) { + try (InputStream is = new FileInputStream(tmpFile)) { + copy(is, os); + } + } + + try (InputStream is = new LZ4FrameInputStream(new FileInputStream(lz4File))) { + Assert.assertEquals("available() should be 0 before first read", 0, is.available()); + + if (is.read() != -1 && testSize > 1) { + Assert.assertTrue( + "After reading 1 byte, available() should report > 0 bytes ready in the buffer", + is.available() > 0 + ); + } + } + } finally { + lz4File.delete(); + } + } }