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; }
/** * Constructor using a configurable block size. * * @param os the outputstream to write compressed data to * @param uncompressedSize the uncompressed size of data * @param blockSize the block size used - must be a power of two * @throws IOException if writing of the size fails */ public SnappyCompressorOutputStream(final OutputStream os, final long uncompressedSize, final int blockSize) throws IOException { this(os, uncompressedSize, createParameterBuilder(blockSize).build()); }
@Override public void close() throws IOException { try { finish(); } finally { os.close(); } }
private void writeLiteralBlock(LZ77Compressor.LiteralBlock block) throws IOException { int len = block.getLength(); if (len <= MAX_LITERAL_SIZE_WITHOUT_SIZE_BYTES) { writeLiteralBlockNoSizeBytes(block, len); } else if (len <= MAX_LITERAL_SIZE_WITH_ONE_SIZE_BYTE) { writeLiteralBlockOneSizeByte(block, len); } else if (len <= MAX_LITERAL_SIZE_WITH_TWO_SIZE_BYTES) { writeLiteralBlockTwoSizeBytes(block, len); } else if (len <= MAX_LITERAL_SIZE_WITH_THREE_SIZE_BYTES) { writeLiteralBlockThreeSizeBytes(block, len); } else { writeLiteralBlockFourSizeBytes(block, len); } }
private void writeBackReference(LZ77Compressor.BackReference block) throws IOException { final int len = block.getLength(); final int offset = block.getOffset(); if (len >= MIN_MATCH_LENGTH_WITH_ONE_OFFSET_BYTE && len <= MAX_MATCH_LENGTH_WITH_ONE_OFFSET_BYTE && offset <= MAX_OFFSET_WITH_ONE_OFFSET_BYTE) { writeBackReferenceWithOneOffsetByte(len, offset); } else if (offset < MAX_OFFSET_WITH_TWO_OFFSET_BYTES) { writeBackReferenceWithTwoOffsetBytes(len, offset); } else { writeBackReferenceWithFourOffsetBytes(len, offset); } }
private void writeBackReferenceWithTwoOffsetBytes(int len, int offset) throws IOException { writeBackReferenceWithLittleEndianOffset(TWO_BYTE_COPY_TAG, 2, len, offset); }
@Override public void write(int b) throws IOException { oneByte[0] = (byte) (b & 0xff); write(oneByte); }
private void writeLiteralBlock(LZ77Compressor.LiteralBlock block) throws IOException { int len = block.getLength(); if (len <= MAX_LITERAL_SIZE_WITHOUT_SIZE_BYTES) { writeLiteralBlockNoSizeBytes(block, len); } else if (len <= MAX_LITERAL_SIZE_WITH_ONE_SIZE_BYTE) { writeLiteralBlockOneSizeByte(block, len); } else if (len <= MAX_LITERAL_SIZE_WITH_TWO_SIZE_BYTES) { writeLiteralBlockTwoSizeBytes(block, len); } else if (len <= MAX_LITERAL_SIZE_WITH_THREE_SIZE_BYTES) { writeLiteralBlockThreeSizeBytes(block, len); } else { writeLiteralBlockFourSizeBytes(block, len); } }
private void writeBackReference(LZ77Compressor.BackReference block) throws IOException { final int len = block.getLength(); final int offset = block.getOffset(); if (len >= MIN_MATCH_LENGTH_WITH_ONE_OFFSET_BYTE && len <= MAX_MATCH_LENGTH_WITH_ONE_OFFSET_BYTE && offset <= MAX_OFFSET_WITH_ONE_OFFSET_BYTE) { writeBackReferenceWithOneOffsetByte(len, offset); } else if (offset < MAX_OFFSET_WITH_TWO_OFFSET_BYTES) { writeBackReferenceWithTwoOffsetBytes(len, offset); } else { writeBackReferenceWithFourOffsetBytes(len, offset); } }
private void writeBackReferenceWithFourOffsetBytes(int len, int offset) throws IOException { writeBackReferenceWithLittleEndianOffset(FOUR_BYTE_COPY_TAG, 4, len, offset); }
@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; }
/** * Constructs a new output stream that compresses * snappy-framed-compressed data to the specified output stream. * @param out the OutputStream to which to write the compressed data * @throws IOException if writing the signature fails */ public FramedSnappyCompressorOutputStream(final OutputStream out) throws IOException { this(out, SnappyCompressorOutputStream.createParameterBuilder(SnappyCompressorInputStream.DEFAULT_BLOCK_SIZE) .build()); }
private void writeBackReferenceWithTwoOffsetBytes(int len, int offset) throws IOException { writeBackReferenceWithLittleEndianOffset(TWO_BYTE_COPY_TAG, 2, len, offset); }
@Override public void close() throws IOException { finish(); os.close(); }
@Override public CompressorOutputStream getCompressorStream(OutputStream stream) throws IOException { if (isFramed() || getSrc() == null || getSrc().getSize() < 0) { return super.getCompressorStream(stream); } return new SnappyCompressorOutputStream(stream, getSrc().getSize()); } }
/** * Constructor using a configurable block size. * * @param os the outputstream to write compressed data to * @param uncompressedSize the uncompressed size of data * @param blockSize the block size used - must be a power of two * @throws IOException if writing of the size fails */ public SnappyCompressorOutputStream(final OutputStream os, final long uncompressedSize, final int blockSize) throws IOException { this(os, uncompressedSize, createParameterBuilder(blockSize).build()); }
private void writeBackReferenceWithFourOffsetBytes(int len, int offset) throws IOException { writeBackReferenceWithLittleEndianOffset(FOUR_BYTE_COPY_TAG, 4, len, offset); }
/** * Constructs a new output stream that compresses * snappy-framed-compressed data to the specified output stream. * @param out the OutputStream to which to write the compressed data * @throws IOException if writing the signature fails */ public FramedSnappyCompressorOutputStream(final OutputStream out) throws IOException { this(out, SnappyCompressorOutputStream.createParameterBuilder(SnappyCompressorInputStream.DEFAULT_BLOCK_SIZE) .build()); }