/** * Creates a Snappy output stream with block checksums disabled. This is only useful for * apples-to-apples benchmarks with other compressors that do not perform block checksums. * * @param out the underlying output stream */ public static SnappyFramedOutputStream newChecksumFreeBenchmarkOutputStream(OutputStream out) throws IOException { return new SnappyFramedOutputStream(out, false); }
/** * Creates a Snappy output stream with block checksums disabled. This is only useful for * apples-to-apples benchmarks with other compressors that do not perform block checksums. * * @param out the underlying output stream */ public static SnappyFramedOutputStream newChecksumFreeBenchmarkOutputStream(OutputStream out) throws IOException { return new SnappyFramedOutputStream(out, false); }
private static byte[] compress(byte[] original) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream snappyOut = new SnappyFramedOutputStream(out); snappyOut.write(original); snappyOut.close(); return out.toByteArray(); }
@Test public void testExtraFlushes() throws Exception { byte[] random = getRandom(0.5, 500000); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream snappyOut = new SnappyFramedOutputStream(out); snappyOut.write(random); for (int i = 0; i < 10; i++) { snappyOut.flush(); } snappyOut.close(); byte[] compressed = out.toByteArray(); assertTrue(compressed.length < random.length); byte[] uncompressed = uncompress(compressed); assertEquals(uncompressed, random); }
@Test public void testCloseIsIdempotent() throws Exception { byte[] random = getRandom(0.5, 500000); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream snappyOut = new SnappyFramedOutputStream(out); snappyOut.write(random); snappyOut.close(); snappyOut.close(); byte[] compressed = out.toByteArray(); InputStream snappyIn = new SnappyFramedInputStream(new ByteArrayInputStream(compressed), true); byte[] uncompressed = toByteArray(snappyIn); assertEquals(uncompressed, random); snappyIn.close(); snappyIn.close(); }
@Test public void testSingleByteWrites() throws Exception { byte[] random = getRandom(0.5, 500000); java.io.ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream snappyOut = new SnappyFramedOutputStream(out); for (byte b : random) { snappyOut.write(b); } snappyOut.close(); byte[] compressed = out.toByteArray(); assertTrue(compressed.length < random.length); byte[] uncompressed = uncompress(compressed); assertEquals(uncompressed, random); }
@Test public void testLargeWrites() throws Exception { byte[] random = getRandom(0.5, 500000); java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(); OutputStream snappyOut = new SnappyFramedOutputStream(out); // partially fill buffer int small = 1000; snappyOut.write(random, 0, small); // write more than the buffer size snappyOut.write(random, small, random.length - small); // get compressed data snappyOut.close(); byte[] compressed = out.toByteArray(); assertTrue(compressed.length < random.length); // decompress byte[] uncompressed = uncompress(compressed); assertEquals(uncompressed, random); // decompress byte at a time InputStream in = new SnappyFramedInputStream(new ByteArrayInputStream(compressed), true); int i = 0; int c; while ((c = in.read()) != -1) { uncompressed[i++] = (byte) c; } assertEquals(i, random.length); assertEquals(uncompressed, random); }
/** * Tests that the presence of the marker bytes can appear as a valid frame * anywhere in stream. */ @Test public void testMarkerFrameInStream() throws IOException { int size = 500000; byte[] random = getRandom(0.5, size); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream os = new SnappyFramedOutputStream(out); byte[] markerFrame = getMarkerFrame(); for (int i = 0; i < size; ) { int toWrite = Math.max((size - i) / 4, 512); // write some data to be compressed os.write(random, i, Math.min(size - i, toWrite)); // force the write of a frame os.flush(); // write the marker frame to the underlying byte array output stream out.write(markerFrame); // this is not accurate for the final write, but at that point it // does not matter // as we will be exiting the for loop now i += toWrite; } byte[] compressed = out.toByteArray(); byte[] uncompressed = uncompress(compressed); assertEquals(random, uncompressed); }