Fixes issue 34 Implement available() method
This commit is contained in:
parent
540ee70f74
commit
72383d990c
|
@ -387,4 +387,21 @@ public class SnappyInputStream extends InputStream
|
|||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.io.InputStream#available()
|
||||
*/
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
if (uncompressedCursor < uncompressedLimit) {
|
||||
return uncompressedLimit - uncompressedCursor;
|
||||
}
|
||||
else {
|
||||
if (hasNextChunk()) {
|
||||
return uncompressedLimit - uncompressedCursor;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ public class SnappyInputStreamTest
|
|||
}
|
||||
|
||||
public static byte[] readFully(InputStream input) throws IOException {
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[4096];
|
||||
for (int readBytes = 0; (readBytes = input.read(buf)) != -1;) {
|
||||
|
@ -56,6 +57,10 @@ public class SnappyInputStreamTest
|
|||
out.flush();
|
||||
return out.toByteArray();
|
||||
}
|
||||
finally {
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] biteWiseReadFully(InputStream input) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
@ -107,6 +112,21 @@ public class SnappyInputStreamTest
|
|||
|
||||
assertEquals(orig.length, uncompressed.length);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void available() throws Exception {
|
||||
byte[] orig = readResourceFile("testdata/calgary/paper6");
|
||||
byte[] compressed = Snappy.compress(orig);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||
byte[] buf = new byte[4];
|
||||
for (int readBytes = 0; (readBytes = in.read(buf)) != -1;) {
|
||||
assertTrue(in.available() >= 0);
|
||||
}
|
||||
assertTrue(in.available() == 0);
|
||||
in.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue