Top Description Fields Constructors Methods
org.tukaani.xz

public Class LZMAInputStream

extends InputStream
Class Inheritance
Imports
java.io.InputStream, .DataInputStream, .IOException, .EOFException, org.tukaani.xz.lz.LZDecoder, org.tukaani.xz.rangecoder.RangeDecoderFromStream, org.tukaani.xz.lzma.LZMADecoder

Decompresses legacy .lzma files and raw LZMA streams (no .lzma header).

Important Note

In contrast to other classes in this package, this class reads data from its input stream one byte at a time. If the input stream is for example java.io.FileInputStream, wrapping it into java.io.BufferedInputStream tends to improve performance a lot. This is not automatically done by this class because there may be use cases where it is desired that this class won't read any bytes past the end of the LZMA stream.

Even when using BufferedInputStream, the performance tends to be worse (maybe 10-20 % slower) than with LZMA2InputStream or XZInputStream (when the .xz file contains LZMA2-compressed data).

Since
1.4

Field Summary

Modifier and TypeField and Description
private ArrayCache
public static final int
DICT_SIZE_MAX

Largest dictionary size supported by this implementation.

private boolean
private IOException
private InputStream
private LZDecoder
private LZMADecoder
private RangeDecoderFromStream
private boolean
private long
remainingSize

Number of uncompressed bytes left to be decompressed, or -1 if the end marker is used.

private final byte[]

Constructor Summary

AccessConstructor and Description
public
LZMAInputStream(InputStream
input stream from which .lzma data is read; it might be a good idea to wrap it in BufferedInputStream, see the note at the top of this page
in
)

Creates a new .lzma file format decompressor without a memory usage limit.

public
LZMAInputStream(InputStream
input stream from which .lzma data is read; it might be a good idea to wrap it in BufferedInputStream, see the note at the top of this page
in
,
ArrayCache
cache to be used for allocating large arrays
arrayCache
)

Creates a new .lzma file format decompressor without a memory usage limit.

public
LZMAInputStream(InputStream
input stream from which .lzma data is read; it might be a good idea to wrap it in BufferedInputStream, see the note at the top of this page
in
,
int
memory usage limit in kibibytes (KiB) or -1 to impose no memory usage limit
memoryLimit
)

Creates a new .lzma file format decompressor with an optional memory usage limit.

public
LZMAInputStream(InputStream
input stream from which .lzma data is read; it might be a good idea to wrap it in BufferedInputStream, see the note at the top of this page
in
,
int
memory usage limit in kibibytes (KiB) or -1 to impose no memory usage limit
memoryLimit
,
ArrayCache
cache to be used for allocating large arrays
arrayCache
)

Creates a new .lzma file format decompressor with an optional memory usage limit.

public
LZMAInputStream(InputStream
input stream from which compressed data is read
in
,
long
uncompressed size of the LZMA stream or -1 if the end marker is used in the LZMA stream
uncompSize
,
byte
LZMA properties byte that has the encoded values for literal context bits (lc), literal position bits (lp), and position bits (pb)
propsByte
,
int
dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]
dictSize
)

Creates a new input stream that decompresses raw LZMA data (no .lzma header) from in.

public
LZMAInputStream(InputStream
input stream from which LZMA-compressed data is read
in
,
long
uncompressed size of the LZMA stream or -1 if the end marker is used in the LZMA stream
uncompSize
,
byte
LZMA properties byte that has the encoded values for literal context bits (lc), literal position bits (lp), and position bits (pb)
propsByte
,
int
dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]
dictSize
,
byte[]
preset dictionary or null to use no preset dictionary
presetDict
)

Creates a new input stream that decompresses raw LZMA data (no .lzma header) from in optionally with a preset dictionary.

public
LZMAInputStream(InputStream
input stream from which LZMA-compressed data is read
in
,
long
uncompressed size of the LZMA stream or -1 if the end marker is used in the LZMA stream
uncompSize
,
byte
LZMA properties byte that has the encoded values for literal context bits (lc), literal position bits (lp), and position bits (pb)
propsByte
,
int
dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]
dictSize
,
byte[]
preset dictionary or null to use no preset dictionary
presetDict
,
ArrayCache
cache to be used for allocating large arrays
arrayCache
)

Creates a new input stream that decompresses raw LZMA data (no .lzma header) from in optionally with a preset dictionary.

public
LZMAInputStream(InputStream
input stream from which LZMA-compressed data is read
in
,
long
uncompressed size of the LZMA stream or -1 if the end marker is used in the LZMA stream
uncompSize
,
int
number of literal context bits, must be in the range [0, 8]
lc
,
int
number of literal position bits, must be in the range [0, 4]
lp
,
int
number position bits, must be in the range [0, 4]
pb
,
int
dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]
dictSize
,
byte[]
preset dictionary or null to use no preset dictionary
presetDict
)

Creates a new input stream that decompresses raw LZMA data (no .lzma header) from in optionally with a preset dictionary.

public
LZMAInputStream(InputStream
input stream from which LZMA-compressed data is read
in
,
long
uncompressed size of the LZMA stream or -1 if the end marker is used in the LZMA stream
uncompSize
,
int
number of literal context bits, must be in the range [0, 8]
lc
,
int
number of literal position bits, must be in the range [0, 4]
lp
,
int
number position bits, must be in the range [0, 4]
pb
,
int
dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]
dictSize
,
byte[]
preset dictionary or null to use no preset dictionary
presetDict
,
ArrayCache
cache to be used for allocating large arrays
arrayCache
)

Creates a new input stream that decompresses raw LZMA data (no .lzma header) from in optionally with a preset dictionary.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes the stream and calls in.close().
public void
enableRelaxedEndCondition()

Enables relaxed end-of-stream condition when uncompressed size is known.

private static int
getDictSize(int dictSize)

public static int

Returns:

approximate memory requirements as kibibytes (KiB)
getMemoryUsage
(int
LZMA dictionary size as bytes, should be in the range [0, DICT_SIZE_MAX]
dictSize
,
byte
LZMA properties byte that encodes the values of lc, lp, and pb
propsByte
)

Gets approximate decompressor memory requirements as kibibytes for the given dictionary size and LZMA properties byte (lc, lp, and pb).

public static int

Returns:

approximate memory requirements as kibibytes (KiB)
getMemoryUsage
(int
LZMA dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]
dictSize
,
int
number of literal context bits, must be in the range [0, 8]
lc
,
int
number of literal position bits, must be in the range [0, 4]
lp
)

Gets approximate decompressor memory requirements as kibibytes for the given dictionary size, lc, and lp.

private void
initialize(InputStream in, long uncompSize, byte propsByte, int dictSize, byte[] presetDict, ArrayCache arrayCache)

private void
initialize(InputStream in, long uncompSize, int lc, int lp, int pb, int dictSize, byte[] presetDict, ArrayCache arrayCache)

private void
public int

Returns:

the next decompressed byte, or -1 to indicate the end of the compressed stream
read
()

Implements abstract java.io.InputStream.read.

Decompresses the next byte from this input stream.
public int

Returns:

number of bytes read, or -1 to indicate the end of the compressed stream
read
(byte[]
target buffer for uncompressed data
buf
,
int
start offset in buf
off
,
int
maximum number of uncompressed bytes to read
len
)

Overrides java.io.InputStream.read.

Decompresses into an array of bytes.
Inherited from java.io.InputStream:
availablemarkmarkSupportednullInputStreamreadreadAllBytesreadNBytesreadNBytesresetskipskipNBytestransferTo

Field Detail

arrayCacheback to summary
private ArrayCache arrayCache
DICT_SIZE_MAXback to summary
public static final int DICT_SIZE_MAX

Largest dictionary size supported by this implementation.

LZMA allows dictionaries up to one byte less than 4 GiB. This implementation supports only 16 bytes less than 2 GiB. This limitation is due to Java using signed 32-bit integers for array indexing. The limitation shouldn't matter much in practice since so huge dictionaries are not normally used.

endReachedback to summary
private boolean endReached
exceptionback to summary
private IOException exception
inback to summary
private InputStream in
lzback to summary
private LZDecoder lz
lzmaback to summary
private LZMADecoder lzma
rcback to summary
private RangeDecoderFromStream rc
relaxedEndConditionback to summary
private boolean relaxedEndCondition
remainingSizeback to summary
private long remainingSize

Number of uncompressed bytes left to be decompressed, or -1 if the end marker is used.

tempBufback to summary
private final byte[] tempBuf

Constructor Detail

LZMAInputStreamback to summary
public LZMAInputStream(InputStream in) throws IOException

Creates a new .lzma file format decompressor without a memory usage limit.

Parameters
in:InputStream

input stream from which .lzma data is read; it might be a good idea to wrap it in BufferedInputStream, see the note at the top of this page

Exceptions
IOException:
may be thrown by in
CorruptedInputException:
file is corrupt or perhaps not in the .lzma format at all
UnsupportedOptionsException:
dictionary size or uncompressed size is too big for this implementation
EOFException:
file is truncated or perhaps not in the .lzma format at all
LZMAInputStreamback to summary
public LZMAInputStream(InputStream in, ArrayCache arrayCache) throws IOException

Creates a new .lzma file format decompressor without a memory usage limit.

This is identical to LZMAInputStream(InputStream) except that this also takes the arrayCache argument.

Parameters
in:InputStream

input stream from which .lzma data is read; it might be a good idea to wrap it in BufferedInputStream, see the note at the top of this page

arrayCache:ArrayCache

cache to be used for allocating large arrays

Exceptions
IOException:
may be thrown by in
CorruptedInputException:
file is corrupt or perhaps not in the .lzma format at all
UnsupportedOptionsException:
dictionary size or uncompressed size is too big for this implementation
EOFException:
file is truncated or perhaps not in the .lzma format at all
Since
1.7
LZMAInputStreamback to summary
public LZMAInputStream(InputStream in, int memoryLimit) throws IOException

Creates a new .lzma file format decompressor with an optional memory usage limit.

Parameters
in:InputStream

input stream from which .lzma data is read; it might be a good idea to wrap it in BufferedInputStream, see the note at the top of this page

memoryLimit:int

memory usage limit in kibibytes (KiB) or -1 to impose no memory usage limit

Exceptions
IOException:
may be thrown by in
CorruptedInputException:
file is corrupt or perhaps not in the .lzma format at all
UnsupportedOptionsException:
dictionary size or uncompressed size is too big for this implementation
MemoryLimitException:
memory usage limit was exceeded
EOFException:
file is truncated or perhaps not in the .lzma format at all
LZMAInputStreamback to summary
public LZMAInputStream(InputStream in, int memoryLimit, ArrayCache arrayCache) throws IOException

Creates a new .lzma file format decompressor with an optional memory usage limit.

This is identical to LZMAInputStream(InputStream, int) except that this also takes the arrayCache argument.

Parameters
in:InputStream

input stream from which .lzma data is read; it might be a good idea to wrap it in BufferedInputStream, see the note at the top of this page

memoryLimit:int

memory usage limit in kibibytes (KiB) or -1 to impose no memory usage limit

arrayCache:ArrayCache

cache to be used for allocating large arrays

Exceptions
IOException:
may be thrown by in
CorruptedInputException:
file is corrupt or perhaps not in the .lzma format at all
UnsupportedOptionsException:
dictionary size or uncompressed size is too big for this implementation
MemoryLimitException:
memory usage limit was exceeded
EOFException:
file is truncated or perhaps not in the .lzma format at all
Since
1.7
LZMAInputStreamback to summary
public LZMAInputStream(InputStream in, long uncompSize, byte propsByte, int dictSize) throws IOException

Creates a new input stream that decompresses raw LZMA data (no .lzma header) from in.

The caller needs to know if the "end of payload marker (EOPM)" alias "end of stream marker (EOS marker)" alias "end marker" present. If the end marker isn't used, the caller must know the exact uncompressed size of the stream.

The caller also needs to provide the LZMA properties byte that encodes the number of literal context bits (lc), literal position bits (lp), and position bits (pb).

The dictionary size used when compressing is also needed. Specifying a too small dictionary size will prevent decompressing the stream. Specifying a too big dictionary is waste of memory but decompression will work.

There is no need to specify a dictionary bigger than the uncompressed size of the data even if a bigger dictionary was used when compressing. If you know the uncompressed size of the data, this might allow saving some memory.

Parameters
in:InputStream

input stream from which compressed data is read

uncompSize:long

uncompressed size of the LZMA stream or -1 if the end marker is used in the LZMA stream

propsByte:byte

LZMA properties byte that has the encoded values for literal context bits (lc), literal position bits (lp), and position bits (pb)

dictSize:int

dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]

Exceptions
CorruptedInputException:
if propsByte is invalid or the first input byte is not 0x00
UnsupportedOptionsException:
dictionary size or uncompressed size is too big for this implementation
LZMAInputStreamback to summary
public LZMAInputStream(InputStream in, long uncompSize, byte propsByte, int dictSize, byte[] presetDict) throws IOException

Creates a new input stream that decompresses raw LZMA data (no .lzma header) from in optionally with a preset dictionary.

Parameters
in:InputStream

input stream from which LZMA-compressed data is read

uncompSize:long

uncompressed size of the LZMA stream or -1 if the end marker is used in the LZMA stream

propsByte:byte

LZMA properties byte that has the encoded values for literal context bits (lc), literal position bits (lp), and position bits (pb)

dictSize:int

dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]

presetDict:byte[]

preset dictionary or null to use no preset dictionary

Exceptions
IOException:
may be thrown by in
CorruptedInputException:
if propsByte is invalid or the first input byte is not 0x00
UnsupportedOptionsException:
dictionary size or uncompressed size is too big for this implementation
EOFException:
file is truncated or corrupt
LZMAInputStreamback to summary
public LZMAInputStream(InputStream in, long uncompSize, byte propsByte, int dictSize, byte[] presetDict, ArrayCache arrayCache) throws IOException

Creates a new input stream that decompresses raw LZMA data (no .lzma header) from in optionally with a preset dictionary.

This is identical to LZMAInputStream(InputStream, long, byte, int, byte[]) except that this also takes the arrayCache argument.

Parameters
in:InputStream

input stream from which LZMA-compressed data is read

uncompSize:long

uncompressed size of the LZMA stream or -1 if the end marker is used in the LZMA stream

propsByte:byte

LZMA properties byte that has the encoded values for literal context bits (lc), literal position bits (lp), and position bits (pb)

dictSize:int

dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]

presetDict:byte[]

preset dictionary or null to use no preset dictionary

arrayCache:ArrayCache

cache to be used for allocating large arrays

Exceptions
IOException:
may be thrown by in
CorruptedInputException:
if propsByte is invalid or the first input byte is not 0x00
UnsupportedOptionsException:
dictionary size or uncompressed size is too big for this implementation
EOFException:
file is truncated or corrupt
Since
1.7
LZMAInputStreamback to summary
public LZMAInputStream(InputStream in, long uncompSize, int lc, int lp, int pb, int dictSize, byte[] presetDict) throws IOException

Creates a new input stream that decompresses raw LZMA data (no .lzma header) from in optionally with a preset dictionary.

Parameters
in:InputStream

input stream from which LZMA-compressed data is read

uncompSize:long

uncompressed size of the LZMA stream or -1 if the end marker is used in the LZMA stream

lc:int

number of literal context bits, must be in the range [0, 8]

lp:int

number of literal position bits, must be in the range [0, 4]

pb:int

number position bits, must be in the range [0, 4]

dictSize:int

dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]

presetDict:byte[]

preset dictionary or null to use no preset dictionary

Exceptions
IOException:
may be thrown by in
CorruptedInputException:
if the first input byte is not 0x00
EOFException:
file is truncated or corrupt
LZMAInputStreamback to summary
public LZMAInputStream(InputStream in, long uncompSize, int lc, int lp, int pb, int dictSize, byte[] presetDict, ArrayCache arrayCache) throws IOException

Creates a new input stream that decompresses raw LZMA data (no .lzma header) from in optionally with a preset dictionary.

This is identical to LZMAInputStream(InputStream, long, int, int, int, int, byte[]) except that this also takes the arrayCache argument.

Parameters
in:InputStream

input stream from which LZMA-compressed data is read

uncompSize:long

uncompressed size of the LZMA stream or -1 if the end marker is used in the LZMA stream

lc:int

number of literal context bits, must be in the range [0, 8]

lp:int

number of literal position bits, must be in the range [0, 4]

pb:int

number position bits, must be in the range [0, 4]

dictSize:int

dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]

presetDict:byte[]

preset dictionary or null to use no preset dictionary

arrayCache:ArrayCache

cache to be used for allocating large arrays

Exceptions
IOException:
may be thrown by in
CorruptedInputException:
if the first input byte is not 0x00
EOFException:
file is truncated or corrupt
Since
1.7

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes the stream and calls in.close(). If the stream was already closed, this does nothing.

Exceptions
IOException:
if thrown by in.close()
enableRelaxedEndConditionback to summary
public void enableRelaxedEndCondition()

Enables relaxed end-of-stream condition when uncompressed size is known. This is useful if uncompressed size is known but it is unknown if the end of stream (EOS) marker is present. After calling this function, both are allowed.

Note that this doesn't actually check if the EOS marker is present. This introduces a few minor downsides:

  • Some (not all!) streams that would have more data than the specified uncompressed size, for example due to data corruption, will be accepted as valid.
  • After read has returned -1 the input position might not be at the end of the stream (too little input may have been read).

This should be called after the constructor before reading any data from the stream. This is a separate function because adding even more constructors to this class didn't look like a good alternative.

Since
1.9
getDictSizeback to summary
private static int getDictSize(int dictSize)
getMemoryUsageback to summary
public static int getMemoryUsage(int dictSize, byte propsByte) throws UnsupportedOptionsException, CorruptedInputException

Gets approximate decompressor memory requirements as kibibytes for the given dictionary size and LZMA properties byte (lc, lp, and pb).

Parameters
dictSize:int

LZMA dictionary size as bytes, should be in the range [0, DICT_SIZE_MAX]

propsByte:byte

LZMA properties byte that encodes the values of lc, lp, and pb

Returns:int

approximate memory requirements as kibibytes (KiB)

Exceptions
UnsupportedOptionsException:
if dictSize is outside the range [0, DICT_SIZE_MAX]
CorruptedInputException:
if propsByte is invalid
getMemoryUsageback to summary
public static int getMemoryUsage(int dictSize, int lc, int lp)

Gets approximate decompressor memory requirements as kibibytes for the given dictionary size, lc, and lp. Note that pb isn't needed.

Parameters
dictSize:int

LZMA dictionary size as bytes, must be in the range [0, DICT_SIZE_MAX]

lc:int

number of literal context bits, must be in the range [0, 8]

lp:int

number of literal position bits, must be in the range [0, 4]

Returns:int

approximate memory requirements as kibibytes (KiB)

initializeback to summary
private void initialize(InputStream in, long uncompSize, byte propsByte, int dictSize, byte[] presetDict, ArrayCache arrayCache) throws IOException
initializeback to summary
private void initialize(InputStream in, long uncompSize, int lc, int lp, int pb, int dictSize, byte[] presetDict, ArrayCache arrayCache) throws IOException
putArraysToCacheback to summary
private void putArraysToCache()
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Decompresses the next byte from this input stream.

Reading lots of data with read() from this input stream may be inefficient. Wrap it in java.io.BufferedInputStream if you need to read lots of data one byte at a time.

Returns:int

the next decompressed byte, or -1 to indicate the end of the compressed stream

Exceptions
IOException:
may be thrown by in
XZIOException:
if the stream has been closed
EOFException:
compressed input is truncated or corrupt
readback to summary
public int read(byte[] buf, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Decompresses into an array of bytes.

If len is zero, no bytes are read and 0 is returned. Otherwise this will block until len bytes have been decompressed, the end of the LZMA stream is reached, or an exception is thrown.

Parameters
buf:byte[]

target buffer for uncompressed data

off:int

start offset in buf

len:int

maximum number of uncompressed bytes to read

Returns:int

number of bytes read, or -1 to indicate the end of the compressed stream

Exceptions
IOException:
may be thrown by in
XZIOException:
if the stream has been closed
EOFException:
compressed input is truncated or corrupt