Top Description Fields Constructors Methods
org.apache.commons.compress.compressors.lzw

public abstract Class LZWInputStream

extends CompressorInputStream
implements InputStreamStatistics
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.utils.InputStreamStatistics
Known Direct Subclasses
org.apache.commons.compress.compressors.z.ZCompressorInputStream, org.apache.commons.compress.archivers.zip.UnshrinkingInputStream
Imports
java.io.IOException, .InputStream, java.nio.ByteOrder, org.apache.commons.compress.MemoryLimitException, org.apache.commons.compress.compressors.CompressorInputStream, org.apache.commons.compress.utils.BitInputStream, .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.

Since
1.10

Field Summary

Modifier and TypeField and Description
private 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

AccessConstructor and Description
protected
LZWInputStream(final InputStream inputStream, final ByteOrder byteOrder)

Method Summary

Modifier and TypeMethod and Description
protected abstract int

Returns:

the new code
addEntry
(int
the previous code
previousCode
,
byte
the next character to append
character
)

Add a new entry to the dictionary.

protected int

Returns:

the new code or -1 if maxTableSize has been reached already
addEntry
(final int
the previous code
previousCode
,
final byte
the character to append
character
,
final int
the maximum table size
maxTableSize
)

Adds a new entry if the maximum table size hasn't been exceeded and returns the new index.

protected int

Returns:

new code for a repeat of the previous code or -1 if maxTableSize has been reached already
addRepeatOfPreviousCode
()

Add entry for repeat of previousCode we haven't added, yet.

public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.
protected abstract int

Returns:

the expanded next code, negative on EOF
decompressNextSymbol
()

Read the next code and expand it.

protected int

Returns:

the new location of the output stack
expandCodeToOutputStack
(final int
the code
code
,
final boolean
whether unfinished entries have been added
addedUnfinishedEntry
)

Expands the entry with index code to the output stack and may create a new entry

protected int
protected int
public long
getCompressedCount()

Implements org.apache.commons.compress.utils.InputStreamStatistics.getCompressedCount.

Gets the amount of raw or compressed bytes read by the stream.
protected int
getPrefix(final int offset)

protected int
protected int
protected void
protected void
initializeTables(final int
maximum code size
maxCodeSize
)

Initializes the arrays based on the maximum code size.

protected void
initializeTables(final int
maximum code size
maxCodeSize
,
final int
maximum allowed estimated memory usage in Kb
memoryLimitInKb
)

Initializes the arrays based on the maximum code size.

public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input stream.
public int
read(final byte[]
the buffer into which the data is read.
b
,
final int
the start offset in array b at which the data is written.
off
,
final int
the maximum number of bytes to read.
len
)

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes.
private int
readFromStack(final byte[] b, final int off, final int len)

protected int

Returns:

the next code
readNextCode
()

Reads the next code from the stream.

protected void
protected void
protected void
setClearCode(final int
code size
codeSize
)

Sets the clear code based on the code size.

protected void
setCodeSize(final int cs)

protected void
setPrefix(final int offset, final int value)

protected void
setTableSize(final int newSize)

Inherited from org.apache.commons.compress.compressors.CompressorInputStream:
countcountgetBytesReadgetCountgetUncompressedCountpushedBackBytes

Field Detail

charactersback to summary
private byte[] characters
clearCodeback to summary
private int clearCode
codeSizeback to summary
private int codeSize
DEFAULT_CODE_SIZEback to summary
protected static final int DEFAULT_CODE_SIZE
inback to summary
protected final BitInputStream in
oneByteback to summary
private final byte[] oneByte
outputStackback to summary
private byte[] outputStack
outputStackLocationback to summary
private int outputStackLocation
prefixesback to summary
private int[] prefixes
previousCodeback to summary
private int previousCode
previousCodeFirstCharback to summary
private byte previousCodeFirstChar
tableSizeback to summary
private int tableSize
UNUSED_PREFIXback to summary
protected static final int UNUSED_PREFIX

Constructor Detail

LZWInputStreamback to summary
protected LZWInputStream(final InputStream inputStream, final ByteOrder byteOrder)

Method Detail

addEntryback to summary
protected abstract int addEntry(int previousCode, byte character) throws IOException

Add a new entry to the dictionary.

Parameters
previousCode:int

the previous code

character:byte

the next character to append

Returns:int

the new code

Exceptions
IOException:
on error
addEntryback to summary
protected int addEntry(final int previousCode, final byte character, final int maxTableSize)

Adds a new entry if the maximum table size hasn't been exceeded and returns the new index.

Parameters
previousCode:int

the previous code

character:byte

the character to append

maxTableSize:int

the maximum table size

Returns:int

the new code or -1 if maxTableSize has been reached already

addRepeatOfPreviousCodeback to summary
protected int addRepeatOfPreviousCode() throws IOException

Add entry for repeat of previousCode we haven't added, yet.

Returns:int

new code for a repeat of the previous code or -1 if maxTableSize has been reached already

Exceptions
IOException:
on error
closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.InputStream.close.

Closes this input stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
decompressNextSymbolback to summary
protected abstract int decompressNextSymbol() throws IOException

Read the next code and expand it.

Returns:int

the expanded next code, negative on EOF

Exceptions
IOException:
on error
expandCodeToOutputStackback to summary
protected int expandCodeToOutputStack(final int code, final boolean addedUnfinishedEntry) throws IOException

Expands the entry with index code to the output stack and may create a new entry

Parameters
code:int

the code

addedUnfinishedEntry:boolean

whether unfinished entries have been added

Returns:int

the new location of the output stack

Exceptions
IOException:
on error
getClearCodeback to summary
protected int getClearCode()
getCodeSizeback to summary
protected int getCodeSize()
getCompressedCountback to summary
public long getCompressedCount()

Implements org.apache.commons.compress.utils.InputStreamStatistics.getCompressedCount.

Doc from org.apache.commons.compress.utils.InputStreamStatistics.getCompressedCount.

Gets the amount of raw or compressed bytes read by the stream.

Returns:long

the amount of raw or compressed bytes read by the stream.

Annotations
@Override
Since
1.17
getPrefixback to summary
protected int getPrefix(final int offset)
getPrefixesLengthback to summary
protected int getPrefixesLength()
getTableSizeback to summary
protected int getTableSize()
incrementCodeSizeback to summary
protected void incrementCodeSize()
initializeTablesback to summary
protected void initializeTables(final int maxCodeSize)

Initializes the arrays based on the maximum code size.

Parameters
maxCodeSize:int

maximum code size

Exceptions
IllegalArgumentException:
if maxCodeSize is out of bounds for prefixes and characters.
initializeTablesback to summary
protected void initializeTables(final int maxCodeSize, final int memoryLimitInKb) throws MemoryLimitException

Initializes the arrays based on the maximum code size. First checks that the estimated memory usage is below memoryLimitInKb

Parameters
maxCodeSize:int

maximum code size

memoryLimitInKb:int

maximum allowed estimated memory usage in Kb

Exceptions
MemoryLimitException:
if estimated memory usage is greater than memoryLimitInKb
IllegalArgumentException:
if maxCodeSize is not bigger than 0
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:int

the next byte of data, or -1 if the end of the stream is reached.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(final byte[] b, final int off, final int len) throws IOException

Overrides java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off-1] and elements b[off+len] through b[b.length-1] are unaffected.

Parameters
b:byte[]

the buffer into which the data is read.

off:int

the start offset in array b at which the data is written.

len:int

the maximum number of bytes to read.

Returns:int

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Annotations
@Override
Exceptions
IOException:
If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
readFromStackback to summary
private int readFromStack(final byte[] b, final int off, final int len)
readNextCodeback to summary
protected int readNextCode() throws IOException

Reads the next code from the stream.

Returns:int

the next code

Exceptions
IOException:
on error
resetCodeSizeback to summary
protected void resetCodeSize()
resetPreviousCodeback to summary
protected void resetPreviousCode()
setClearCodeback to summary
protected void setClearCode(final int codeSize)

Sets the clear code based on the code size.

Parameters
codeSize:int

code size

setCodeSizeback to summary
protected void setCodeSize(final int cs)
setPrefixback to summary
protected void setPrefix(final int offset, final int value)
setTableSizeback to summary
protected void setTableSize(final int newSize)