Top Description Inners Fields Constructors Methods
org.apache.tools.bzip2

public Class CBZip2OutputStream

extends OutputStream
implements BZip2Constants
Class Inheritance
All Implemented Interfaces
org.apache.tools.bzip2.BZip2Constants
Imports
java.io.IOException, .OutputStream

An output stream that compresses into the BZip2 format (without the file header chars) into another stream.

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
private final int
private int
private final int
blockSize100k

Always: in the range 0 ..

private BlockSort
private int
private int
protected static final int
CLEARMASK

This constant is accessible by subclasses for historical purposes.

private int
private final CRC
private int
private CBZip2OutputStream.Data
data

All memory intensive stuff.

protected static final int
DEPTH_THRESH

This constant is accessible by subclasses for historical purposes.

protected static final int
GREATER_ICOST

This constant is accessible by subclasses for historical purposes.

private static final int[]
INCS

Knuth's increments seem to work better than Incerpi-Sedgewick here.

private int
last

Index of the last char in the block, so the block size == last + 1.

protected static final int
LESSER_ICOST

This constant is accessible by subclasses for historical purposes.

public static final int
MAX_BLOCKSIZE

The maximum supported blocksize == 9.

public static final int
MIN_BLOCKSIZE

The minimum supported blocksize == 1.

private int
private int
private OutputStream
protected static final int
QSORT_STACK_SIZE

This constant is accessible by subclasses for historical purposes.

private int
protected static final int
SETMASK

This constant is accessible by subclasses for historical purposes.

protected static final int
SMALL_THRESH

This constant is accessible by subclasses for historical purposes.

protected static final int
WORK_FACTOR

This constant is accessible by subclasses for historical purposes.

Constructor Summary

AccessConstructor and Description
public
CBZip2OutputStream(final OutputStream
* the destination stream.
out
)

Constructs a new CBZip2OutputStream with a blocksize of 900k.

public
CBZip2OutputStream(final OutputStream
the destination stream.
out
,
final int
the blockSize as 100k units.
blockSize
)

Constructs a new CBZip2OutputStream with specified blocksize.

Method Summary

Modifier and TypeMethod and Description
private void
private void
private void
bsPutInt(final int u)

private void
bsPutUByte(final int c)

private void
bsW(final int n, final int v)

public static int

Returns:

The blocksize, between MIN_BLOCKSIZE and MAX_BLOCKSIZE both inclusive. For a negative inputLength this method returns MAX_BLOCKSIZE always.
chooseBlockSize
(long
The length of the data which will be compressed by CBZip2OutputStream.
inputLength
)

Chooses a blocksize based on the given length of the data to compress.

public void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes this output stream and releases any system resources associated with this stream.
private void
private void
protected void
finalize()

Overrides java.lang.Object.finalize.

Overridden to close the stream.
public void
public void
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this output stream and forces any buffered output bytes to be written out.
private void
public final int

Returns:

int
getBlockSize
()

Returns the blocksize parameter specified at construction time.

private static void
hbAssignCodes(final int[] code, final byte[] length, final int minLen, final int maxLen, final int alphaSize)

protected static void
hbMakeCodeLengths(char[]
char[]
len
,
int[]
char[]
freq
,
int
int
alphaSize
,
int
int
maxLen
)

This method is accessible by subclasses for historical purposes.

private static void
hbMakeCodeLengths(final byte[] len, final int[] freq, final CBZip2OutputStream.Data dat, final int alphaSize, final int maxLen)

private void
init()

private void
private void
private void
private void
sendMTFValues0(final int nGroups, final int alphaSize)

private int
sendMTFValues1(final int nGroups, final int alphaSize)

private void
sendMTFValues2(final int nGroups, final int nSelectors)

private void
sendMTFValues3(final int nGroups, final int alphaSize)

private void
private void
sendMTFValues5(final int nGroups, final int nSelectors)

private void
sendMTFValues6(final int nGroups, final int alphaSize)

private void
public void
write(final int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.
public void
write(final byte[]
the data.
buf
,
int
the start offset in the data.
offs
,
final int
the number of bytes to write.
len
)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.
private void
write0(int b)

Keeps track of the last bytes written and implicitly performs run-length encoding as the first step of the bzip2 algorithm.

private void
writeRun()

Writes the current byte to the buffer, run-length encoding it if it has been repeated at least four times (the first step RLEs sequences of four identical bytes).

Inherited from java.io.OutputStream:
nullOutputStreamwrite

Field Detail

allowableBlockSizeback to summary
private final int allowableBlockSize
blockCRCback to summary
private int blockCRC
blockSize100kback to summary
private final int blockSize100k

Always: in the range 0 .. 9. The current block size is 100000 * this number.

blockSorterback to summary
private BlockSort blockSorter
bsBuffback to summary
private int bsBuff
bsLiveback to summary
private int bsLive
CLEARMASKback to summary
protected static final int CLEARMASK

This constant is accessible by subclasses for historical purposes. If you don't know what it means then you don't need it.

combinedCRCback to summary
private int combinedCRC
crcback to summary
private final CRC crc
currentCharback to summary
private int currentChar
databack to summary
private CBZip2OutputStream.Data data

All memory intensive stuff.

DEPTH_THRESHback to summary
protected static final int DEPTH_THRESH

This constant is accessible by subclasses for historical purposes. If you don't know what it means then you don't need it.

GREATER_ICOSTback to summary
protected static final int GREATER_ICOST

This constant is accessible by subclasses for historical purposes. If you don't know what it means then you don't need it.

INCSback to summary
private static final int[] INCS

Knuth's increments seem to work better than Incerpi-Sedgewick here. Possibly because the number of elems to sort is usually small, typically <= 20.

Annotations
@SuppressWarnings:unused
lastback to summary
private int last

Index of the last char in the block, so the block size == last + 1.

LESSER_ICOSTback to summary
protected static final int LESSER_ICOST

This constant is accessible by subclasses for historical purposes. If you don't know what it means then you don't need it.

MAX_BLOCKSIZEback to summary
public static final int MAX_BLOCKSIZE

The maximum supported blocksize == 9.

MIN_BLOCKSIZEback to summary
public static final int MIN_BLOCKSIZE

The minimum supported blocksize == 1.

nInUseback to summary
private int nInUse
nMTFback to summary
private int nMTF
outback to summary
private OutputStream out
QSORT_STACK_SIZEback to summary
protected static final int QSORT_STACK_SIZE

This constant is accessible by subclasses for historical purposes. If you don't know what it means then you don't need it.

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.

runLengthback to summary
private int runLength
SETMASKback to summary
protected static final int SETMASK

This constant is accessible by subclasses for historical purposes. If you don't know what it means then you don't need it.

SMALL_THRESHback to summary
protected static final int SMALL_THRESH

This constant is accessible by subclasses for historical purposes. If you don't know what it means then you don't need it.

WORK_FACTORback to summary
protected static final int WORK_FACTOR

This constant is accessible by subclasses for historical purposes. If you don't know what it means then you don't need it.

Constructor Detail

CBZip2OutputStreamback to summary
public CBZip2OutputStream(final OutputStream out) throws IOException

Constructs a new CBZip2OutputStream with a blocksize of 900k.

Attention: The caller is responsible to write the two BZip2 magic bytes "BZ" to the specified stream prior to calling this constructor.

Parameters
out:OutputStream

* the destination stream.

Exceptions
IOException:
if an I/O error occurs in the specified stream.
NullPointerException:
if out == null.
CBZip2OutputStreamback to summary
public CBZip2OutputStream(final OutputStream out, final int blockSize) throws IOException

Constructs a new CBZip2OutputStream with specified blocksize.

Attention: The caller is responsible to write the two BZip2 magic bytes "BZ" to the specified stream prior to calling this constructor.

Parameters
out:OutputStream

the destination stream.

blockSize:int

the blockSize as 100k units.

Exceptions
IOException:
if an I/O error occurs in the specified stream.
IllegalArgumentException:
if (blockSize < 1) || (blockSize > 9).
NullPointerException:
if out == null.
See Also
MIN_BLOCKSIZE, MAX_BLOCKSIZE

Method Detail

blockSortback to summary
private void blockSort()
bsFinishedWithStreamback to summary
private void bsFinishedWithStream() throws IOException
bsPutIntback to summary
private void bsPutInt(final int u) throws IOException
bsPutUByteback to summary
private void bsPutUByte(final int c) throws IOException
bsWback to summary
private void bsW(final int n, final int v) throws IOException
chooseBlockSizeback to summary
public static int chooseBlockSize(long inputLength)

Chooses a blocksize based on the given length of the data to compress.

Parameters
inputLength:long

The length of the data which will be compressed by CBZip2OutputStream.

Returns:int

The blocksize, between MIN_BLOCKSIZE and MAX_BLOCKSIZE both inclusive. For a negative inputLength this method returns MAX_BLOCKSIZE always.

closeback to summary
public void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.OutputStream.close.

Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
endBlockback to summary
private void endBlock() throws IOException
endCompressionback to summary
private void endCompression() throws IOException
finalizeback to summary
protected void finalize() throws Throwable

Overrides java.lang.Object.finalize.

Overridden to close the stream.

Annotations
@Override
Exceptions
Throwable:

Doc from java.lang.Object.finalize.

the Exception raised by this method

finishback to summary
public void finish() throws IOException
flushback to summary
public void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.OutputStream.flush.

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of 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.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
generateMTFValuesback to summary
private void generateMTFValues()
getBlockSizeback to summary
public final int getBlockSize()

Returns the blocksize parameter specified at construction time.

Returns:int

int

hbAssignCodesback to summary
private static void hbAssignCodes(final int[] code, final byte[] length, final int minLen, final int maxLen, final int alphaSize)
hbMakeCodeLengthsback to summary
protected static void hbMakeCodeLengths(char[] len, int[] freq, int alphaSize, int maxLen)

This method is accessible by subclasses for historical purposes. If you don't know what it does then you don't need it.

Parameters
len:char[]

char[]

freq:int[]

char[]

alphaSize:int

int

maxLen:int

int

hbMakeCodeLengthsback to summary
private static void hbMakeCodeLengths(final byte[] len, final int[] freq, final CBZip2OutputStream.Data dat, final int alphaSize, final int maxLen)
initback to summary
private void init() throws IOException
initBlockback to summary
private void initBlock()
moveToFrontCodeAndSendback to summary
private void moveToFrontCodeAndSend() throws IOException
sendMTFValuesback to summary
private void sendMTFValues() throws IOException
sendMTFValues0back to summary
private void sendMTFValues0(final int nGroups, final int alphaSize)
sendMTFValues1back to summary
private int sendMTFValues1(final int nGroups, final int alphaSize)
sendMTFValues2back to summary
private void sendMTFValues2(final int nGroups, final int nSelectors)
sendMTFValues3back to summary
private void sendMTFValues3(final int nGroups, final int alphaSize)
sendMTFValues4back to summary
private void sendMTFValues4() throws IOException
sendMTFValues5back to summary
private void sendMTFValues5(final int nGroups, final int nSelectors) throws IOException
sendMTFValues6back to summary
private void sendMTFValues6(final int nGroups, final int alphaSize) throws IOException
sendMTFValues7back to summary
private void sendMTFValues7() throws IOException
writeback to summary
public void write(final int b) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for 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.

Parameters
b:int

the byte.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeback to summary
public void write(final byte[] buf, int offs, final int len) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes 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.

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.

Parameters
buf:byte[]

the data.

offs:int

the start offset in the data.

len:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.
write0back to summary
private void write0(int b) throws IOException

Keeps track of the last bytes written and implicitly performs run-length encoding as the first step of the bzip2 algorithm.

writeRunback to summary
private void writeRun() throws IOException

Writes the current byte to the buffer, run-length encoding it if it has been repeated at least four times (the first step RLEs sequences of four identical bytes).

Flushes the current block before writing data if it is full.

"write to the buffer" means adding to data.buffer starting two steps "after" this.last - initially starting at index 1 (not 0) - and updating this.last to point to the last index written minus 1.

org.apache.tools.bzip2 back to summary

pack-priv final Class CBZip2OutputStream.Data

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final byte[]
pack-priv final int[]
pack-priv final byte[]
pack-priv final int[]
pack-priv final boolean[]
pack-priv final int[]
pack-priv int
origPtr

Index of original line in Burrows-Wheeler table.

pack-priv final int[]
pack-priv final byte[]
pack-priv final byte[]
pack-priv final byte[]
pack-priv final int[][]
pack-priv final short[]
pack-priv final int[]
pack-priv final byte[][]
pack-priv final int[][]
pack-priv final boolean[]
pack-priv final char[]
pack-priv final byte[]
pack-priv final int[]

Constructor Summary

AccessConstructor and Description
pack-priv
Data(int blockSize100k)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

blockback to summary
pack-priv final byte[] block
fmapback to summary
pack-priv final int[] fmap
generateMTFValues_yyback to summary
pack-priv final byte[] generateMTFValues_yy
heapback to summary
pack-priv final int[] heap
inUseback to summary
pack-priv final boolean[] inUse
mtfFreqback to summary
pack-priv final int[] mtfFreq
origPtrback to summary
pack-priv int origPtr

Index of original line in Burrows-Wheeler table.

This is the index in fmap that points to the last byte of the original data.

parentback to summary
pack-priv final int[] parent
selectorback to summary
pack-priv final byte[] selector
selectorMtfback to summary
pack-priv final byte[] selectorMtf
sendMTFValues2_posback to summary
pack-priv final byte[] sendMTFValues2_pos
sendMTFValues_codeback to summary
pack-priv final int[][] sendMTFValues_code
sendMTFValues_costback to summary
pack-priv final short[] sendMTFValues_cost
sendMTFValues_faveback to summary
pack-priv final int[] sendMTFValues_fave
sendMTFValues_lenback to summary
pack-priv final byte[][] sendMTFValues_len
sendMTFValues_rfreqback to summary
pack-priv final int[][] sendMTFValues_rfreq
sentMTFValues4_inUse16back to summary
pack-priv final boolean[] sentMTFValues4_inUse16
sfmapback to summary
pack-priv final char[] sfmap
unseqToSeqback to summary
pack-priv final byte[] unseqToSeq
weightback to summary
pack-priv final int[] weight

Constructor Detail

Databack to summary
pack-priv Data(int blockSize100k)