Fixes issue 34 Implement available() method

This commit is contained in:
Taro L. Saito 2011-10-25 10:39:27 +09:00
parent 540ee70f74
commit 72383d990c
2 changed files with 43 additions and 6 deletions

View File

@ -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;
}
}
}
}

View File

@ -48,13 +48,18 @@ public class SnappyInputStreamTest
}
public static byte[] readFully(InputStream input) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
for (int readBytes = 0; (readBytes = input.read(buf)) != -1;) {
out.write(buf, 0, readBytes);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
for (int readBytes = 0; (readBytes = input.read(buf)) != -1;) {
out.write(buf, 0, readBytes);
}
out.flush();
return out.toByteArray();
}
finally {
input.close();
}
out.flush();
return out.toByteArray();
}
public static byte[] biteWiseReadFully(InputStream input) throws IOException {
@ -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();
}
}