Class AbstractLZ77CompressorInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.commons.compress.compressors.CompressorInputStream
-
- org.apache.commons.compress.compressors.lz77support.AbstractLZ77CompressorInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,InputStreamStatistics
- Direct Known Subclasses:
BlockLZ4CompressorInputStream
,SnappyCompressorInputStream
public abstract class AbstractLZ77CompressorInputStream extends CompressorInputStream implements InputStreamStatistics
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 byread
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 aStackOverflowError
otherwise.The contract for subclasses'
read
implementation is:- keep track of the current state of the stream. Is it inside a literal block or a back-reference or in-between blocks?
- Use
readOneByte()
to access the underlying stream directly. - If a new literal block starts, use
startLiteral(long)
to tell this class about it and read the literal data usingreadLiteral(byte[], int, int)
until it returns0
.hasMoreDataInBlock()
will returnfalse
before the next call toreadLiteral(byte[], int, int)
would return0
. - If a new back-reference starts, use
startBackReference(int, long)
to tell this class about it and read the literal data usingreadBackReference(byte[], int, int)
until it returns0
.hasMoreDataInBlock()
will returnfalse
before the next call toreadBackReference(byte[], int, int)
would return0
. - If the end of the stream has been reached, return
-1
as this class' methods will never do so themselves.
readOneByte()
andreadLiteral(byte[], int, int)
update the counter for bytes read.- Since:
- 1.14
-
-
Constructor Summary
Constructors Constructor Description AbstractLZ77CompressorInputStream(java.io.InputStream is, int windowSize)
Creates a new LZ77 input stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
void
close()
long
getCompressedCount()
int
getSize()
Get the uncompressed size of the streamvoid
prefill(byte[] data)
Adds some initial data to fill the window with.int
read()
-
Methods inherited from class org.apache.commons.compress.compressors.CompressorInputStream
getBytesRead, getCount, getUncompressedCount
-
Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.compress.utils.InputStreamStatistics
getUncompressedCount
-
-
-
-
Constructor Detail
-
AbstractLZ77CompressorInputStream
public AbstractLZ77CompressorInputStream(java.io.InputStream is, int windowSize) throws java.io.IOException
Creates a new LZ77 input stream.- Parameters:
is
- An InputStream to read compressed data fromwindowSize
- Size of the window kept for back-references, must be bigger than the biggest offset expected.- Throws:
java.io.IOException
- if reading failsjava.lang.IllegalArgumentException
- if windowSize is not bigger than 0
-
-
Method Detail
-
read
public int read() throws java.io.IOException
- Specified by:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.IOException
-
available
public int available()
- Overrides:
available
in classjava.io.InputStream
-
getSize
public int getSize()
Get the uncompressed size of the stream- Returns:
- the uncompressed size
-
prefill
public void prefill(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
- the data to fill the window with.- Throws:
java.lang.IllegalStateException
- if the stream has already started to read data
-
getCompressedCount
public long getCompressedCount()
- Specified by:
getCompressedCount
in interfaceInputStreamStatistics
- Returns:
- the amount of raw or compressed bytes read by the stream
- Since:
- 1.17
-
-