mirror of
https://github.com/xerial/snappy-java.git
synced 2025-04-08 19:35:08 +02:00
#89: Fixes SnappyInputStream not to throw an IOException when the input is empty
This commit is contained in:
parent
dfc9322a5b
commit
164e51da2e
@ -82,7 +82,7 @@ public class SnappyInputStream extends InputStream
|
|||||||
readBytes += ret;
|
readBytes += ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Quick test of the header
|
// Quick test of the header
|
||||||
if (readBytes < header.length || header[0] != SnappyCodec.MAGIC_HEADER[0]) {
|
if (readBytes < header.length || header[0] != SnappyCodec.MAGIC_HEADER[0]) {
|
||||||
// do the default uncompression
|
// do the default uncompression
|
||||||
readFully(header, readBytes);
|
readFully(header, readBytes);
|
||||||
@ -106,7 +106,11 @@ public class SnappyInputStream extends InputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void readFully(byte[] fragment, int fragmentLength) throws IOException {
|
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
|
compressed = new byte[Math.max(8 * 1024, fragmentLength)]; // 8K
|
||||||
System.arraycopy(fragment, 0, compressed, 0, fragmentLength);
|
System.arraycopy(fragment, 0, compressed, 0, fragmentLength);
|
||||||
int cursor = fragmentLength;
|
int cursor = fragmentLength;
|
||||||
|
@ -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();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
byte[] buf = new byte[4096];
|
byte[] buf = new byte[4096];
|
||||||
for (int readData = 0; (readData = input.read()) != -1;) {
|
for (int readData = 0; (readData = input.read()) != -1;) {
|
||||||
@ -108,7 +108,7 @@ public class SnappyInputStreamTest
|
|||||||
byte[] compressed = Snappy.compress(orig);
|
byte[] compressed = Snappy.compress(orig);
|
||||||
|
|
||||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||||
byte[] uncompressed = biteWiseReadFully(in);
|
byte[] uncompressed = byteWiseReadFully(in);
|
||||||
|
|
||||||
assertEquals(orig.length, uncompressed.length);
|
assertEquals(orig.length, uncompressed.length);
|
||||||
assertArrayEquals(orig, uncompressed);
|
assertArrayEquals(orig, uncompressed);
|
||||||
@ -129,4 +129,17 @@ public class SnappyInputStreamTest
|
|||||||
in.close();
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user