private void writeCrc() throws IOException { checksum.update(buffer, 0, currentIndex); writeLittleEndian(4, mask(checksum.getValue())); checksum.reset(); }
/** {@inheritDoc} */ @Override public int read() throws IOException { return read(oneByte, 0, 1) == -1 ? -1 : oneByte[0] & 0xFF; }
/** * 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 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; }
/** {@inheritDoc} */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { int read = readOnce(b, off, len); if (read == -1) { readNextBlock(); if (endReached) { return -1; } read = readOnce(b, off, len); } return read; }
private void writeLiteralBlockOneSizeByte(LZ77Compressor.LiteralBlock block, int len) throws IOException { writeLiteralBlockWithSize(ONE_SIZE_BYTE_MARKER, 1, len, block); }
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); }
@Override public void write(int b) throws IOException { oneByte[0] = (byte) (b & 0xff); write(oneByte); }
/** Create a new PureJavaCrc32 object. */ public PureJavaCrc32C() { reset(); // non-private, but the class is now final }
/** * Constructor using a configurable buffer size. * * @param is * An InputStream to read compressed data from * @param blockSize * The block size used in compression * * @throws IOException if reading fails */ public SnappyCompressorInputStream(final InputStream is, final int blockSize) throws IOException { super(is, blockSize); uncompressedBytesRemaining = size = (int) readSize(); }
@Override public void close() throws IOException { try { finish(); } finally { out.close(); } }
/** {@inheritDoc} */ @Override public void close() throws IOException { try { if (currentCompressedChunk != null) { currentCompressedChunk.close(); currentCompressedChunk = null; } } finally { in.close(); } }
/** * 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 writeLiteralBlockFourSizeBytes(LZ77Compressor.LiteralBlock block, int len) throws IOException { writeLiteralBlockWithSize(FOUR_SIZE_BYTE_MARKER, 4, len, block); }
private void writeBackReferenceWithTwoOffsetBytes(int len, int offset) throws IOException { writeBackReferenceWithLittleEndianOffset(TWO_BYTE_COPY_TAG, 2, len, offset); }
private void writeLiteralBlockTwoSizeBytes(LZ77Compressor.LiteralBlock block, int len) throws IOException { writeLiteralBlockWithSize(TWO_SIZE_BYTE_MARKER, 2, len, block); }
private void writeLiteralBlockNoSizeBytes(LZ77Compressor.LiteralBlock block, int len) throws IOException { writeLiteralBlockWithSize(len - 1 << 2, 0, len, block); }
private void writeLiteralBlockThreeSizeBytes(LZ77Compressor.LiteralBlock block, int len) throws IOException { writeLiteralBlockWithSize(THREE_SIZE_BYTE_MARKER, 3, len, block); }