Class SnappyFramedOutputStream
java.lang.Object
java.io.OutputStream
org.xerial.snappy.SnappyFramedOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Channel, WritableByteChannel
- Since:
- 1.1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intprivate final ByteBufferprivate final BufferPoolprivate booleanprivate final Checksumstatic final intThe default block size to use.static final doubleThe default min compression ratio to use.private final ByteBufferprivate final ByteBufferstatic final intThe x-snappy-framed specification allows for a chunk size up to 16,777,211 bytes in length.private final doubleprivate final WritableByteChannelprivate final ByteBuffer -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newSnappyFramedOutputStreamusing theDEFAULT_BLOCK_SIZEandDEFAULT_MIN_COMPRESSION_RATIO.SnappyFramedOutputStream(OutputStream out, int blockSize, double minCompressionRatio) Creates a newSnappyFramedOutputStreaminstance.SnappyFramedOutputStream(OutputStream out, int blockSize, double minCompressionRatio, BufferPool bufferPool) Creates a newSnappyFramedOutputStreaminstance.SnappyFramedOutputStream(OutputStream out, BufferPool bufferPool) Creates a newSnappyFramedOutputStreamusing theDEFAULT_BLOCK_SIZEandDEFAULT_MIN_COMPRESSION_RATIO.Creates a newSnappyFramedOutputStreamusing theDEFAULT_BLOCK_SIZEandDEFAULT_MIN_COMPRESSION_RATIO.SnappyFramedOutputStream(WritableByteChannel out, int blockSize, double minCompressionRatio) Creates a newSnappyFramedOutputStreaminstance.SnappyFramedOutputStream(WritableByteChannel out, int blockSize, double minCompressionRatio, BufferPool bufferPool) Creates a newSnappyFramedOutputStreaminstance.SnappyFramedOutputStream(WritableByteChannel out, BufferPool bufferPool) Creates a newSnappyFramedOutputStreamusing theDEFAULT_BLOCK_SIZEandDEFAULT_MIN_COMPRESSION_RATIO. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclose()final voidflush()private voidCompresses and writes out any buffered data.booleanisOpen()longTransfers all the content from is to thisOutputStream.longTransfers all the content from rbc to thisWritableByteChannel.voidwrite(byte[] input, int offset, int length) voidwrite(int b) intwrite(ByteBuffer src) private voidwriteBlock(WritableByteChannel out, ByteBuffer data, boolean compressed, int crc32c) Write a frame (block) to out.private voidwriteCompressed(ByteBuffer buffer) the crc, compresses the data, determines if the compression ratio is acceptable and callsinvalid reference
SnappyFramed#maskedCrc32c(byte[], int, int)writeBlock(java.nio.channels.WritableByteChannel, java.nio.ByteBuffer, boolean, int)to actually write the frame.private voidWrites the implementation specific header or "marker bytes" to out.Methods inherited from class OutputStream
nullOutputStream, write
-
Field Details
-
MAX_BLOCK_SIZE
public static final int MAX_BLOCK_SIZEThe x-snappy-framed specification allows for a chunk size up to 16,777,211 bytes in length. However, it also goes on to state:We place an additional restriction that the uncompressed data in a chunk must be no longer than 65536 bytes. This allows consumers to easily use small fixed-size buffers.- See Also:
-
DEFAULT_BLOCK_SIZE
public static final int DEFAULT_BLOCK_SIZEThe default block size to use.- See Also:
-
DEFAULT_MIN_COMPRESSION_RATIO
public static final double DEFAULT_MIN_COMPRESSION_RATIOThe default min compression ratio to use.- See Also:
-
crc32
-
headerBuffer
-
bufferPool
-
blockSize
private final int blockSize -
buffer
-
directInputBuffer
-
outputBuffer
-
minCompressionRatio
private final double minCompressionRatio -
out
-
closed
private boolean closed
-
-
Constructor Details
-
SnappyFramedOutputStream
Creates a newSnappyFramedOutputStreamusing theDEFAULT_BLOCK_SIZEandDEFAULT_MIN_COMPRESSION_RATIO.Uses
DefaultPoolFactoryto obtainBufferPoolfor buffers.- Parameters:
out- The underlyingOutputStreamto write to. Must not benull.- Throws:
IOException
-
SnappyFramedOutputStream
Creates a newSnappyFramedOutputStreamusing theDEFAULT_BLOCK_SIZEandDEFAULT_MIN_COMPRESSION_RATIO.- Parameters:
out- The underlyingOutputStreamto write to. Must not benull.bufferPool- Used to obtain buffer instances. Must not benull.- Throws:
IOException
-
SnappyFramedOutputStream
public SnappyFramedOutputStream(OutputStream out, int blockSize, double minCompressionRatio) throws IOException Creates a newSnappyFramedOutputStreaminstance.Uses
DefaultPoolFactoryto obtainBufferPoolfor buffers.- Parameters:
out- The underlyingOutputStreamto write to. Must not benull.blockSize- The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].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
-
SnappyFramedOutputStream
public SnappyFramedOutputStream(OutputStream out, int blockSize, double minCompressionRatio, BufferPool bufferPool) throws IOException Creates a newSnappyFramedOutputStreaminstance.- Parameters:
out- The underlyingOutputStreamto write to. Must not benull.blockSize- The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].minCompressionRatio- Defines the minimum compression ratio (compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].bufferPool- Used to obtain buffer instances. Must not benull.- Throws:
IOException
-
SnappyFramedOutputStream
Creates a newSnappyFramedOutputStreamusing theDEFAULT_BLOCK_SIZEandDEFAULT_MIN_COMPRESSION_RATIO.Uses
DefaultPoolFactoryto obtainBufferPoolfor buffers.- Parameters:
out- The underlyingWritableByteChannelto write to. Must not benull.- Throws:
IOException- Since:
- 1.1.1
-
SnappyFramedOutputStream
Creates a newSnappyFramedOutputStreamusing theDEFAULT_BLOCK_SIZEandDEFAULT_MIN_COMPRESSION_RATIO.Uses
DefaultPoolFactoryto obtainBufferPoolfor buffers.- Parameters:
out- The underlyingWritableByteChannelto write to. Must not benull.bufferPool- Used to obtain buffer instances. Must not benull.- Throws:
IOException
-
SnappyFramedOutputStream
public SnappyFramedOutputStream(WritableByteChannel out, int blockSize, double minCompressionRatio) throws IOException Creates a newSnappyFramedOutputStreaminstance.- Parameters:
out- The underlyingWritableByteChannelto write to. Must not benull.blockSize- The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].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- Since:
- 1.1.1
-
SnappyFramedOutputStream
public SnappyFramedOutputStream(WritableByteChannel out, int blockSize, double minCompressionRatio, BufferPool bufferPool) throws IOException Creates a newSnappyFramedOutputStreaminstance.- Parameters:
out- The underlyingWritableByteChannelto write to. Must not benull.blockSize- The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].minCompressionRatio- Defines the minimum compression ratio (compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].bufferPool- Used to obtain buffer instances. Must not benull.- Throws:
IOException- Since:
- 1.1.1
-
-
Method Details
-
writeHeader
Writes the implementation specific header or "marker bytes" to out.- Parameters:
out- The underlyingOutputStream.- Throws:
IOException
-
isOpen
-
write
- Specified by:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
write
- Specified by:
writein interfaceWritableByteChannel- Throws:
IOException
-
transferFrom
Transfers all the content from is to thisOutputStream. This potentially limits the amount of buffering required to compress content.- Parameters:
is- The source of data to compress.- Returns:
- The number of bytes read from is.
- Throws:
IOException- Since:
- 1.1.1
-
transferFrom
Transfers all the content from rbc to thisWritableByteChannel. This potentially limits the amount of buffering required to compress content.- Parameters:
rbc- The source of data to compress.- Returns:
- The number of bytes read from rbc.
- Throws:
IOException- Since:
- 1.1.1
-
flush
- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
flushBuffer
Compresses and writes out any buffered data. This does nothing if there is no currently buffered data.- Throws:
IOException
-
writeCompressed
the crc, compresses the data, determines if the compression ratio is acceptable and callsinvalid reference
SnappyFramed#maskedCrc32c(byte[], int, int)writeBlock(java.nio.channels.WritableByteChannel, java.nio.ByteBuffer, boolean, int)to actually write the frame.- Parameters:
buffer-- Throws:
IOException
-
writeBlock
private void writeBlock(WritableByteChannel out, ByteBuffer data, boolean compressed, int crc32c) throws IOException Write a frame (block) to out.- Parameters:
out- TheOutputStreamto write to.data- The data to write.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
-