org.at4j.comp.lzma
Class LzmaInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.at4j.comp.lzma.LzmaInputStream
All Implemented Interfaces:
Closeable

public final class LzmaInputStream
extends InputStream

This class provides an InputStream for decoding data using the Lempel-Ziv-Markov chain algorithm. It uses the LZMA decoder from the LZMA SDK.

The API from the LZMA SDK is built around a standalone decoder. To adapt that into the Java streams API, the decoder is launched in a separate execution thread. The decoder writes data to a blocking queue that this stream reads its data from.

Errors are propagated up from the decoder to the calling thread. If an error occurs in the decoder, it will be reported to the calling thread the next time that it tries to read from or close the stream.

Decoding a file consumes a lot of memory – up to the dictionary size set when encoding it multiplied with the decoder data queue depth plus one. (The default dictionary size is 2^23 = 8.4 MB and the default data queue depth is 1.)

By default, data in an LZMA stream has the following format:

  1. Settings used when encoding (5 bytes)
  2. Total size of data in the stream when uncompressed (8 bytes, little endian). This may be set to -1 if the size of the data was unknown at the time of compression (stream mode). If so, the data part must be followed by an end of stream marker
  3. Data
  4. End of stream marker (if the total data size was set to -1)
When LZMA compressed data is used in a setting where some of the header data may be known before opening the stream, such as in a Zip file, the settings header may be omitted. When creating this stream, the client may pass in a LzmaInputStreamSettings object that says which headers the stream can expect to find.

Since:
1.0
Author:
Karl Gustafsson
See Also:
LzmaInputStreamSettings, LzmaOutputStream

Constructor Summary
LzmaInputStream(InputStream is)
          Create a LZMA decoding stream using the default settings.
LzmaInputStream(InputStream is, LzmaInputStreamSettings settings)
          Create a LZMA decoding stream using custom settings.
 
Method Summary
 void close()
           
protected  void finalize()
           
 int read()
           
 int read(byte[] barr)
           
 int read(byte[] barr, int off, int len)
           
 
Methods inherited from class java.io.InputStream
available, mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LzmaInputStream

public LzmaInputStream(InputStream is)
Create a LZMA decoding stream using the default settings.

Parameters:
is - The stream containing LZMA encoded data. Since this stream is read in a separate thread, a lock-aware stream such as one returned from Files.openForRead(org.entityfs.ReadableFile) cannot be used.
See Also:
LzmaInputStream(InputStream, LzmaInputStreamSettings), LzmaInputStreamSettings

LzmaInputStream

public LzmaInputStream(InputStream is,
                       LzmaInputStreamSettings settings)
                throws IllegalArgumentException
Create a LZMA decoding stream using custom settings.

Parameters:
is - The stream containing LZMA encoded data. Since this stream is read in a separate thread, a lock-aware stream such as one returned from Files.openForRead(org.entityfs.ReadableFile) cannot be used.
settings - The compression settings that were used when writing the compressed data.
Throws:
IllegalArgumentException - If the uncompressed size is less than -1.
Method Detail

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] barr)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

read

public int read(byte[] barr,
                int off,
                int len)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable