working on issue 9

This commit is contained in:
Taro L. Saito 2011-04-03 17:05:33 +09:00
parent a5ad209e8f
commit 0a4ee3c6fb
2 changed files with 35 additions and 3 deletions

View File

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

View File

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