mirror of
https://github.com/xerial/snappy-java.git
synced 2025-07-20 12:34:30 +02:00
working on issue 9
This commit is contained in:
parent
a5ad209e8f
commit
0a4ee3c6fb
@ -83,7 +83,7 @@ public class SnappyInputStream extends InputStream
|
||||
|
||||
protected void readFully(byte[] fragment, int fragmentLength) throws IOException {
|
||||
// read the entire input data to the buffer
|
||||
compressed = new byte[Math.max(blockSize, fragmentLength)];
|
||||
compressed = new byte[Math.max(SnappyOutputStream.DEFAULT_BLOCK_SIZE, fragmentLength)];
|
||||
System.arraycopy(fragment, 0, compressed, 0, fragmentLength);
|
||||
int cursor = fragmentLength;
|
||||
for (int readBytes = 0; (readBytes = in.read(compressed, cursor, compressed.length - cursor)) != -1;) {
|
||||
@ -95,6 +95,8 @@ public class SnappyInputStream extends InputStream
|
||||
}
|
||||
}
|
||||
|
||||
finishedReading = true;
|
||||
|
||||
// Uncompress
|
||||
try {
|
||||
int uncompressedLength = Snappy.uncompressedLength(compressed, 0, cursor);
|
||||
@ -170,8 +172,15 @@ public class SnappyInputStream extends InputStream
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
byte[] buf = new byte[1];
|
||||
return read(buf, 0, 1);
|
||||
if (uncompressedCursor < uncompressedLimit) {
|
||||
return uncompressed[uncompressedCursor++];
|
||||
}
|
||||
else {
|
||||
if (hasNextChunk())
|
||||
return read();
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -56,6 +56,16 @@ public class SnappyInputStreamTest
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static byte[] biteWiseReadFully(InputStream input) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[4096];
|
||||
for (int readData = 0; (readData = input.read()) != -1;) {
|
||||
out.write(readData);
|
||||
}
|
||||
out.flush();
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws Exception {
|
||||
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
|
||||
@ -85,4 +95,17 @@ public class SnappyInputStreamTest
|
||||
assertEquals(orig.length, uncompressed.length);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void biteWiseRead() throws Exception {
|
||||
byte[] orig = readResourceFile("alice29.txt");
|
||||
byte[] compressed = Snappy.compress(orig);
|
||||
|
||||
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
|
||||
byte[] uncompressed = biteWiseReadFully(in);
|
||||
|
||||
assertEquals(orig.length, uncompressed.length);
|
||||
assertArrayEquals(orig, uncompressed);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user