#89: Fixes SnappyInputStream not to throw an IOException when the input is empty

This commit is contained in:
Taro L. Saito 2014-10-23 12:12:39 +09:00
parent dfc9322a5b
commit 164e51da2e
2 changed files with 21 additions and 4 deletions

View File

@ -82,7 +82,7 @@ public class SnappyInputStream extends InputStream
readBytes += ret;
}
// Quick test of the header
// Quick test of the header
if (readBytes < header.length || header[0] != SnappyCodec.MAGIC_HEADER[0]) {
// do the default uncompression
readFully(header, readBytes);
@ -106,7 +106,11 @@ public class SnappyInputStream extends InputStream
}
protected void readFully(byte[] fragment, int fragmentLength) throws IOException {
// read the entire input data to the buffer
if(fragmentLength == 0) {
finishedReading = true;
return;
}
// read the entire input data to the buffer
compressed = new byte[Math.max(8 * 1024, fragmentLength)]; // 8K
System.arraycopy(fragment, 0, compressed, 0, fragmentLength);
int cursor = fragmentLength;

View File

@ -62,7 +62,7 @@ public class SnappyInputStreamTest
}
}
public static byte[] biteWiseReadFully(InputStream input) throws IOException {
public static byte[] byteWiseReadFully(InputStream input) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
for (int readData = 0; (readData = input.read()) != -1;) {
@ -108,7 +108,7 @@ public class SnappyInputStreamTest
byte[] compressed = Snappy.compress(orig);
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
byte[] uncompressed = biteWiseReadFully(in);
byte[] uncompressed = byteWiseReadFully(in);
assertEquals(orig.length, uncompressed.length);
assertArrayEquals(orig, uncompressed);
@ -129,4 +129,17 @@ public class SnappyInputStreamTest
in.close();
}
@Test
public void emptyStream() throws Exception {
try {
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(new byte[0]));
byte[] uncompressed = readFully(in);
assertEquals(0, uncompressed.length);
}
catch(Exception e) {
fail("should not reach here");
}
}
}