Package org.iq80.snappy
Class AbstractSnappyOutputStream
java.lang.Object
java.io.OutputStream
org.iq80.snappy.AbstractSnappyOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
SnappyFramedOutputStream,SnappyOutputStream
This is a base class supporting both the
SnappyOutputStream and
SnappyFramedOutputStream.
Delegates writing the header bytes and individual frames to the specific implementations. Implementations may also override the crc32 checksum calculation.
- Since:
- 0.4
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intprivate final byte[]private booleanprivate final doubleprivate final OutputStreamprivate final byte[]private intprivate final BufferRecycler -
Constructor Summary
ConstructorsConstructorDescriptionAbstractSnappyOutputStream(OutputStream out, int blockSize, double minCompressionRatio) -
Method Summary
Modifier and TypeMethodDescriptionprotected intcalculateCRC32C(byte[] data, int offset, int length) Calculates a CRC32C checksum over the data.final voidclose()private voidcopyToBuffer(byte[] input, int offset, int length) final voidflush()private voidCompresses and writes out any buffered data.voidwrite(byte[] input, int offset, int length) voidwrite(int b) protected abstract voidwriteBlock(OutputStream out, byte[] data, int offset, int length, boolean compressed, int crc32c) Write a frame (block) to out.private voidwriteCompressed(byte[] input, int offset, int length) Calculatesthe crc, compresses the data, determines if the compression ratio is acceptable and callswriteBlock(OutputStream, byte[], int, int, boolean, int)to actually write the frame.protected abstract voidwriteHeader(OutputStream out) Writes the implementation specific header or "marker bytes" to out.Methods inherited from class java.io.OutputStream
nullOutputStream, write
-
Field Details
-
recycler
-
blockSize
private final int blockSize -
buffer
private final byte[] buffer -
outputBuffer
private final byte[] outputBuffer -
minCompressionRatio
private final double minCompressionRatio -
out
-
position
private int position -
closed
private boolean closed
-
-
Constructor Details
-
AbstractSnappyOutputStream
public AbstractSnappyOutputStream(OutputStream out, int blockSize, double minCompressionRatio) throws IOException - Parameters:
out- The underlyingOutputStreamto write to. Must not benull.blockSize- The block size (of raw data) to compress before writing frames to out.minCompressionRatio- Defines the minimum compression ratio (compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].- Throws:
IOException
-
-
Method Details
-
writeHeader
Writes the implementation specific header or "marker bytes" to out.- Parameters:
out- The underlyingOutputStream.- Throws:
IOException
-
write
- Specified by:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
flush
- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
copyToBuffer
private void copyToBuffer(byte[] input, int offset, int length) -
flushBuffer
Compresses and writes out any buffered data. This does nothing if there is no currently buffered data.- Throws:
IOException
-
writeCompressed
Calculatesthe crc, compresses the data, determines if the compression ratio is acceptable and callswriteBlock(OutputStream, byte[], int, int, boolean, int)to actually write the frame.- Parameters:
input- The byte[] containing the raw data to be compressed.offset- The offset into input where the data starts.length- The amount of data in input.- Throws:
IOException
-
calculateCRC32C
protected int calculateCRC32C(byte[] data, int offset, int length) Calculates a CRC32C checksum over the data.This can be overridden to provider alternative implementations (such as returning 0 if checksums are not desired).
- Returns:
- The CRC32 checksum.
-
writeBlock
protected abstract void writeBlock(OutputStream out, byte[] data, int offset, int length, boolean compressed, int crc32c) throws IOException Write a frame (block) to out.- Parameters:
out- TheOutputStreamto write to.data- The data to write.offset- The offset in data to start at.length- The length of data to use.compressed- Indicates if data is the compressed or raw content. This is based on whether the compression ratio desired is reached.crc32c- The calculated checksum.- Throws:
IOException
-