public final class LzmaOutputStream extends OutputStream
OutputStream
for encoding data using
the Lempel-Ziv-Markov chain algorithm. It uses the LZMA encoder from the LZMA SDK.
The data written by this encoder is has the following structure:
The API from the LZMA SDK is built around a standalone encoder. To adapt that into the Java streams API, the encoder is launched in a separate execution thread and it is fed data as it is written to the stream. When the stream is closed, the encoder finishes and the encoder thread is shut down.
Errors are propagated up from the encoder to the calling thread. If an error occurs in the encoder, it will be reported to the calling thread the next time that it tries to write to or close the stream.
If the application that uses this stream writes small data chunks to it, it
is probably a good idea to wrap it in a BufferedOutputStream
to ensure that the LZMA encoder gets bigger chunks of data to work with.
Compressing a file requires a lot of memory since the compression dictionary
has to be held in memory. The default size of the dictionary is
2^23 = 8.4 MB. By default, the buffer used to feed data to the
encoder has an unlimited size, which may cause the entire written data to be
stored in memory if the writing thread is fast and the encoder is slow. A
maximum size for the buffer can be set by using a custom data queue depth
when creating the compressing stream. See
LzmaOutputStreamSettings.setMaxDataQueueSize(int)
.
LzmaOutputStreamSettings
,
LzmaInputStream
Constructor and Description |
---|
LzmaOutputStream(OutputStream out)
Create a new LZMA compressing output stream using default compression
settings.
|
LzmaOutputStream(OutputStream out,
LzmaOutputStreamSettings settings)
Create a new LZMA compressing output stream using custom compression
settings.
|
LzmaOutputStream(OutputStream out,
LzmaOutputStreamSettings settings,
long uncompressedDataSize)
Create a new LZMA compressing output stream using custom compression
settings that compresses data of a known size.
|
public LzmaOutputStream(OutputStream out)
This method will set the uncompressed data size to -1
in the
compressed stream and will write an end of stream marker after all data.
out
- The output stream to write compressed data to.LzmaOutputStream(OutputStream, LzmaOutputStreamSettings)
public LzmaOutputStream(OutputStream out, LzmaOutputStreamSettings settings)
This method will set the uncompressed data size to -1 in the compressed stream and will write an end of stream marker after all data.
out
- The output stream to write compressed data to.settings
- Compression settings.LzmaOutputStream(OutputStream)
,
LzmaOutputStream(OutputStream, LzmaOutputStreamSettings, long)
public LzmaOutputStream(OutputStream out, LzmaOutputStreamSettings settings, long uncompressedDataSize) throws IllegalArgumentException
out
- The output stream to write compressed data to.settings
- Compression settings.uncompressedDataSize
- The size of the uncompressed data. Set this
to -1
(or rather, use any of the other constructors) if the data
size is unknown.IllegalArgumentException
- If the data size is zero or less than
-1.public void write(int b) throws IOException
write
in class OutputStream
IOException
public void write(byte[] barr) throws IOException
write
in class OutputStream
IOException
public void write(byte[] barr, int offset, int len) throws IOException
write
in class OutputStream
IOException
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException