diff --git a/src/main/java/org/xerial/snappy/SnappyInputStream.java b/src/main/java/org/xerial/snappy/SnappyInputStream.java index 9feebdb..4d1886b 100755 --- a/src/main/java/org/xerial/snappy/SnappyInputStream.java +++ b/src/main/java/org/xerial/snappy/SnappyInputStream.java @@ -83,7 +83,7 @@ public class SnappyInputStream extends InputStream protected void readFully(byte[] fragment, int fragmentLength) throws IOException { // read the entire input data to the buffer - compressed = new byte[Math.max(blockSize, fragmentLength)]; + compressed = new byte[Math.max(SnappyOutputStream.DEFAULT_BLOCK_SIZE, fragmentLength)]; System.arraycopy(fragment, 0, compressed, 0, fragmentLength); int cursor = fragmentLength; for (int readBytes = 0; (readBytes = in.read(compressed, cursor, compressed.length - cursor)) != -1;) { @@ -95,6 +95,8 @@ public class SnappyInputStream extends InputStream } } + finishedReading = true; + // Uncompress try { int uncompressedLength = Snappy.uncompressedLength(compressed, 0, cursor); @@ -170,8 +172,15 @@ public class SnappyInputStream extends InputStream @Override public int read() throws IOException { - byte[] buf = new byte[1]; - return read(buf, 0, 1); + if (uncompressedCursor < uncompressedLimit) { + return uncompressed[uncompressedCursor++]; + } + else { + if (hasNextChunk()) + return read(); + else + return -1; + } } } diff --git a/src/test/java/org/xerial/snappy/SnappyInputStreamTest.java b/src/test/java/org/xerial/snappy/SnappyInputStreamTest.java index 00d7390..d61520e 100755 --- a/src/test/java/org/xerial/snappy/SnappyInputStreamTest.java +++ b/src/test/java/org/xerial/snappy/SnappyInputStreamTest.java @@ -56,6 +56,16 @@ public class SnappyInputStreamTest return out.toByteArray(); } + public static byte[] biteWiseReadFully(InputStream input) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buf = new byte[4096]; + for (int readData = 0; (readData = input.read()) != -1;) { + out.write(readData); + } + out.flush(); + return out.toByteArray(); + } + @Test public void read() throws Exception { ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream(); @@ -85,4 +95,17 @@ public class SnappyInputStreamTest assertEquals(orig.length, uncompressed.length); assertArrayEquals(orig, uncompressed); } + + @Test + public void biteWiseRead() throws Exception { + byte[] orig = readResourceFile("alice29.txt"); + byte[] compressed = Snappy.compress(orig); + + SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed)); + byte[] uncompressed = biteWiseReadFully(in); + + assertEquals(orig.length, uncompressed.length); + assertArrayEquals(orig, uncompressed); + } + }