Top Description Inners Fields Constructors Methods
org.apache.commons.compress.compressors.snappy

public Class SnappyCompressorInputStream

extends AbstractLZ77CompressorInputStream
Class Inheritance
Imports
java.io.IOException, .InputStream, org.apache.commons.compress.compressors.lz77support.AbstractLZ77CompressorInputStream, org.apache.commons.compress.utils.ByteUtils

CompressorInputStream for the raw Snappy format.

This implementation uses an internal buffer in order to handle the back-references that are at the heart of the LZ77 algorithm. The size of the buffer must be at least as big as the biggest offset used in the compressed stream. The current version of the Snappy algorithm as defined by Google works on 32k blocks and doesn't contain offsets bigger than 32k which is the default block size used by this class.

Since
1.7
See Also
Snappy compressed format description

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum

Field Summary

Modifier and TypeField and Description
public static final int
DEFAULT_BLOCK_SIZE

Default block size

private boolean
private final int
private SnappyCompressorInputStream.State
state

Current state of the stream

private static final int
TAG_MASK

Mask used to determine the type of "tag" is being processed

private int
uncompressedBytesRemaining

Number of uncompressed bytes still to be read.

Inherited from org.apache.commons.compress.compressors.lz77support.AbstractLZ77CompressorInputStream:
supplier

Constructor Summary

AccessConstructor and Description
public
SnappyCompressorInputStream(final InputStream
An InputStream to read compressed data from
is
)

Constructor using the default buffer size of 32k.

public
SnappyCompressorInputStream(final InputStream
An InputStream to read compressed data from
is
,
final int
The block size used in compression
blockSize
)

Constructor using a configurable buffer size.

Method Summary

Modifier and TypeMethod and Description
private void
fill()

Try to fill the buffer with the next block of data.

public int

Returns:

the uncompressed size
getSize
()

Overrides org.apache.commons.compress.compressors.lz77support.AbstractLZ77CompressorInputStream.getSize.

Gets the uncompressed size of the stream
public int
read(final byte[]
the buffer into which the data is read.
b
,
final int
the start offset in array b at which the data is written.
off
,
final int
the maximum number of bytes to read.
len
)

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes.
private int
readLiteralLength(final int b)

private long

Returns:

The size of the uncompressed data
readSize
()

The stream starts with the uncompressed length (up to a maximum of 2^32 - 1), stored as a little-endian varint.

Inherited from org.apache.commons.compress.compressors.lz77support.AbstractLZ77CompressorInputStream:
availableclosegetCompressedCounthasMoreDataInBlockprefillreadreadBackReferencereadLiteralreadOneBytestartBackReferencestartLiteral

Field Detail

DEFAULT_BLOCK_SIZEback to summary
public static final int DEFAULT_BLOCK_SIZE

Default block size

endReachedback to summary
private boolean endReached
sizeback to summary
private final int size

Hides org.apache.commons.compress.compressors.lz77support.AbstractLZ77CompressorInputStream.size.

The size of the uncompressed data

stateback to summary
private SnappyCompressorInputStream.State state

Current state of the stream

TAG_MASKback to summary
private static final int TAG_MASK

Mask used to determine the type of "tag" is being processed

uncompressedBytesRemainingback to summary
private int uncompressedBytesRemaining

Number of uncompressed bytes still to be read.

Constructor Detail

SnappyCompressorInputStreamback to summary
public SnappyCompressorInputStream(final InputStream is) throws IOException

Constructor using the default buffer size of 32k.

Parameters
is:InputStream

An InputStream to read compressed data from

Exceptions
IOException:
if reading fails
SnappyCompressorInputStreamback to summary
public SnappyCompressorInputStream(final InputStream is, final int blockSize) throws IOException

Constructor using a configurable buffer size.

Parameters
is:InputStream

An InputStream to read compressed data from

blockSize:int

The block size used in compression

Exceptions
IOException:
if reading fails
IllegalArgumentException:
if blockSize is not bigger than 0

Method Detail

fillback to summary
private void fill() throws IOException

Try to fill the buffer with the next block of data.

getSizeback to summary
public int getSize()

Overrides org.apache.commons.compress.compressors.lz77support.AbstractLZ77CompressorInputStream.getSize.

Gets the uncompressed size of the stream

Returns:int

the uncompressed size

Annotations
@Override
readback to summary
public int read(final byte[] b, final int off, final int len) throws IOException

Overrides java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off-1] and elements b[off+len] through b[b.length-1] are unaffected.

Parameters
b:byte[]

the buffer into which the data is read.

off:int

the start offset in array b at which the data is written.

len:int

the maximum number of bytes to read.

Returns:int

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Annotations
@Override
Exceptions
IOException:
If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
readLiteralLengthback to summary
private int readLiteralLength(final int b) throws IOException
readSizeback to summary
private long readSize() throws IOException

The stream starts with the uncompressed length (up to a maximum of 2^32 - 1), stored as a little-endian varint. Varints consist of a series of bytes, where the lower 7 bits are data and the upper bit is set iff there are more bytes to be read. In other words, an uncompressed length of 64 would be stored as 0x40, and an uncompressed length of 2097150 (0x1FFFFE) would be stored as 0xFE 0xFF 0x7F.

Returns:long

The size of the uncompressed data

Exceptions
IOException:
Could not read a byte
org.apache.commons.compress.compressors.snappy back to summary

private final Enum SnappyCompressorInputStream.State

extends Enum<SnappyCompressorInputStream.State>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final SnappyCompressorInputStream.State
public static final SnappyCompressorInputStream.State
public static final SnappyCompressorInputStream.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static SnappyCompressorInputStream.State
public static SnappyCompressorInputStream.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

IN_BACK_REFERENCEback to summary
public static final SnappyCompressorInputStream.State IN_BACK_REFERENCE
IN_LITERALback to summary
public static final SnappyCompressorInputStream.State IN_LITERAL
NO_BLOCKback to summary
public static final SnappyCompressorInputStream.State NO_BLOCK

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static SnappyCompressorInputStream.State valueOf(String name)
valuesback to summary
public static SnappyCompressorInputStream.State[] values()