Package org.xerial.snappy
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 int
private final ByteBuffer
private final BufferPool
private boolean
private final Checksum
static final int
The default block size to use.static final double
The default min compression ratio to use.private final ByteBuffer
private final ByteBuffer
static final int
The x-snappy-framed specification allows for a chunk size up to 16,777,211 bytes in length.private final double
private final WritableByteChannel
private final ByteBuffer
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.SnappyFramedOutputStream
(OutputStream out, int blockSize, double minCompressionRatio) Creates a newSnappyFramedOutputStream
instance.SnappyFramedOutputStream
(OutputStream out, int blockSize, double minCompressionRatio, BufferPool bufferPool) Creates a newSnappyFramedOutputStream
instance.SnappyFramedOutputStream
(OutputStream out, BufferPool bufferPool) Creates a newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.Creates a newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.SnappyFramedOutputStream
(WritableByteChannel out, int blockSize, double minCompressionRatio) Creates a newSnappyFramedOutputStream
instance.SnappyFramedOutputStream
(WritableByteChannel out, int blockSize, double minCompressionRatio, BufferPool bufferPool) Creates a newSnappyFramedOutputStream
instance.SnappyFramedOutputStream
(WritableByteChannel out, BufferPool bufferPool) Creates a newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
close()
final void
flush()
private void
Compresses and writes out any buffered data.boolean
isOpen()
long
Transfers all the content from is to thisOutputStream
.long
Transfers all the content from rbc to thisWritableByteChannel
.void
write
(byte[] input, int offset, int length) void
write
(int b) int
write
(ByteBuffer src) private void
writeBlock
(WritableByteChannel out, ByteBuffer data, boolean compressed, int crc32c) Write a frame (block) to out.private void
writeCompressed
(ByteBuffer buffer) invalid reference
SnappyFramed#maskedCrc32c(byte[], int, int)
writeBlock(java.nio.channels.WritableByteChannel, java.nio.ByteBuffer, boolean, int)
to actually write the frame.private void
Writes the implementation specific header or "marker bytes" to out.Methods inherited from class java.io.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 newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.Uses
DefaultPoolFactory
to obtainBufferPool
for buffers.- Parameters:
out
- The underlyingOutputStream
to write to. Must not benull
.- Throws:
IOException
-
SnappyFramedOutputStream
Creates a newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.- Parameters:
out
- The underlyingOutputStream
to 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 newSnappyFramedOutputStream
instance.Uses
DefaultPoolFactory
to obtainBufferPool
for buffers.- Parameters:
out
- The underlyingOutputStream
to 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 newSnappyFramedOutputStream
instance.- Parameters:
out
- The underlyingOutputStream
to 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 newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.Uses
DefaultPoolFactory
to obtainBufferPool
for buffers.- Parameters:
out
- The underlyingWritableByteChannel
to write to. Must not benull
.- Throws:
IOException
- Since:
- 1.1.1
-
SnappyFramedOutputStream
Creates a newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.Uses
DefaultPoolFactory
to obtainBufferPool
for buffers.- Parameters:
out
- The underlyingWritableByteChannel
to 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 newSnappyFramedOutputStream
instance.- Parameters:
out
- The underlyingWritableByteChannel
to 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 newSnappyFramedOutputStream
instance.- Parameters:
out
- The underlyingWritableByteChannel
to 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
public boolean isOpen() -
write
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
- Specified by:
write
in 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:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceChannel
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
flushBuffer
Compresses and writes out any buffered data. This does nothing if there is no currently buffered data.- Throws:
IOException
-
writeCompressed
invalid 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
- TheOutputStream
to 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
-