Top Description Fields Constructors Methods
org.tukaani.xz

public Class LZMA2InputStream

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

Decompresses a raw LZMA2 stream (no XZ headers).

Field Summary

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

Largest dictionary size supported by this implementation.

public static final int
DICT_SIZE_MIN

Smallest valid LZMA2 dictionary size.

private boolean
private IOException
private DataInputStream
private boolean
private LZDecoder
private LZMADecoder
private boolean
private boolean
private RangeDecoderFromBuffer
private final byte[]
private int

Constructor Summary

AccessConstructor and Description
public
LZMA2InputStream(InputStream
input stream from which LZMA2-compressed data is read
in
,
int
LZMA2 dictionary size as bytes, must be in the range [DICT_SIZE_MIN, DICT_SIZE_MAX]
dictSize
)

Creates a new input stream that decompresses raw LZMA2 data from in.

public
LZMA2InputStream(InputStream
input stream from which LZMA2-compressed data is read
in
,
int
LZMA2 dictionary size as bytes, must be in the range [DICT_SIZE_MIN, DICT_SIZE_MAX]
dictSize
,
byte[]
preset dictionary or null to use no preset dictionary
presetDict
)

Creates a new LZMA2 decompressor using a preset dictionary.

pack-priv
LZMA2InputStream(InputStream
input stream from which LZMA2-compressed data is read
in
,
int
LZMA2 dictionary size as bytes, must be in the range [DICT_SIZE_MIN, 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 LZMA2 decompressor using a preset dictionary and array cache.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the number of uncompressed bytes that can be read without blocking
available
()

Overrides java.io.InputStream.available.

Returns the number of uncompressed bytes that can be read without blocking.
public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes the stream and calls in.close().
private void
private void
private static int
getDictSize(int dictSize)

public static int

Returns:

approximate memory requirements as kibibytes (KiB)
getMemoryUsage
(int
LZMA2 dictionary size as bytes, must be in the range [DICT_SIZE_MIN, DICT_SIZE_MAX]
dictSize
)

Gets approximate decompressor memory requirements as kibibytes for the given dictionary size.

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:
markmarkSupportednullInputStreamreadreadAllBytesreadNBytesreadNBytesresetskipskipNBytestransferTo

Field Detail

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

Largest dictionary size supported by this implementation.

The LZMA2 algorithm allows dictionaries up to one byte less than 4 GiB. This implementation supports only 16 bytes less than 2 GiB for raw LZMA2 streams, and for .xz files the maximum is 1.5 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.

DICT_SIZE_MINback to summary
public static final int DICT_SIZE_MIN

Smallest valid LZMA2 dictionary size.

Very tiny dictionaries would be a performance problem, so the minimum is 4 KiB.

endReachedback to summary
private boolean endReached
exceptionback to summary
private IOException exception
inback to summary
private DataInputStream in
isLZMAChunkback to summary
private boolean isLZMAChunk
lzback to summary
private LZDecoder lz
lzmaback to summary
private LZMADecoder lzma
needDictResetback to summary
private boolean needDictReset
needPropsback to summary
private boolean needProps
rcback to summary
private RangeDecoderFromBuffer rc
tempBufback to summary
private final byte[] tempBuf
uncompressedSizeback to summary
private int uncompressedSize

Constructor Detail

LZMA2InputStreamback to summary
public LZMA2InputStream(InputStream in, int dictSize)

Creates a new input stream that decompresses raw LZMA2 data from in.

The caller needs to know the dictionary size used when compressing; the dictionary size isn't stored as part of a raw LZMA2 stream.

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 LZMA2-compressed data is read

dictSize:int

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

LZMA2InputStreamback to summary
public LZMA2InputStream(InputStream in, int dictSize, byte[] presetDict)

Creates a new LZMA2 decompressor using a preset dictionary.

This is like LZMA2InputStream(InputStream, int) except that the dictionary may be initialized using a preset dictionary. If a preset dictionary was used when compressing the data, the same preset dictionary must be provided when decompressing.

Parameters
in:InputStream

input stream from which LZMA2-compressed data is read

dictSize:int

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

presetDict:byte[]

preset dictionary or null to use no preset dictionary

LZMA2InputStreamback to summary
pack-priv LZMA2InputStream(InputStream in, int dictSize, byte[] presetDict, ArrayCache arrayCache)

Creates a new LZMA2 decompressor using a preset dictionary and array cache.

This is like LZMA2InputStream(InputStream, int, byte[]) except that this also takes the arrayCache argument.

Parameters
in:InputStream

input stream from which LZMA2-compressed data is read

dictSize:int

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

presetDict:byte[]

preset dictionary or null to use no preset dictionary

arrayCache:ArrayCache

cache to be used for allocating large arrays

Since
1.7

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.InputStream.available.

Returns the number of uncompressed bytes that can be read without blocking. The value is returned with an assumption that the compressed input data will be valid. If the compressed data is corrupt, CorruptedInputException may get thrown before the number of bytes claimed to be available have been read from this input stream.

In LZMA2InputStream, the return value will be non-zero when the decompressor is in the middle of an LZMA2 chunk. The return value will then be the number of uncompressed bytes remaining from that chunk. The return value can also be non-zero in the middle of an uncompressed chunk, but then the return value depends also on the available() method of the underlying InputStream.

Returns:int

the number of uncompressed bytes that can be read without blocking

Exceptions
IOException:

Doc from java.io.InputStream.available.

if an I/O error occurs.

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()
decodeChunkHeaderback to summary
private void decodeChunkHeader() throws IOException
decodePropsback to summary
private void decodeProps() throws IOException
getDictSizeback to summary
private static int getDictSize(int dictSize)
getMemoryUsageback to summary
public static int getMemoryUsage(int dictSize)

Gets approximate decompressor memory requirements as kibibytes for the given dictionary size.

Parameters
dictSize:int

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

Returns:int

approximate memory requirements as kibibytes (KiB)

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 LZMA2 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