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

public Class FramedSnappyCompressorInputStream

extends CompressorInputStream
implements InputStreamStatistics
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.utils.InputStreamStatistics
Imports
java.io.IOException, .InputStream, .PushbackInputStream, java.util.Arrays, org.apache.commons.codec.digest.PureJavaCrc32C, org.apache.commons.compress.compressors.CompressorInputStream, org.apache.commons.compress.utils.BoundedInputStream, .ByteUtils, .IOUtils, .InputStreamStatistics, org.apache.commons.io.input.CountingInputStream

CompressorInputStream for the framing Snappy format.

Based on the "spec" in the version "Last revised: 2013-10-25"

Since
1.7
See Also
Snappy framing format description

Field Summary

Modifier and TypeField and Description
private final int
private final PureJavaCrc32C
pack-priv static final int
private final CountingInputStream
private SnappyCompressorInputStream
private final FramedSnappyDialect
dialect

The dialect to expect

private boolean
private long
private final PushbackInputStream
inputStream

The underlying stream to read compressed data from

private boolean
pack-priv static final long
MASK_OFFSET

package private for tests only.

private static final int
private static final int
private static final int
private final byte[]
private static final int
private static final int
private final ByteUtils.ByteSupplier
pack-priv static final byte[]
private static final int
private int
private long

Constructor Summary

AccessConstructor and Description
public
FramedSnappyCompressorInputStream(final InputStream
the InputStream from which to read the compressed data
in
)

Constructs a new input stream that decompresses snappy-framed-compressed data from the specified input stream using the FramedSnappyDialect#STANDARD dialect.

public
FramedSnappyCompressorInputStream(final InputStream
the InputStream from which to read the compressed data
in
,
final FramedSnappyDialect
the dialect used by the compressed stream
dialect
)

Constructs a new input stream that decompresses snappy-framed-compressed data from the specified input stream.

public
FramedSnappyCompressorInputStream(final InputStream
the InputStream from which to read the compressed data
in
,
final int
the block size to use for the compressed stream
blockSize
,
final FramedSnappyDialect
the dialect used by the compressed stream
dialect
)

Constructs a new input stream that decompresses snappy-framed-compressed data from the specified input stream.

Method Summary

Modifier and TypeMethod and Description
public int
available()

Overrides java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.
public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.
public long
getCompressedCount()

Implements org.apache.commons.compress.utils.InputStreamStatistics.getCompressedCount.

Gets the amount of raw or compressed bytes read by the stream.
public static boolean

Returns:

true if this is a .sz stream, false otherwise
matches
(final byte[]
the bytes to check
signature
,
final int
the number of bytes to check
length
)

Checks if the signature matches what is expected for a .sz file.

public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input 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 long
private void
private int

Returns:

-1 if there is no current chunk or the number of bytes read from the current chunk (which may be -1 if the end of the chunk is reached).
readOnce
(final byte[] b, final int off, final int len)

Read from the current chunk into the given array.

private int
private int
private void
private void
pack-priv static long
unmask(long x)

private void
Inherited from org.apache.commons.compress.compressors.CompressorInputStream:
countcountgetBytesReadgetCountgetUncompressedCountpushedBackBytes

Field Detail

blockSizeback to summary
private final int blockSize
checksumback to summary
private final PureJavaCrc32C checksum
COMPRESSED_CHUNK_TYPEback to summary
pack-priv static final int COMPRESSED_CHUNK_TYPE
countingStreamback to summary
private final CountingInputStream countingStream
currentCompressedChunkback to summary
private SnappyCompressorInputStream currentCompressedChunk
dialectback to summary
private final FramedSnappyDialect dialect

The dialect to expect

endReachedback to summary
private boolean endReached
expectedChecksumback to summary
private long expectedChecksum
inputStreamback to summary
private final PushbackInputStream inputStream

The underlying stream to read compressed data from

inUncompressedChunkback to summary
private boolean inUncompressedChunk
MASK_OFFSETback to summary
pack-priv static final long MASK_OFFSET

package private for tests only.

MAX_SKIPPABLE_TYPEback to summary
private static final int MAX_SKIPPABLE_TYPE
MAX_UNSKIPPABLE_TYPEback to summary
private static final int MAX_UNSKIPPABLE_TYPE
MIN_UNSKIPPABLE_TYPEback to summary
private static final int MIN_UNSKIPPABLE_TYPE
oneByteback to summary
private final byte[] oneByte
PADDING_CHUNK_TYPEback to summary
private static final int PADDING_CHUNK_TYPE
STREAM_IDENTIFIER_TYPEback to summary
private static final int STREAM_IDENTIFIER_TYPE
supplierback to summary
private final ByteUtils.ByteSupplier supplier
SZ_SIGNATUREback to summary
pack-priv static final byte[] SZ_SIGNATURE
UNCOMPRESSED_CHUNK_TYPEback to summary
private static final int UNCOMPRESSED_CHUNK_TYPE
uncompressedBytesRemainingback to summary
private int uncompressedBytesRemaining
unreadBytesback to summary
private long unreadBytes

Constructor Detail

FramedSnappyCompressorInputStreamback to summary
public FramedSnappyCompressorInputStream(final InputStream in) throws IOException

Constructs a new input stream that decompresses snappy-framed-compressed data from the specified input stream using the FramedSnappyDialect#STANDARD dialect.

Parameters
in:InputStream

the InputStream from which to read the compressed data

Exceptions
IOException:
if reading fails
FramedSnappyCompressorInputStreamback to summary
public FramedSnappyCompressorInputStream(final InputStream in, final FramedSnappyDialect dialect) throws IOException

Constructs a new input stream that decompresses snappy-framed-compressed data from the specified input stream.

Parameters
in:InputStream

the InputStream from which to read the compressed data

dialect:FramedSnappyDialect

the dialect used by the compressed stream

Exceptions
IOException:
if reading fails
FramedSnappyCompressorInputStreamback to summary
public FramedSnappyCompressorInputStream(final InputStream in, final int blockSize, final FramedSnappyDialect dialect) throws IOException

Constructs a new input stream that decompresses snappy-framed-compressed data from the specified input stream.

Parameters
in:InputStream

the InputStream from which to read the compressed data

blockSize:int

the block size to use for the compressed stream

dialect:FramedSnappyDialect

the dialect used by the compressed stream

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

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.InputStream.available.

Doc from java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. The read might be on the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

A subclass's implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.

Returns:int

an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.InputStream.close.

Closes this input stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
getCompressedCountback to summary
public long getCompressedCount()

Implements org.apache.commons.compress.utils.InputStreamStatistics.getCompressedCount.

Doc from org.apache.commons.compress.utils.InputStreamStatistics.getCompressedCount.

Gets the amount of raw or compressed bytes read by the stream.

Returns:long

the amount of raw or compressed bytes read by the stream.

Annotations
@Override
Since
1.17
matchesback to summary
public static boolean matches(final byte[] signature, final int length)

Checks if the signature matches what is expected for a .sz file.

.sz files start with a chunk with tag 0xff and content sNaPpY.

Parameters
signature:byte[]

the bytes to check

length:int

the number of bytes to check

Returns:boolean

true if this is a .sz stream, false otherwise

readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:int

the next byte of data, or -1 if the end of the stream is reached.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
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.
readCrcback to summary
private long readCrc() throws IOException
readNextBlockback to summary
private void readNextBlock() throws IOException
readOnceback to summary
private int readOnce(final byte[] b, final int off, final int len) throws IOException

Read from the current chunk into the given array.

Returns:int

-1 if there is no current chunk or the number of bytes read from the current chunk (which may be -1 if the end of the chunk is reached).

readOneByteback to summary
private int readOneByte() throws IOException
readSizeback to summary
private int readSize() throws IOException
readStreamIdentifierback to summary
private void readStreamIdentifier() throws IOException
skipBlockback to summary
private void skipBlock() throws IOException
unmaskback to summary
pack-priv static long unmask(long x)
verifyLastChecksumAndResetback to summary
private void verifyLastChecksumAndReset() throws IOException