Author: bodewig
Date: Sat Dec 7 07:30:18 2013
New Revision: 1548803
URL: http://svn.apache.org/r1548803
Log:
COMPRESS-147 unit test for the remaining chunk types along with
necessary fixes.
The test archive is hand-crafted and the CRCs are wrong right now,
will be fixed once I implement CRC verification inside the stream.
Added:
commons/proper/compress/trunk/src/test/resources/mixed.txt.sz (with props)
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java?rev=1548803&r1=1548802&r2=1548803&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
(original)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
Sat Dec 7 07:30:18 2013
@@ -125,6 +125,9 @@ public class FramedSnappyCompressorInput
int read = -1;
if (inUncompressedChunk) {
int amount = Math.min(uncompressedBytesRemaining, len);
+ if (amount == 0) {
+ return -1;
+ }
read = in.read(b, off, amount);
if (read != -1) {
uncompressedBytesRemaining -= read;
@@ -144,6 +147,7 @@ public class FramedSnappyCompressorInput
}
private void readNextBlock() throws IOException {
+ inUncompressedChunk = false;
int type = readOneByte();
if (type == -1) {
endReached = true;
@@ -158,12 +162,14 @@ public class FramedSnappyCompressorInput
readNextBlock();
} else if (type >= MIN_UNSKIPPABLE_TYPE && type <=
MAX_UNSKIPPABLE_TYPE) {
throw new IOException("unskippable chunk with type " + type
+ + " (hex " + Integer.toHexString(type) + ")"
+ " detected.");
} else if (type == UNCOMPRESSED_CHUNK_TYPE) {
- uncompressedBytesRemaining = readSize();
+ inUncompressedChunk = true;
+ uncompressedBytesRemaining = readSize() - 4 /* CRC */;
readCrc();
} else if (type == COMPRESSED_CHUNK_TYPE) {
- int size = readSize();
+ int size = readSize() - 4 /* CRC */;
readCrc();
currentCompressedChunk =
new SnappyCompressorInputStream(new BoundedInputStream(in,
size));
Modified:
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java?rev=1548803&r1=1548802&r2=1548803&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
(original)
+++
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
Sat Dec 7 07:30:18 2013
@@ -18,7 +18,10 @@
*/
package org.apache.commons.compress.compressors;
+import static org.junit.Assert.assertArrayEquals;
+
import java.io.BufferedInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -117,8 +120,8 @@ public final class FramedSnappyTestCase
try {
FileInputStream gz = new FileInputStream(outputGz);
try {
- assertTrue(Arrays.equals(IOUtils.toByteArray(sz),
- IOUtils.toByteArray(gz)));
+ assertArrayEquals(IOUtils.toByteArray(sz),
+ IOUtils.toByteArray(gz));
} finally {
gz.close();
}
@@ -127,6 +130,28 @@ public final class FramedSnappyTestCase
}
}
+ public void testRemainingChunkTypes() throws Exception {
+ final FileInputStream isSz = new
FileInputStream(getFile("mixed.txt.sz"));
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try {
+ CompressorInputStream in = new
FramedSnappyCompressorInputStream(isSz);
+ IOUtils.copy(in, out);
+ out.close();
+ } finally {
+ isSz.close();
+ }
+
+ assertArrayEquals(new byte[] { '1', '2', '3', '4',
+ '5', '6', '7', '8', '9',
+ '5', '6', '7', '8', '9',
+ '5', '6', '7', '8', '9',
+ '5', '6', '7', '8', '9',
+ '5', '6', '7', '8', '9', 10,
+ '1', '2', '3', '4',
+ '1', '2', '3', '4',
+ }, out.toByteArray());
+ }
+
private void testUnarchive(StreamWrapper<CompressorInputStream> wrapper)
throws Exception {
final File input = getFile("bla.tar.sz");
final File output = new File(dir, "bla.tar");
@@ -154,8 +179,8 @@ public final class FramedSnappyTestCase
try {
FileInputStream orig = new FileInputStream(original);
try {
- assertTrue(Arrays.equals(IOUtils.toByteArray(written),
- IOUtils.toByteArray(orig)));
+ assertArrayEquals(IOUtils.toByteArray(written),
+ IOUtils.toByteArray(orig));
} finally {
orig.close();
}
Added: commons/proper/compress/trunk/src/test/resources/mixed.txt.sz
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/resources/mixed.txt.sz?rev=1548803&view=auto
==============================================================================
Binary file - no diff available.
Propchange: commons/proper/compress/trunk/src/test/resources/mixed.txt.sz
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream