mirror of
https://github.com/xerial/snappy-java.git
synced 2025-07-28 00:14:15 +02:00
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,13 +48,18 @@ public class SnappyInputStreamTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] readFully(InputStream input) throws IOException {
|
public static byte[] readFully(InputStream input) throws IOException {
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
try {
|
||||||
byte[] buf = new byte[4096];
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
for (int readBytes = 0; (readBytes = input.read(buf)) != -1;) {
|
byte[] buf = new byte[4096];
|
||||||
out.write(buf, 0, readBytes);
|
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 {
|
public static byte[] biteWiseReadFully(InputStream input) throws IOException {
|
||||||
@ -107,6 +112,21 @@ public class SnappyInputStreamTest
|
|||||||
|
|
||||||
assertEquals(orig.length, uncompressed.length);
|
assertEquals(orig.length, uncompressed.length);
|
||||||
assertArrayEquals(orig, uncompressed);
|
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…
x
Reference in New Issue
Block a user