Top Description Inners Fields Constructors Methods
org.apache.commons.compress.compressors.bzip2

public Class BZip2CompressorOutputStream

extends CompressorOutputStream
implements BZip2Constants
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.compressors.bzip2.BZip2Constants
Imports
java.io.IOException, .OutputStream, java.util.Arrays, org.apache.commons.compress.compressors.CompressorOutputStream

An output stream that compresses into the BZip2 format into another stream.

The compression requires large amounts of memory. Thus you should call the close() method as soon as possible, to force BZip2CompressorOutputStream 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 BZip2CompressorInputStream use

<code>65k + (5 * blocksize)</code>.
Memory usage by blocksize
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 BZip2CompressorInputStream 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
private volatile boolean
private int
private final CRC
private int
private BZip2CompressorOutputStream.Data
data

All memory intensive stuff.

private static final int
private int
last

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

private static final int
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
private int

Constructor Summary

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

Constructs a new BZip2CompressorOutputStream with a blocksize of 900k.

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

Constructs a new BZip2CompressorOutputStream 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
(final long
The length of the data which will be compressed by BZip2CompressorOutputStream.
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
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:

the blocksize parameter specified at construction time
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)

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

private void
init()

Writes magic bytes like BZ on the first position of the stream and bytes indicating the file-format, which is huffmanized, followed by a digit indicating blockSize100k.

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 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.
public void
write(final int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte 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).

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
closedback to summary
private volatile boolean closed
combinedCRCback to summary
private int combinedCRC
crcback to summary
private final CRC crc
currentCharback to summary
private int currentChar
databack to summary
private BZip2CompressorOutputStream.Data data

All memory intensive stuff.

GREATER_ICOSTback to summary
private static final int GREATER_ICOST
lastback to summary
private int last

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

LESSER_ICOSTback to summary
private static final int LESSER_ICOST
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
runLengthback to summary
private int runLength

Constructor Detail

BZip2CompressorOutputStreamback to summary
public BZip2CompressorOutputStream(final OutputStream out) throws IOException

Constructs a new BZip2CompressorOutputStream with a blocksize of 900k.

Parameters
out:OutputStream

the destination stream.

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

Constructs a new BZip2CompressorOutputStream with specified blocksize.

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(final 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 BZip2CompressorOutputStream.

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

the blocksize parameter specified at construction time

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
private static void hbMakeCodeLengths(final byte[] len, final int[] freq, final BZip2CompressorOutputStream.Data dat, final int alphaSize, final int maxLen)
initback to summary
private void init() throws IOException

Writes magic bytes like BZ on the first position of the stream and bytes indicating the file-format, which is huffmanized, followed by a digit indicating blockSize100k.

Exceptions
IOException:
if the magic bytes could not been written
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 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.
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.
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.commons.compress.compressors.bzip2 back to summary

pack-priv final Class BZip2CompressorOutputStream.Data

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream.Data

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(final 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(final int blockSize100k)