public class CBZip2OutputStream extends OutputStream implements BZip2Constants
The compression requires large amounts of memory. Thus you should call the
close()
method as soon as possible, to force
CBZip2OutputStream to release the allocated memory.
You can shrink the amount of allocated memory and maybe raise the compression speed by choosing a lower blocksize, which in turn may cause a lower compression ratio. You can avoid unnecessary memory allocation by avoiding using a blocksize which is bigger than the size of the input.
You can compute the memory usage for compressing by the following formula:
<code>400k + (9 * blocksize)</code>.
To get the memory required for decompression by CBZip2InputStream
use
<code>65k + (5 * blocksize)</code>.
Memory usage by blocksize | ||
---|---|---|
Blocksize | Compression memory usage | Decompression memory usage |
100k | 1300k | 565k |
200k | 2200k | 1065k |
300k | 3100k | 1565k |
400k | 4000k | 2065k |
500k | 4900k | 2565k |
600k | 5800k | 3065k |
700k | 6700k | 3565k |
800k | 7600k | 4065k |
900k | 8500k | 4565k |
For decompression CBZip2InputStream allocates less memory if the bzipped input is smaller than one block.
Instances of this class are not threadsafe.
TODO: Update to BZip2 1.0.1
Modifier and Type | Field and Description |
---|---|
protected static int |
CLEARMASK
This constant is accessible by subclasses for historical
purposes.
|
protected static int |
DEPTH_THRESH
This constant is accessible by subclasses for historical
purposes.
|
protected static int |
GREATER_ICOST
This constant is accessible by subclasses for historical
purposes.
|
protected static int |
LESSER_ICOST
This constant is accessible by subclasses for historical
purposes.
|
static int |
MAX_BLOCKSIZE
The maximum supported blocksize == 9.
|
static int |
MIN_BLOCKSIZE
The minimum supported blocksize == 1.
|
protected static int |
QSORT_STACK_SIZE
This constant is accessible by subclasses for historical
purposes.
|
protected static int |
SETMASK
This constant is accessible by subclasses for historical
purposes.
|
protected static int |
SMALL_THRESH
This constant is accessible by subclasses for historical
purposes.
|
protected static int |
WORK_FACTOR
This constant is accessible by subclasses for historical
purposes.
|
baseBlockSize, G_SIZE, MAX_ALPHA_SIZE, MAX_CODE_LEN, MAX_SELECTORS, N_GROUPS, N_ITERS, NUM_OVERSHOOT_BYTES, rNums, RUNA, RUNB
Constructor and Description |
---|
CBZip2OutputStream(OutputStream out)
Constructs a new CBZip2OutputStream with a blocksize of 900k.
|
CBZip2OutputStream(OutputStream out,
int blockSize)
Constructs a new CBZip2OutputStream with specified blocksize.
|
Modifier and Type | Method and Description |
---|---|
static int |
chooseBlockSize(long inputLength)
Chooses a blocksize based on the given length of the data to compress.
|
void |
close()
Closes this output stream and releases any system resources associated with this stream.
|
protected void |
finalize()
Overridden to close the stream.
|
void |
finish() |
void |
flush()
Flushes this output stream and forces any buffered output bytes to be written out.
|
int |
getBlockSize()
Returns the blocksize parameter specified at construction time.
|
protected static void |
hbMakeCodeLengths(char[] len,
int[] freq,
int alphaSize,
int maxLen)
This method is accessible by subclasses for historical
purposes.
|
void |
write(byte[] buf,
int offs,
int len)
Writes
len bytes from the specified byte array starting at offset off
to this output stream. |
void |
write(int b)
Writes the specified byte to this output stream.
|
write
protected static final int CLEARMASK
protected static final int DEPTH_THRESH
protected static final int GREATER_ICOST
protected static final int LESSER_ICOST
public static final int MAX_BLOCKSIZE
public static final int MIN_BLOCKSIZE
protected static final int QSORT_STACK_SIZE
If you are ever unlucky/improbable enough to get a stack overflow whilst sorting, increase the following constant and try again. In practice I have never seen the stack go above 27 elems, so the following limit seems very generous.
protected static final int SETMASK
protected static final int SMALL_THRESH
protected static final int WORK_FACTOR
public CBZip2OutputStream(OutputStream out) throws IOException
Attention: The caller is responsible to write the two BZip2 magic bytes "BZ" to the specified stream prior to calling this constructor.
out
- *
the destination stream.IOException
- if an I/O error occurs in the specified stream.NullPointerException
- if out == null
.public CBZip2OutputStream(OutputStream out, int blockSize) throws IOException
Attention: The caller is responsible to write the two BZip2 magic bytes "BZ" to the specified stream prior to calling this constructor.
out
- the destination stream.blockSize
- the blockSize as 100k units.IOException
- if an I/O error occurs in the specified stream.IllegalArgumentException
- if (blockSize < 1) || (blockSize > 9)
.NullPointerException
- if out == null
.MIN_BLOCKSIZE
,
MAX_BLOCKSIZE
public static int chooseBlockSize(long inputLength)
inputLength
- The length of the data which will be compressed by
CBZip2OutputStream.MIN_BLOCKSIZE
and
MAX_BLOCKSIZE
both inclusive. For a negative
inputLength this method returns MAX_BLOCKSIZE
always.public void close() throws IOException
OutputStream
close
is that it closes the output stream. A closed stream
cannot perform output operations and cannot be reopened.
The close
method of OutputStream
does nothing.
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException
- if an I/O error occurs.protected void finalize() throws Throwable
Throwable
public void finish() throws IOException
IOException
public void flush() throws IOException
OutputStream
flush
is that calling it is an indication that, if any bytes previously
written have been buffered by the implementation of the output stream, such bytes should
immediately be written to their intended destination.
If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.
The flush
method of OutputStream
does nothing.
flush
in interface Flushable
flush
in class OutputStream
IOException
- if an I/O error occurs.public final int getBlockSize()
protected static void hbMakeCodeLengths(char[] len, int[] freq, int alphaSize, int maxLen)
public void write(byte[] buf, int offs, int len) throws IOException
OutputStream
len
bytes from the specified byte array starting at offset off
to this output stream. The general contract for write(b, off, len)
is that some of
the bytes in the array b
are written to the output stream in order; element
b[off]
is the first byte written and b[off+len-1]
is the last byte
written by this operation.
The write
method of OutputStream
calls the write method of one argument
on each of the bytes to be written out. Subclasses are encouraged to override this method and
provide a more efficient implementation.
If b
is null
, a NullPointerException
is thrown.
If off
is negative, or len
is negative, or off+len
is
greater than the length of the array b
, then an IndexOutOfBoundsException
is thrown.
write
in class OutputStream
buf
- the data.offs
- the start offset in the data.len
- the number of bytes to write.IOException
- if an I/O error occurs. In particular, an IOException
is thrown if the
output stream is closed.public void write(int b) throws IOException
write
is
that one byte is written to the output stream. The byte to be written is the eight low-order bits
of the argument b
. The 24 high-order bits of b
are ignored.
Subclasses of OutputStream
must provide an implementation for this method.
write
in class OutputStream
b
- the byte
.IOException
- if an I/O error occurs. In particular, an IOException
may be thrown if
the output stream has been closed.