Skip to content

Commit 164e51d

Browse files
committed
xerial#89: Fixes SnappyInputStream not to throw an IOException when the input is empty
1 parent dfc9322 commit 164e51d

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/main/java/org/xerial/snappy/SnappyInputStream.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected void readHeader() throws IOException {
8282
readBytes += ret;
8383
}
8484

85-
// Quick test of the header
85+
// Quick test of the header
8686
if (readBytes < header.length || header[0] != SnappyCodec.MAGIC_HEADER[0]) {
8787
// do the default uncompression
8888
readFully(header, readBytes);
@@ -106,7 +106,11 @@ protected void readHeader() throws IOException {
106106
}
107107

108108
protected void readFully(byte[] fragment, int fragmentLength) throws IOException {
109-
// read the entire input data to the buffer
109+
if(fragmentLength == 0) {
110+
finishedReading = true;
111+
return;
112+
}
113+
// read the entire input data to the buffer
110114
compressed = new byte[Math.max(8 * 1024, fragmentLength)]; // 8K
111115
System.arraycopy(fragment, 0, compressed, 0, fragmentLength);
112116
int cursor = fragmentLength;

src/test/java/org/xerial/snappy/SnappyInputStreamTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static byte[] readFully(InputStream input) throws IOException {
6262
}
6363
}
6464

65-
public static byte[] biteWiseReadFully(InputStream input) throws IOException {
65+
public static byte[] byteWiseReadFully(InputStream input) throws IOException {
6666
ByteArrayOutputStream out = new ByteArrayOutputStream();
6767
byte[] buf = new byte[4096];
6868
for (int readData = 0; (readData = input.read()) != -1;) {
@@ -108,7 +108,7 @@ public void biteWiseRead() throws Exception {
108108
byte[] compressed = Snappy.compress(orig);
109109

110110
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
111-
byte[] uncompressed = biteWiseReadFully(in);
111+
byte[] uncompressed = byteWiseReadFully(in);
112112

113113
assertEquals(orig.length, uncompressed.length);
114114
assertArrayEquals(orig, uncompressed);
@@ -129,4 +129,17 @@ public void available() throws Exception {
129129
in.close();
130130
}
131131

132+
@Test
133+
public void emptyStream() throws Exception {
134+
try {
135+
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(new byte[0]));
136+
byte[] uncompressed = readFully(in);
137+
assertEquals(0, uncompressed.length);
138+
}
139+
catch(Exception e) {
140+
fail("should not reach here");
141+
}
142+
143+
}
144+
132145
}

0 commit comments

Comments
 (0)