Class LZWInputStream
java.lang.Object
java.io.InputStream
org.apache.commons.compress.compressors.CompressorInputStream
org.apache.commons.compress.compressors.lzw.LZWInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
,InputStreamStatistics
- Direct Known Subclasses:
UnshrinkingInputStream
,ZCompressorInputStream
Generic LZW implementation. It is used internally for the Z decompressor and the Unshrinking Zip file compression method, but may be useful for third-party projects in implementing their own LZW variations.
- Since:
- 1.10
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]
private int
private int
protected static final int
protected final BitInputStream
private final byte[]
private byte[]
private int
private int[]
private int
private byte
private int
protected static final int
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
LZWInputStream
(InputStream inputStream, ByteOrder byteOrder) -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract int
addEntry
(int previousCode, byte character) Add a new entry to the dictionary.protected int
addEntry
(int previousCode, byte character, int maxTableSize) Adds a new entry if the maximum table size hasn't been exceeded and returns the new index.protected int
Add entry for repeat of previousCode we haven't added, yet.void
close()
protected abstract int
Read the next code and expand it.protected int
expandCodeToOutputStack
(int code, boolean addedUnfinishedEntry) Expands the entry with index code to the output stack and may create a new entryprotected int
protected int
long
protected int
getPrefix
(int offset) protected int
protected int
protected void
protected void
initializeTables
(int maxCodeSize) Initializes the arrays based on the maximum code size.protected void
initializeTables
(int maxCodeSize, int memoryLimitInKb) Initializes the arrays based on the maximum code size.int
read()
int
read
(byte[] b, int off, int len) private int
readFromStack
(byte[] b, int off, int len) protected int
Reads the next code from the stream.protected void
protected void
protected void
setClearCode
(int codeSize) Sets the clear code based on the code size.protected void
setCodeSize
(int cs) protected void
setPrefix
(int offset, int value) protected void
setTableSize
(int newSize) Methods inherited from class org.apache.commons.compress.compressors.CompressorInputStream
count, count, getBytesRead, getCount, getUncompressedCount, pushedBackBytes
Methods inherited from class java.io.InputStream
available, mark, markSupported, read, readAllBytes, readNBytes, reset, skip, transferTo
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.commons.compress.utils.InputStreamStatistics
getUncompressedCount
-
Field Details
-
DEFAULT_CODE_SIZE
protected static final int DEFAULT_CODE_SIZE- See Also:
-
UNUSED_PREFIX
protected static final int UNUSED_PREFIX- See Also:
-
oneByte
private final byte[] oneByte -
in
-
clearCode
private int clearCode -
codeSize
private int codeSize -
previousCodeFirstChar
private byte previousCodeFirstChar -
previousCode
private int previousCode -
tableSize
private int tableSize -
prefixes
private int[] prefixes -
characters
private byte[] characters -
outputStack
private byte[] outputStack -
outputStackLocation
private int outputStackLocation
-
-
Constructor Details
-
LZWInputStream
-
-
Method Details
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
getCompressedCount
public long getCompressedCount()- Specified by:
getCompressedCount
in interfaceInputStreamStatistics
- Returns:
- the amount of raw or compressed bytes read by the stream
- Since:
- 1.17
-
decompressNextSymbol
Read the next code and expand it.- Returns:
- the expanded next code, negative on EOF
- Throws:
IOException
- on error
-
addEntry
Add a new entry to the dictionary.- Parameters:
previousCode
- the previous codecharacter
- the next character to append- Returns:
- the new code
- Throws:
IOException
- on error
-
setClearCode
protected void setClearCode(int codeSize) Sets the clear code based on the code size.- Parameters:
codeSize
- code size
-
initializeTables
Initializes the arrays based on the maximum code size. First checks that the estimated memory usage is below memoryLimitInKb- Parameters:
maxCodeSize
- maximum code sizememoryLimitInKb
- maximum allowed estimated memory usage in Kb- Throws:
MemoryLimitException
- if estimated memory usage is greater than memoryLimitInKbIllegalArgumentException
- ifmaxCodeSize
is not bigger than 0
-
initializeTables
protected void initializeTables(int maxCodeSize) Initializes the arrays based on the maximum code size.- Parameters:
maxCodeSize
- maximum code size- Throws:
IllegalArgumentException
- ifmaxCodeSize
is not bigger than 0
-
readNextCode
Reads the next code from the stream.- Returns:
- the next code
- Throws:
IOException
- on error
-
addEntry
protected int addEntry(int previousCode, byte character, int maxTableSize) Adds a new entry if the maximum table size hasn't been exceeded and returns the new index.- Parameters:
previousCode
- the previous codecharacter
- the character to appendmaxTableSize
- the maximum table size- Returns:
- the new code or -1 if maxTableSize has been reached already
-
addRepeatOfPreviousCode
Add entry for repeat of previousCode we haven't added, yet.- Returns:
- new code for a repeat of the previous code or -1 if maxTableSize has been reached already
- Throws:
IOException
- on error
-
expandCodeToOutputStack
Expands the entry with index code to the output stack and may create a new entry- Parameters:
code
- the codeaddedUnfinishedEntry
- whether unfinished entries have been added- Returns:
- the new location of the output stack
- Throws:
IOException
- on error
-
readFromStack
private int readFromStack(byte[] b, int off, int len) -
getCodeSize
protected int getCodeSize() -
resetCodeSize
protected void resetCodeSize() -
setCodeSize
protected void setCodeSize(int cs) -
incrementCodeSize
protected void incrementCodeSize() -
resetPreviousCode
protected void resetPreviousCode() -
getPrefix
protected int getPrefix(int offset) -
setPrefix
protected void setPrefix(int offset, int value) -
getPrefixesLength
protected int getPrefixesLength() -
getClearCode
protected int getClearCode() -
getTableSize
protected int getTableSize() -
setTableSize
protected void setTableSize(int newSize)
-