@Signature public void __construct(Environment env, OutputStream outputStream, boolean framed) throws IOException { this.outputStream = framed ? new FramedLZ4CompressorOutputStream(outputStream) : new BlockLZ4CompressorOutputStream(outputStream); } }
@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 (!finished) { if (currentIndex > 0) { flushBlock(); } writeTrailer(); finished = true; } }
@Override public void write(int b) throws IOException { oneByte[0] = (byte) (b & 0xff); write(oneByte); }
/** * Constructs a new output stream that compresses data using the * LZ4 frame format using the given block size. * @param out the OutputStream to which to write the compressed data * @param params the parameters to use * @throws IOException if writing the signature fails */ public FramedLZ4CompressorOutputStream(OutputStream out, Parameters params) throws IOException { this.params = params; blockData = new byte[params.blockSize.getSize()]; this.out = out; blockHash = params.withBlockChecksum ? new XXHash32() : null; out.write(FramedLZ4CompressorInputStream.LZ4_SIGNATURE); writeFrameDescriptor(); blockDependencyBuffer = params.withBlockDependency ? new byte[BlockLZ4CompressorInputStream.WINDOW_SIZE] : null; }
@Override public void write(byte[] data, int off, int len) throws IOException { if (params.withContentChecksum) { contentHash.update(data, off, len); } if (currentIndex + len > blockData.length) { flushBlock(); while (len > blockData.length) { System.arraycopy(data, off, blockData, 0, blockData.length); off += blockData.length; len -= blockData.length; currentIndex = blockData.length; flushBlock(); } } System.arraycopy(data, off, blockData, currentIndex, len); currentIndex += len; }
appendToBlockDependencyBuffer(blockData, 0, currentIndex);
@Override public void write(int b) throws IOException { oneByte[0] = (byte) (b & 0xff); write(oneByte); }
/** * Constructs a new output stream that compresses data using the * LZ4 frame format using the given block size. * @param out the OutputStream to which to write the compressed data * @param params the parameters to use * @throws IOException if writing the signature fails */ public FramedLZ4CompressorOutputStream(OutputStream out, Parameters params) throws IOException { this.params = params; blockData = new byte[params.blockSize.getSize()]; this.out = out; blockHash = params.withBlockChecksum ? new XXHash32() : null; out.write(FramedLZ4CompressorInputStream.LZ4_SIGNATURE); writeFrameDescriptor(); blockDependencyBuffer = params.withBlockDependency ? new byte[BlockLZ4CompressorInputStream.WINDOW_SIZE] : null; }
@Override public void write(byte[] data, int off, int len) throws IOException { if (params.withContentChecksum) { contentHash.update(data, off, len); } if (currentIndex + len > blockData.length) { flushBlock(); while (len > blockData.length) { System.arraycopy(data, off, blockData, 0, blockData.length); off += blockData.length; len -= blockData.length; currentIndex = blockData.length; flushBlock(); } } System.arraycopy(data, off, blockData, currentIndex, len); currentIndex += len; }
appendToBlockDependencyBuffer(blockData, 0, currentIndex);
/** * 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 (!finished) { if (currentIndex > 0) { flushBlock(); } writeTrailer(); finished = true; } }
@Override public void close() throws IOException { finish(); out.close(); }
/** * @param stream the stream to write to, should be buffered */ @Override public CompressorOutputStream getCompressorStream(OutputStream stream) throws IOException { return framed ? new FramedLZ4CompressorOutputStream(stream) : (CompressorOutputStream) new BlockLZ4CompressorOutputStream(stream); } }