public abstract class AbstractLZ77CompressorInputStream extends CompressorInputStream implements InputStreamStatistics
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()
to access the underlying stream
directly.startLiteral(long)
to
tell this class about it and read the literal data using readLiteral(byte[], int, int)
until it returns 0
. hasMoreDataInBlock()
will return false
before the next
call to readLiteral(byte[], int, int)
would return 0
.startBackReference(int, long)
to
tell this class about it and read the literal data using readBackReference(byte[], int, int)
until it returns 0
. hasMoreDataInBlock()
will return false
before the next
call to readBackReference(byte[], int, int)
would return 0
.-1
as this class' methods will never do so themselves.readOneByte()
and readLiteral(byte[], int, int)
update the counter
for bytes read.
Modifier and Type | Field and Description |
---|---|
protected ByteUtils.ByteSupplier |
supplier
Supplier that delegates to
readOneByte() . |
Constructor and Description |
---|
AbstractLZ77CompressorInputStream(InputStream is,
int windowSize)
Creates a new LZ77 input stream.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
void |
close() |
long |
getCompressedCount() |
int |
getSize()
Get the uncompressed size of the stream
|
protected boolean |
hasMoreDataInBlock()
Is there still data remaining inside the current block?
|
void |
prefill(byte[] data)
Adds some initial data to fill the window with.
|
int |
read() |
protected int |
readBackReference(byte[] b,
int off,
int len)
Reads data from the current back-reference.
|
protected int |
readLiteral(byte[] b,
int off,
int len)
Reads data from the current literal block.
|
protected int |
readOneByte()
Reads a single byte from the real input stream and ensures the data is accounted for.
|
protected void |
startBackReference(int offset,
long length)
Used by subclasses to signal the next block contains a back-reference with the given coordinates.
|
protected void |
startLiteral(long length)
Used by subclasses to signal the next block contains the given
amount of literal data.
|
count, count, getBytesRead, getCount, getUncompressedCount, pushedBackBytes
mark, markSupported, read, read, reset, skip
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getUncompressedCount
protected final ByteUtils.ByteSupplier supplier
readOneByte()
.public AbstractLZ77CompressorInputStream(InputStream is, int windowSize)
is
- An InputStream to read compressed data fromwindowSize
- Size of the window kept for back-references, must be bigger than the biggest offset expected.IllegalArgumentException
- if windowSize is not bigger than 0public int read() throws IOException
read
in class InputStream
IOException
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
public int available()
available
in class InputStream
public int getSize()
public void prefill(byte[] data)
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.
data
- the data to fill the window with.IllegalStateException
- if the stream has already started to read datapublic long getCompressedCount()
getCompressedCount
in interface InputStreamStatistics
protected final void startLiteral(long length)
length
- the length of the blockIllegalArgumentException
- if length is negativeprotected final boolean hasMoreDataInBlock()
protected final int readLiteral(byte[] b, int off, int len) throws IOException
b
- buffer to write data tooff
- offset to start writing tolen
- maximum amount of data to readIOException
- if the underlying stream throws or signals
an EOF before the amount of data promised for the block have
been readNullPointerException
- if b
is nullIndexOutOfBoundsException
- if off
is
negative, len
is negative, or len
is
greater than b.length - off
protected final void startBackReference(int offset, long length)
offset
- the offset of the back-referencelength
- the length of the back-referenceIllegalArgumentException
- if offset not bigger than 0 or
bigger than the number of bytes available for back-references
or if length is negativeprotected final int readBackReference(byte[] b, int off, int len)
b
- buffer to write data tooff
- offset to start writing tolen
- maximum amount of data to readNullPointerException
- if b
is nullIndexOutOfBoundsException
- if off
is
negative, len
is negative, or len
is
greater than b.length - off
protected final int readOneByte() throws IOException
IOException
- if the underlying stream throwsCopyright © 2002–2022 The Apache Software Foundation. All rights reserved.