Top Description Fields Constructors Methods
org.xerial.snappy

public final Class SnappyFramedOutputStream

extends OutputStream
implements WritableByteChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.WritableByteChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable
Static Imports
org.xerial.snappy.SnappyFramed.COMPRESSED_DATA_FLAG, .SnappyFramed.HEADER_BYTES, .SnappyFramed.UNCOMPRESSED_DATA_FLAG, .SnappyFramed.maskedCrc32c

Implements the x-snappy-framed as an OutputStream and WritableByteChannel.
Author
Brett Okken
Since
1.1.0

Field Summary

Modifier and TypeField and Description
private final int
private final ByteBuffer
private final BufferPool
private boolean
private final Checksum
public static final int
DEFAULT_BLOCK_SIZE

The default block size to use.

public static final double
DEFAULT_MIN_COMPRESSION_RATIO

The default min compression ratio to use.

private final ByteBuffer
private final ByteBuffer
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.

private final double
private final WritableByteChannel
private final ByteBuffer

Constructor Summary

AccessConstructor and Description
public
SnappyFramedOutputStream(OutputStream
The underlying OutputStream to write to. Must not be null.
out
)

Creates a new SnappyFramedOutputStream using the DEFAULT_BLOCK_SIZE and DEFAULT_MIN_COMPRESSION_RATIO.

public
SnappyFramedOutputStream(OutputStream
The underlying OutputStream to write to. Must not be null.
out
,
BufferPool
Used to obtain buffer instances. Must not be null.
bufferPool
)

Creates a new SnappyFramedOutputStream using the DEFAULT_BLOCK_SIZE and DEFAULT_MIN_COMPRESSION_RATIO.

public
SnappyFramedOutputStream(OutputStream
The underlying OutputStream to write to. Must not be null.
out
,
int
The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].
blockSize
,
double
Defines the minimum compression ratio ( compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].
minCompressionRatio
)

Creates a new SnappyFramedOutputStream instance.

public
SnappyFramedOutputStream(OutputStream
The underlying OutputStream to write to. Must not be null.
out
,
int
The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].
blockSize
,
double
Defines the minimum compression ratio ( compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].
minCompressionRatio
,
BufferPool
Used to obtain buffer instances. Must not be null.
bufferPool
)

Creates a new SnappyFramedOutputStream instance.

public
public
SnappyFramedOutputStream(WritableByteChannel
The underlying WritableByteChannel to write to. Must not be null.
out
,
BufferPool
Used to obtain buffer instances. Must not be null.
bufferPool
)

Creates a new SnappyFramedOutputStream using the DEFAULT_BLOCK_SIZE and DEFAULT_MIN_COMPRESSION_RATIO.

public
SnappyFramedOutputStream(WritableByteChannel
The underlying WritableByteChannel to write to. Must not be null.
out
,
int
The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].
blockSize
,
double
Defines the minimum compression ratio ( compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].
minCompressionRatio
)

Creates a new SnappyFramedOutputStream instance.

public
SnappyFramedOutputStream(WritableByteChannel
The underlying WritableByteChannel to write to. Must not be null.
out
,
int
The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].
blockSize
,
double
Defines the minimum compression ratio ( compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].
minCompressionRatio
,
BufferPool
Used to obtain buffer instances. Must not be null.
bufferPool
)

Creates a new SnappyFramedOutputStream instance.

Method Summary

Modifier and TypeMethod and Description
public final void
public final void
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this output stream and forces any buffered output bytes to be written out.

private void
flushBuffer()

Compresses and writes out any buffered data.

public boolean
isOpen()

Implements java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

public long

Returns:

The number of bytes read from is.
transferFrom
(InputStream
The source of data to compress.
is
)

Transfers all the content from is to this OutputStream.

public long

Returns:

The number of bytes read from rbc.
transferFrom
(ReadableByteChannel
The source of data to compress.
rbc
)

Transfers all the content from rbc to this WritableByteChannel.

public void
write(int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.

public void
write(byte[]
the data.
input
,
int
the start offset in the data.
offset
,
int
the number of bytes to write.
length
)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

public int
write(ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Implements java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

private void
writeBlock(final WritableByteChannel
The OutputStream to write to.
out
,
ByteBuffer
The data to write.
data
,
boolean
Indicates if data is the compressed or raw content. This is based on whether the compression ratio desired is reached.
compressed
,
int
The calculated checksum.
crc32c
)

Write a frame (block) to out.

private void
writeCompressed(ByteBuffer buffer)

SnappyFramed#maskedCrc32c(byte[], int, int) the crc, compresses the data, determines if the compression ratio is acceptable and calls writeBlock(java.nio.channels.WritableByteChannel, java.nio.ByteBuffer, boolean, int) to actually write the frame.

private void
writeHeader(WritableByteChannel
The underlying OutputStream.
out
)

Writes the implementation specific header or "marker bytes" to out.

Inherited from java.io.OutputStream:
nullOutputStreamwrite

Field Detail

blockSizeback to summary
private final int blockSize
bufferback to summary
private final ByteBuffer buffer
bufferPoolback to summary
private final BufferPool bufferPool
closedback to summary
private boolean closed
crc32back to summary
private final Checksum crc32
DEFAULT_BLOCK_SIZEback to summary
public static final int DEFAULT_BLOCK_SIZE

The default block size to use.

DEFAULT_MIN_COMPRESSION_RATIOback to summary
public static final double DEFAULT_MIN_COMPRESSION_RATIO

The default min compression ratio to use.

directInputBufferback to summary
private final ByteBuffer directInputBuffer
headerBufferback to summary
private final ByteBuffer headerBuffer
MAX_BLOCK_SIZEback to summary
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.

minCompressionRatioback to summary
private final double minCompressionRatio
outback to summary
private final WritableByteChannel out
outputBufferback to summary
private final ByteBuffer outputBuffer

Constructor Detail

SnappyFramedOutputStreamback to summary
public SnappyFramedOutputStream(OutputStream out) throws IOException

Creates a new SnappyFramedOutputStream using the DEFAULT_BLOCK_SIZE and DEFAULT_MIN_COMPRESSION_RATIO.

Uses DefaultPoolFactory to obtain BufferPool for buffers.

Parameters
out:OutputStream

The underlying OutputStream to write to. Must not be null.

SnappyFramedOutputStreamback to summary
public SnappyFramedOutputStream(OutputStream out, BufferPool bufferPool) throws IOException

Creates a new SnappyFramedOutputStream using the DEFAULT_BLOCK_SIZE and DEFAULT_MIN_COMPRESSION_RATIO.

Parameters
out:OutputStream

The underlying OutputStream to write to. Must not be null.

bufferPool:BufferPool

Used to obtain buffer instances. Must not be null.

SnappyFramedOutputStreamback to summary
public SnappyFramedOutputStream(OutputStream out, int blockSize, double minCompressionRatio) throws IOException

Creates a new SnappyFramedOutputStream instance.

Uses DefaultPoolFactory to obtain BufferPool for buffers.

Parameters
out:OutputStream

The underlying OutputStream to write to. Must not be null.

blockSize:int

The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].

minCompressionRatio:double

Defines the minimum compression ratio ( compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].

SnappyFramedOutputStreamback to summary
public SnappyFramedOutputStream(OutputStream out, int blockSize, double minCompressionRatio, BufferPool bufferPool) throws IOException

Creates a new SnappyFramedOutputStream instance.

Parameters
out:OutputStream

The underlying OutputStream to write to. Must not be null.

blockSize:int

The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].

minCompressionRatio:double

Defines the minimum compression ratio ( compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].

bufferPool:BufferPool

Used to obtain buffer instances. Must not be null.

SnappyFramedOutputStreamback to summary
public SnappyFramedOutputStream(WritableByteChannel out) throws IOException

Creates a new SnappyFramedOutputStream using the DEFAULT_BLOCK_SIZE and DEFAULT_MIN_COMPRESSION_RATIO.

Uses DefaultPoolFactory to obtain BufferPool for buffers.

Parameters
out:WritableByteChannel

The underlying WritableByteChannel to write to. Must not be null.

Since
1.1.1
SnappyFramedOutputStreamback to summary
public SnappyFramedOutputStream(WritableByteChannel out, BufferPool bufferPool) throws IOException

Creates a new SnappyFramedOutputStream using the DEFAULT_BLOCK_SIZE and DEFAULT_MIN_COMPRESSION_RATIO.

Uses DefaultPoolFactory to obtain BufferPool for buffers.

Parameters
out:WritableByteChannel

The underlying WritableByteChannel to write to. Must not be null.

bufferPool:BufferPool

Used to obtain buffer instances. Must not be null.

SnappyFramedOutputStreamback to summary
public SnappyFramedOutputStream(WritableByteChannel out, int blockSize, double minCompressionRatio) throws IOException

Creates a new SnappyFramedOutputStream instance.

Parameters
out:WritableByteChannel

The underlying WritableByteChannel to write to. Must not be null.

blockSize:int

The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].

minCompressionRatio:double

Defines the minimum compression ratio ( compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].

Since
1.1.1
SnappyFramedOutputStreamback to summary
public SnappyFramedOutputStream(WritableByteChannel out, int blockSize, double minCompressionRatio, BufferPool bufferPool) throws IOException

Creates a new SnappyFramedOutputStream instance.

Parameters
out:WritableByteChannel

The underlying WritableByteChannel to write to. Must not be null.

blockSize:int

The block size (of raw data) to compress before writing frames to out. Must be in (0, 65536].

minCompressionRatio:double

Defines the minimum compression ratio ( compressedLength / rawLength) that must be achieved to write the compressed data. This must be in (0, 1.0].

bufferPool:BufferPool

Used to obtain buffer instances. Must not be null.

Since
1.1.1

Method Detail

closeback to summary
public final void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.nio.channels.Channel.close, java.io.Closeable.close.

Doc from java.nio.channels.Channel.close.

Closes this channel.

After a channel is closed, any further attempt to invoke I/O operations upon it will cause a ClosedChannelException to be thrown.

If this channel is already closed then invoking this method has no effect.

This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
flushback to summary
public final void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.OutputStream.flush.

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
flushBufferback to summary
private void flushBuffer() throws IOException

Compresses and writes out any buffered data. This does nothing if there is no currently buffered data.

isOpenback to summary
public boolean isOpen()

Implements java.nio.channels.Channel.isOpen.

Doc from java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

Returns:boolean

true if, and only if, this channel is open

Annotations
@Override
transferFromback to summary
public long transferFrom(InputStream is) throws IOException

Transfers all the content from is to this OutputStream. This potentially limits the amount of buffering required to compress content.

Parameters
is:InputStream

The source of data to compress.

Returns:long

The number of bytes read from is.

Since
1.1.1
transferFromback to summary
public long transferFrom(ReadableByteChannel rbc) throws IOException

Transfers all the content from rbc to this WritableByteChannel. This potentially limits the amount of buffering required to compress content.

Parameters
rbc:ReadableByteChannel

The source of data to compress.

Returns:long

The number of bytes read from rbc.

Since
1.1.1
writeback to summary
public void write(int b) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters
b:int

the byte.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeback to summary
public void write(byte[] input, int offset, int length) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters
input:byte[]

the data.

offset:int

the start offset in the data.

length:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.
writeback to summary
public int write(ByteBuffer src) throws IOException

Implements java.nio.channels.WritableByteChannel.write.

Doc from java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

An attempt is made to write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, src.remaining(), at the moment this method is invoked.

Suppose that a byte sequence of length n is written, where 0 <= n <= r. This byte sequence will be transferred from the buffer starting at index p, where p is the buffer's position at the moment this method is invoked; the index of the last byte written will be p + n - 1. Upon return the buffer's position will be equal to p + n; its limit will not have changed.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.

This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters
src:ByteBuffer

The buffer from which bytes are to be retrieved

Returns:int

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
writeBlockback to summary
private void writeBlock(final WritableByteChannel out, ByteBuffer data, boolean compressed, int crc32c) throws IOException

Write a frame (block) to out.

Parameters
out:WritableByteChannel

The OutputStream to write to.

data:ByteBuffer

The data to write.

compressed:boolean

Indicates if data is the compressed or raw content. This is based on whether the compression ratio desired is reached.

crc32c:int

The calculated checksum.

writeCompressedback to summary
private void writeCompressed(ByteBuffer buffer) throws IOException

SnappyFramed#maskedCrc32c(byte[], int, int) the crc, compresses the data, determines if the compression ratio is acceptable and calls writeBlock(java.nio.channels.WritableByteChannel, java.nio.ByteBuffer, boolean, int) to actually write the frame.

writeHeaderback to summary
private void writeHeader(WritableByteChannel out) throws IOException

Writes the implementation specific header or "marker bytes" to out.

Parameters
out:WritableByteChannel

The underlying OutputStream.