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

public abstract Class AbstractLZ77CompressorInputStream

extends CompressorInputStream
implements InputStreamStatistics
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.utils.InputStreamStatistics
Known Direct Subclasses
org.apache.commons.compress.compressors.snappy.SnappyCompressorInputStream, org.apache.commons.compress.compressors.lz4.BlockLZ4CompressorInputStream
Imports
java.io.IOException, .InputStream, java.util.Arrays, org.apache.commons.compress.compressors.CompressorInputStream, org.apache.commons.compress.utils.ByteUtils, .IOUtils, .InputStreamStatistics, org.apache.commons.io.input.CountingInputStream

Encapsulates code common to LZ77 decompressors.

Assumes the stream consists of blocks of literal data and back-references (called copies) in any order. Of course the first block must be a literal block for the scheme to work - unless the prefill method has been used to provide initial data that is never returned by read but only used for back-references.

Subclasses must override the three-arg read method as the no-arg version delegates to it and the default implementation delegates to the no-arg version, leading to infinite mutual recursion and a StackOverflowError otherwise.

The contract for subclasses' read implementation is:

readOneByte and readLiteral update the counter for bytes read.

Since
1.14

Field Summary

Modifier and TypeField and Description
private int
backReferenceOffset

Offset of the current back-reference.

private final byte[]
buf

Buffer to write decompressed bytes to for back-references, will be three times windowSize big.

private long
bytesRemaining

Number of bytes still to be read from the current literal or back-reference.

private final CountingInputStream
in

The underlying stream to read compressed data from

private final byte[]
private int
readIndex

Index of the next byte to be read.

private int
size

Uncompressed size

protected final ByteUtils.ByteSupplier
supplier

Supplier that delegates to readOneByte.

private final int
windowSize

Size of the window - must be bigger than the biggest offset expected.

private int
writeIndex

One behind the index of the last byte in the buffer that was written, i.e. the next position to write to

Constructor Summary

AccessConstructor and Description
public
AbstractLZ77CompressorInputStream(final InputStream
An InputStream to read compressed data from
is
,
final int
Size of the window kept for back-references, must be bigger than the biggest offset expected.
windowSize
)

Creates a new LZ77 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 int

Returns:

the uncompressed size
getSize
()

Gets the uncompressed size of the stream

protected final boolean

Returns:

true if there is still data remaining inside the current block.
hasMoreDataInBlock
()

Is there still data remaining inside the current block?

public void
prefill(final byte[]
the data to fill the window with.
data
)

Adds some initial data to fill the window with.

public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input stream.
protected final int

Returns:

number of bytes read, may be 0. Will never return -1 as EOF-detection is the responsibility of the subclass
readBackReference
(final byte[]
buffer to write data to
b
,
final int
offset to start writing to
off
,
final int
maximum amount of data to read
len
)

Reads data from the current back-reference.

private int
readFromBuffer(final byte[] b, final int off, final int len)

protected final int

Returns:

number of bytes read, may be 0. Will never return -1 as EOF-detection is the responsibility of the subclass
readLiteral
(final byte[]
buffer to write data to
b
,
final int
offset to start writing to
off
,
final int
maximum amount of data to read
len
)

Reads data from the current literal block.

protected final int

Returns:

the byte read as value between 0 and 255 or -1 if EOF has been reached.
readOneByte
()

Reads a single byte from the real input stream and ensures the data is accounted for.

private void
protected final void
startBackReference(final int
the offset of the back-reference
offset
,
final long
the length of the back-reference
length
)

Used by subclasses to signal the next block contains a back-reference with the given coordinates.

protected final void
startLiteral(final long
the length of the block
length
)

Used by subclasses to signal the next block contains the given amount of literal data.

private void
tryToCopy(final int bytesToCopy)

private void
tryToReadLiteral(final int bytesToRead)

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

Field Detail

backReferenceOffsetback to summary
private int backReferenceOffset

Offset of the current back-reference.

bufback to summary
private final byte[] buf

Buffer to write decompressed bytes to for back-references, will be three times windowSize big.

Three times so we can slide the whole buffer a windowSize to the left once we've read twice windowSize and still have enough data inside of it to satisfy back-references.

bytesRemainingback to summary
private long bytesRemaining

Number of bytes still to be read from the current literal or back-reference.

inback to summary
private final CountingInputStream in

The underlying stream to read compressed data from

oneByteback to summary
private final byte[] oneByte
readIndexback to summary
private int readIndex

Index of the next byte to be read.

sizeback to summary
private int size

Uncompressed size

supplierback to summary
protected final ByteUtils.ByteSupplier supplier

Supplier that delegates to readOneByte.

windowSizeback to summary
private final int windowSize

Size of the window - must be bigger than the biggest offset expected.

writeIndexback to summary
private int writeIndex

One behind the index of the last byte in the buffer that was written, i.e. the next position to write to

Constructor Detail

AbstractLZ77CompressorInputStreamback to summary
public AbstractLZ77CompressorInputStream(final InputStream is, final int windowSize)

Creates a new LZ77 input stream.

Parameters
is:InputStream

An InputStream to read compressed data from

windowSize:int

Size of the window kept for back-references, must be bigger than the biggest offset expected.

Exceptions
IllegalArgumentException:
if windowSize is not bigger than 0

Method Detail

availableback to summary
public int available()

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
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
getSizeback to summary
public int getSize()

Gets the uncompressed size of the stream

Returns:int

the uncompressed size

hasMoreDataInBlockback to summary
protected final boolean hasMoreDataInBlock()

Is there still data remaining inside the current block?

Returns:boolean

true if there is still data remaining inside the current block.

prefillback to summary
public void prefill(final byte[] data)

Adds some initial data to fill the window with.

This is used if the stream has been cut into blocks and back-references of one block may refer to data of the previous block(s). One such example is the LZ4 frame format using block dependency.

Parameters
data:byte[]

the data to fill the window with.

Exceptions
IllegalStateException:
if the stream has already started to read data
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.
readBackReferenceback to summary
protected final int readBackReference(final byte[] b, final int off, final int len)

Reads data from the current back-reference.

Parameters
b:byte[]

buffer to write data to

off:int

offset to start writing to

len:int

maximum amount of data to read

Returns:int

number of bytes read, may be 0. Will never return -1 as EOF-detection is the responsibility of the subclass

Exceptions
NullPointerException:
if b is null
IndexOutOfBoundsException:
if off is negative, len is negative, or len is greater than b.length - off
readFromBufferback to summary
private int readFromBuffer(final byte[] b, final int off, final int len)
readLiteralback to summary
protected final int readLiteral(final byte[] b, final int off, final int len) throws IOException

Reads data from the current literal block.

Parameters
b:byte[]

buffer to write data to

off:int

offset to start writing to

len:int

maximum amount of data to read

Returns:int

number of bytes read, may be 0. Will never return -1 as EOF-detection is the responsibility of the subclass

Exceptions
IOException:
if the underlying stream throws or signals an EOF before the amount of data promised for the block have been read
NullPointerException:
if b is null
IndexOutOfBoundsException:
if off is negative, len is negative, or len is greater than b.length - off
readOneByteback to summary
protected final int readOneByte() throws IOException

Reads a single byte from the real input stream and ensures the data is accounted for.

Returns:int

the byte read as value between 0 and 255 or -1 if EOF has been reached.

Exceptions
IOException:
if the underlying stream throws
slideBufferback to summary
private void slideBuffer()
startBackReferenceback to summary
protected final void startBackReference(final int offset, final long length)

Used by subclasses to signal the next block contains a back-reference with the given coordinates.

Parameters
offset:int

the offset of the back-reference

length:long

the length of the back-reference

Exceptions
IllegalArgumentException:
if offset not bigger than 0 or bigger than the number of bytes available for back-references or if length is negative
startLiteralback to summary
protected final void startLiteral(final long length)

Used by subclasses to signal the next block contains the given amount of literal data.

Parameters
length:long

the length of the block

Exceptions
IllegalArgumentException:
if length is negative
tryToCopyback to summary
private void tryToCopy(final int bytesToCopy)
tryToReadLiteralback to summary
private void tryToReadLiteral(final int bytesToRead) throws IOException