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:
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
- This class is not thread-safe
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intDefault code size in bits.protected final BitInputStreamInput.protected static final intUnused marker. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedLZWInputStream(InputStream inputStream, ByteOrder byteOrder) Constructs a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract intaddEntry(int previousCode, byte character) Add a new entry to the dictionary.protected intaddEntry(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 intAdd entry for repeat of previousCode we haven't added, yet.voidclose()protected abstract intReads the next code and expand it.protected intexpandCodeToOutputStack(int code, boolean addedUnfinishedEntry) Expands the entry with index code to the output stack and may create a new entry.protected intGets the clear code.protected intGets the code size in bits.longGets the amount of raw or compressed bytes read by the stream.protected intgetPrefix(int offset) Gets the prefix at the given offset.protected intGets the prefixes length.protected intGets the table size.protected voidIncrements the code size by one.protected voidinitializeTables(int maxCodeSize) Initializes the arrays based on the maximum code size.protected voidinitializeTables(int maxCodeSize, int memoryLimiKiB) Initializes the arrays based on the maximum code size.intread()intread(byte[] b, int off, int len) protected intReads the next code from the stream.protected voidResets the code size to its default value.protected voidResets the previous code to its default value.protected voidsetClearCode(int codeSize) Sets the clear code based on the code size.protected voidsetCodeSize(int codeSize) Sets the code size in bits.protected voidsetPrefix(int offset, int value) Sets the prefix at the given offset.protected voidsetTableSize(int tableSize) Sets the table size.Methods inherited from class org.apache.commons.compress.compressors.CompressorInputStream
count, count, getBytesRead, getCount, getUncompressedCount, pushedBackBytesMethods inherited from class java.io.InputStream
available, mark, markSupported, read, reset, skipMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.commons.compress.utils.InputStreamStatistics
getUncompressedCount
-
Field Details
-
DEFAULT_CODE_SIZE
Default code size in bits.- See Also:
-
UNUSED_PREFIX
Unused marker.- See Also:
-
in
Input.
-
-
Constructor Details
-
LZWInputStream
Constructs a new instance.- Parameters:
inputStream- The underlying input stream.byteOrder- the input byte order.
-
-
Method Details
-
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
-
addEntry
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
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
decompressNextSymbol
Reads the next code and expand it.- Returns:
- the expanded next code, negative on EOF
- Throws:
IOException- on error
-
expandCodeToOutputStack
Expands the entry with index code to the output stack and may create a new entry.- Parameters:
code- the code.addedUnfinishedEntry- whether unfinished entries have been added.- Returns:
- the new location of the output stack.
- Throws:
IOException- if an I/O error occurs.
-
getClearCode
Gets the clear code.- Returns:
- the clear code.
-
getCodeSize
Gets the code size in bits.- Returns:
- the code size in bits.
-
getCompressedCount
Description copied from interface:InputStreamStatisticsGets the amount of raw or compressed bytes read by the stream.- Specified by:
getCompressedCountin interfaceInputStreamStatistics- Returns:
- the amount of raw or compressed bytes read by the stream.
- Since:
- 1.17
-
getPrefix
Gets the prefix at the given offset.- Parameters:
offset- offset to query.- Returns:
- the prefix at the given offset.
-
getPrefixesLength
Gets the prefixes length.- Returns:
- the prefixes length.
-
getTableSize
Gets the table size.- Returns:
- the table size.
-
incrementCodeSize
Increments the code size by one. -
initializeTables
Initializes the arrays based on the maximum code size.- Parameters:
maxCodeSize- maximum code size- Throws:
IllegalArgumentException- ifmaxCodeSizeis out of bounds forprefixesandcharacters.
-
initializeTables
Initializes the arrays based on the maximum code size. First checks that the estimated memory usage is below memoryLimitInKb- Parameters:
maxCodeSize- maximum code sizememoryLimiKiB- maximum allowed estimated memory usage in kibibytes (KiB).- Throws:
MemoryLimitException- if estimated memory usage is greater than memoryLimitKiBIllegalArgumentException- ifmaxCodeSizeis not bigger than 0
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
readNextCode
Reads the next code from the stream.- Returns:
- the next code
- Throws:
IOException- on error
-
resetCodeSize
Resets the code size to its default value. -
resetPreviousCode
Resets the previous code to its default value. -
setClearCode
Sets the clear code based on the code size.- Parameters:
codeSize- code size
-
setCodeSize
Sets the code size in bits.- Parameters:
codeSize- the code size in bits.
-
setPrefix
Sets the prefix at the given offset.- Parameters:
offset- the target offset.value- the new value.
-
setTableSize
Sets the table size.- Parameters:
tableSize- the new table size.
-