public abstract class LZWInputStream extends CompressorInputStream implements InputStreamStatistics
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.
Modifier and Type | Field and Description |
---|---|
private byte[] |
characters |
private int |
clearCode |
private int |
codeSize |
protected static int |
DEFAULT_CODE_SIZE |
protected BitInputStream |
in |
private byte[] |
oneByte |
private byte[] |
outputStack |
private int |
outputStackLocation |
private int[] |
prefixes |
private int |
previousCode |
private byte |
previousCodeFirstChar |
private int |
tableSize |
protected static int |
UNUSED_PREFIX |
Modifier | Constructor and Description |
---|---|
protected |
LZWInputStream(java.io.InputStream inputStream,
java.nio.ByteOrder byteOrder) |
Modifier and Type | Method and Description |
---|---|
protected 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 |
addRepeatOfPreviousCode()
Add entry for repeat of previousCode we haven't added, yet.
|
void |
close() |
protected abstract int |
decompressNextSymbol()
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 entry
|
protected int |
getClearCode() |
protected int |
getCodeSize() |
long |
getCompressedCount() |
protected int |
getPrefix(int offset) |
protected int |
getPrefixesLength() |
protected int |
getTableSize() |
protected void |
incrementCodeSize() |
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 |
readNextCode()
Reads the next code from the stream.
|
protected void |
resetCodeSize() |
protected void |
resetPreviousCode() |
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) |
count, count, getBytesRead, getCount, getUncompressedCount, pushedBackBytes
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getUncompressedCount
protected static final int DEFAULT_CODE_SIZE
protected static final int UNUSED_PREFIX
private final byte[] oneByte
protected final BitInputStream in
private int clearCode
private int codeSize
private byte previousCodeFirstChar
private int previousCode
private int tableSize
private int[] prefixes
private byte[] characters
private byte[] outputStack
private int outputStackLocation
protected LZWInputStream(java.io.InputStream inputStream, java.nio.ByteOrder byteOrder)
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int read(byte[] b, int off, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public long getCompressedCount()
getCompressedCount
in interface InputStreamStatistics
protected abstract int decompressNextSymbol() throws java.io.IOException
java.io.IOException
- on errorprotected abstract int addEntry(int previousCode, byte character) throws java.io.IOException
previousCode
- the previous codecharacter
- the next character to appendjava.io.IOException
- on errorprotected void setClearCode(int codeSize)
codeSize
- code sizeprotected void initializeTables(int maxCodeSize, int memoryLimitInKb) throws MemoryLimitException
maxCodeSize
- maximum code sizememoryLimitInKb
- maximum allowed estimated memory usage in KbMemoryLimitException
- if estimated memory usage is greater than memoryLimitInKbprotected void initializeTables(int maxCodeSize)
maxCodeSize
- maximum code sizeprotected int readNextCode() throws java.io.IOException
java.io.IOException
- on errorprotected int addEntry(int previousCode, byte character, int maxTableSize)
previousCode
- the previous codecharacter
- the character to appendmaxTableSize
- the maximum table sizeprotected int addRepeatOfPreviousCode() throws java.io.IOException
java.io.IOException
- on errorprotected int expandCodeToOutputStack(int code, boolean addedUnfinishedEntry) throws java.io.IOException
code
- the codeaddedUnfinishedEntry
- whether unfinished entries have been addedjava.io.IOException
- on errorprivate int readFromStack(byte[] b, int off, int len)
protected int getCodeSize()
protected void resetCodeSize()
protected void setCodeSize(int cs)
protected void incrementCodeSize()
protected void resetPreviousCode()
protected int getPrefix(int offset)
protected void setPrefix(int offset, int value)
protected int getPrefixesLength()
protected int getClearCode()
protected int getTableSize()
protected void setTableSize(int newSize)