Package org.xerial.snappy
Class SnappyFramedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.xerial.snappy.SnappyFramedOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
,java.nio.channels.Channel
,java.nio.channels.WritableByteChannel
public final class SnappyFramedOutputStream extends java.io.OutputStream implements java.nio.channels.WritableByteChannel
- Since:
- 1.1.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.ByteBuffer
buffer
private boolean
closed
static int
DEFAULT_BLOCK_SIZE
The default block size to use.static double
DEFAULT_MIN_COMPRESSION_RATIO
The default min compression ratio to use.private java.nio.ByteBuffer
directInputBuffer
private java.nio.ByteBuffer
headerBuffer
static int
MAX_BLOCK_SIZE
The x-snappy-framed specification allows for a chunk size up to 16,777,211 bytes in length.private double
minCompressionRatio
private java.nio.channels.WritableByteChannel
out
private java.nio.ByteBuffer
outputBuffer
-
Constructor Summary
Constructors Constructor Description SnappyFramedOutputStream(java.io.OutputStream out)
Creates a newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.SnappyFramedOutputStream(java.io.OutputStream out, int blockSize, double minCompressionRatio)
Creates a newSnappyFramedOutputStream
instance.SnappyFramedOutputStream(java.nio.channels.WritableByteChannel out)
Creates a newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.SnappyFramedOutputStream(java.nio.channels.WritableByteChannel out, int blockSize, double minCompressionRatio)
Creates a newSnappyFramedOutputStream
instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
flush()
private void
flushBuffer()
Compresses and writes out any buffered data.boolean
isOpen()
long
transferFrom(java.io.InputStream is)
Transfers all the content from is to thisOutputStream
.long
transferFrom(java.nio.channels.ReadableByteChannel rbc)
Transfers all the content from rbc to thisWritableByteChannel
.void
write(byte[] input, int offset, int length)
void
write(int b)
int
write(java.nio.ByteBuffer src)
private void
writeBlock(java.nio.channels.WritableByteChannel out, java.nio.ByteBuffer data, boolean compressed, int crc32c)
Write a frame (block) to out.private void
writeCompressed(java.nio.ByteBuffer buffer)
SnappyFramed.maskedCrc32c(byte[], int, int)
the crc, compresses the data, determines if the compression ratio is acceptable and callswriteBlock(java.nio.channels.WritableByteChannel, java.nio.ByteBuffer, boolean, int)
to actually write the frame.private void
writeHeader(java.nio.channels.WritableByteChannel out)
Writes the implementation specific header or "marker bytes" to out.
-
-
-
Field Detail
-
MAX_BLOCK_SIZE
public static final int MAX_BLOCK_SIZE
The 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:
- Constant Field Values
-
DEFAULT_BLOCK_SIZE
public static final int DEFAULT_BLOCK_SIZE
The default block size to use.- See Also:
- Constant Field Values
-
DEFAULT_MIN_COMPRESSION_RATIO
public static final double DEFAULT_MIN_COMPRESSION_RATIO
The default min compression ratio to use.- See Also:
- Constant Field Values
-
headerBuffer
private final java.nio.ByteBuffer headerBuffer
-
buffer
private final java.nio.ByteBuffer buffer
-
directInputBuffer
private final java.nio.ByteBuffer directInputBuffer
-
outputBuffer
private final java.nio.ByteBuffer outputBuffer
-
minCompressionRatio
private final double minCompressionRatio
-
out
private final java.nio.channels.WritableByteChannel out
-
closed
private boolean closed
-
-
Constructor Detail
-
SnappyFramedOutputStream
public SnappyFramedOutputStream(java.io.OutputStream out) throws java.io.IOException
Creates a newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.- Parameters:
out
- The underlyingOutputStream
to write to. Must not benull
.- Throws:
java.io.IOException
-
SnappyFramedOutputStream
public SnappyFramedOutputStream(java.io.OutputStream out, int blockSize, double minCompressionRatio) throws java.io.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].- Throws:
java.io.IOException
-
SnappyFramedOutputStream
public SnappyFramedOutputStream(java.nio.channels.WritableByteChannel out) throws java.io.IOException
Creates a newSnappyFramedOutputStream
using theDEFAULT_BLOCK_SIZE
andDEFAULT_MIN_COMPRESSION_RATIO
.- Parameters:
out
- The underlyingWritableByteChannel
to write to. Must not benull
.- Throws:
java.io.IOException
- Since:
- 1.1.1
-
SnappyFramedOutputStream
public SnappyFramedOutputStream(java.nio.channels.WritableByteChannel out, int blockSize, double minCompressionRatio) throws java.io.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:
java.io.IOException
- Since:
- 1.1.1
-
-
Method Detail
-
writeHeader
private void writeHeader(java.nio.channels.WritableByteChannel out) throws java.io.IOException
Writes the implementation specific header or "marker bytes" to out.- Parameters:
out
- The underlyingOutputStream
.- Throws:
java.io.IOException
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in interfacejava.nio.channels.Channel
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] input, int offset, int length) throws java.io.IOException
- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.WritableByteChannel
- Throws:
java.io.IOException
-
transferFrom
public long transferFrom(java.io.InputStream is) throws java.io.IOException
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:
java.io.IOException
- Since:
- 1.1.1
-
transferFrom
public long transferFrom(java.nio.channels.ReadableByteChannel rbc) throws java.io.IOException
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:
java.io.IOException
- Since:
- 1.1.1
-
flush
public final void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
close
public final void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
flushBuffer
private void flushBuffer() throws java.io.IOException
Compresses and writes out any buffered data. This does nothing if there is no currently buffered data.- Throws:
java.io.IOException
-
writeCompressed
private void writeCompressed(java.nio.ByteBuffer buffer) throws java.io.IOException
SnappyFramed.maskedCrc32c(byte[], int, int)
the crc, compresses the data, determines if the compression ratio is acceptable and callswriteBlock(java.nio.channels.WritableByteChannel, java.nio.ByteBuffer, boolean, int)
to actually write the frame.- Parameters:
buffer
-- Throws:
java.io.IOException
-
writeBlock
private void writeBlock(java.nio.channels.WritableByteChannel out, java.nio.ByteBuffer data, boolean compressed, int crc32c) throws java.io.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:
java.io.IOException
-
-