@Override public void close() throws IOException { try { finish(); } finally { out.close(); } }
/** * Compresses all remaining data and writes it to the stream, * doesn't close the underlying stream. * @throws IOException if an error occurs */ public void finish() throws IOException { if (currentIndex > 0) { flushBuffer(); } }
private void writeCrc() throws IOException { checksum.update(buffer, 0, currentIndex); writeLittleEndian(4, mask(checksum.getValue())); checksum.reset(); }
private void flushBuffer() throws IOException { out.write(FramedSnappyCompressorInputStream.COMPRESSED_CHUNK_TYPE); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (OutputStream o = new SnappyCompressorOutputStream(baos, currentIndex, params)) { o.write(buffer, 0, currentIndex); } byte[] b = baos.toByteArray(); writeLittleEndian(3, b.length + 4L /* CRC */); writeCrc(); out.write(b); currentIndex = 0; }
@Test public void testDecompressSnappy() throws IOException { final File tmpDir = temporaryFolder.newFolder("testDecompressSnappy"); final File snappyFile = new File(tmpDir, testFile.getName() + ".sz"); Assert.assertFalse(snappyFile.exists()); try (final OutputStream out = new FramedSnappyCompressorOutputStream(new FileOutputStream(snappyFile))) { ByteStreams.copy(new FileInputStream(testFile), out); } try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(snappyFile), snappyFile.getName())) { assertGoodDataStream(inputStream); } }
@Override public void write(int b) throws IOException { oneByte[0] = (byte) (b & 0xff); write(oneByte); }
private void flushBuffer() throws IOException { out.write(FramedSnappyCompressorInputStream.COMPRESSED_CHUNK_TYPE); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (OutputStream o = new SnappyCompressorOutputStream(baos, currentIndex, params)) { o.write(buffer, 0, currentIndex); } byte[] b = baos.toByteArray(); writeLittleEndian(3, b.length + 4L /* CRC */); writeCrc(); out.write(b); currentIndex = 0; }
@Override public void write(int b) throws IOException { oneByte[0] = (byte) (b & 0xff); write(oneByte); }
/** * @param stream the stream to write to, should be buffered */ @Override public CompressorOutputStream getCompressorStream(OutputStream stream) throws IOException { if (!framed) { throw new UnsupportedOperationException("Must know the uncompressed size" + " for non-framed snappy"); } return new FramedSnappyCompressorOutputStream(stream); } }
private void writeCrc() throws IOException { checksum.update(buffer, 0, currentIndex); writeLittleEndian(4, mask(checksum.getValue())); checksum.reset(); }
@Override public void write(byte[] data, int off, int len) throws IOException { if (currentIndex + len > MAX_COMPRESSED_BUFFER_SIZE) { flushBuffer(); while (len > MAX_COMPRESSED_BUFFER_SIZE) { System.arraycopy(data, off, buffer, 0, MAX_COMPRESSED_BUFFER_SIZE); off += MAX_COMPRESSED_BUFFER_SIZE; len -= MAX_COMPRESSED_BUFFER_SIZE; currentIndex = MAX_COMPRESSED_BUFFER_SIZE; flushBuffer(); } } System.arraycopy(data, off, buffer, currentIndex, len); currentIndex += len; }
@Override public void close() throws IOException { finish(); out.close(); }
return new FramedSnappyCompressorOutputStream(out);
/** * Compresses all remaining data and writes it to the stream, * doesn't close the underlying stream. * @throws IOException if an error occurs */ public void finish() throws IOException { if (currentIndex > 0) { flushBuffer(); } }
@Override public void write(byte[] data, int off, int len) throws IOException { if (currentIndex + len > MAX_COMPRESSED_BUFFER_SIZE) { flushBuffer(); while (len > MAX_COMPRESSED_BUFFER_SIZE) { System.arraycopy(data, off, buffer, 0, MAX_COMPRESSED_BUFFER_SIZE); off += MAX_COMPRESSED_BUFFER_SIZE; len -= MAX_COMPRESSED_BUFFER_SIZE; currentIndex = MAX_COMPRESSED_BUFFER_SIZE; flushBuffer(); } } System.arraycopy(data, off, buffer, currentIndex, len); currentIndex += len; }