Class BZip2CompressorOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- org.apache.commons.compress.CompressFilterOutputStream<T>
-
- org.apache.commons.compress.compressors.CompressorOutputStream<java.io.OutputStream>
-
- org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class BZip2CompressorOutputStream extends CompressorOutputStream<java.io.OutputStream>
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 forceBZip2CompressorOutputStreamto 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
BZip2CompressorInputStreamuse<code>65k + (5 * blocksize)</code>.
Memory usage by blocksize Memory usage by blocksize Blocksize Compression
memory usageDecompression
memory usage100k 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
BZip2CompressorInputStreamallocates 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
-
-
Field Summary
Fields Modifier and Type Field Description static intBASEBLOCKSIZEConstant 100000.static intG_SIZEConstant 50.static intMAX_ALPHA_SIZEConstant 258.static intMAX_BLOCKSIZEThe maximum supported blocksize== 9.static intMAX_CODE_LENConstant 23.static intMAX_SELECTORSConstant 18002.static intMIN_BLOCKSIZEThe minimum supported blocksize== 1.static intN_GROUPSConstant 6.static intN_ITERSConstant 4.static intNUM_OVERSHOOT_BYTESConstant 20.static intRUNAConstant 0.static intRUNBConstant 1.
-
Constructor Summary
Constructors Constructor Description BZip2CompressorOutputStream(java.io.OutputStream out)Constructs a newBZip2CompressorOutputStreamwith a blocksize of 900k.BZip2CompressorOutputStream(java.io.OutputStream out, int blockSize)Constructs a newBZip2CompressorOutputStreamwith specified blocksize.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static intchooseBlockSize(long inputLength)Chooses a blocksize based on the given length of the data to compress.voidclose()voidfinish()Finishes the addition of entries to this stream, without closing it.voidflush()intgetBlockSize()Returns the blocksize parameter specified at construction time.voidwrite(byte[] buf, int offs, int len)voidwrite(int b)-
Methods inherited from class org.apache.commons.compress.CompressFilterOutputStream
checkOpen, isClosed, isFinished, out, write, write, writeUsAscii, writeUsAsciiRaw, writeUtf8
-
-
-
-
Field Detail
-
MIN_BLOCKSIZE
public static final int MIN_BLOCKSIZE
The minimum supported blocksize== 1.- See Also:
- Constant Field Values
-
MAX_BLOCKSIZE
public static final int MAX_BLOCKSIZE
The maximum supported blocksize== 9.- See Also:
- Constant Field Values
-
BASEBLOCKSIZE
public static final int BASEBLOCKSIZE
Constant 100000.- See Also:
- Constant Field Values
-
MAX_ALPHA_SIZE
public static final int MAX_ALPHA_SIZE
Constant 258.- See Also:
- Constant Field Values
-
MAX_CODE_LEN
public static final int MAX_CODE_LEN
Constant 23.- See Also:
- Constant Field Values
-
RUNA
public static final int RUNA
Constant 0.- See Also:
- Constant Field Values
-
RUNB
public static final int RUNB
Constant 1.- See Also:
- Constant Field Values
-
N_GROUPS
public static final int N_GROUPS
Constant 6.- See Also:
- Constant Field Values
-
G_SIZE
public static final int G_SIZE
Constant 50.- See Also:
- Constant Field Values
-
N_ITERS
public static final int N_ITERS
Constant 4.- See Also:
- Constant Field Values
-
MAX_SELECTORS
public static final int MAX_SELECTORS
Constant 18002.- See Also:
- Constant Field Values
-
NUM_OVERSHOOT_BYTES
public static final int NUM_OVERSHOOT_BYTES
Constant 20.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BZip2CompressorOutputStream
public BZip2CompressorOutputStream(java.io.OutputStream out) throws java.io.IOException
Constructs a newBZip2CompressorOutputStreamwith a blocksize of 900k.- Parameters:
out- the destination stream.- Throws:
java.io.IOException- if an I/O error occurs in the specified stream.java.lang.NullPointerException- ifout == null.
-
BZip2CompressorOutputStream
public BZip2CompressorOutputStream(java.io.OutputStream out, int blockSize) throws java.io.IOException
Constructs a newBZip2CompressorOutputStreamwith specified blocksize.- Parameters:
out- the destination stream.blockSize- the blockSize as 100k units.- Throws:
java.io.IOException- if an I/O error occurs in the specified stream.java.lang.IllegalArgumentException- if(blockSize < 1) || (blockSize > 9).java.lang.NullPointerException- ifout == null.- See Also:
MIN_BLOCKSIZE,MAX_BLOCKSIZE
-
-
Method Detail
-
chooseBlockSize
public static int chooseBlockSize(long inputLength)
Chooses a blocksize based on the given length of the data to compress.- Parameters:
inputLength- The length of the data which will be compressed byBZip2CompressorOutputStream.- Returns:
- The blocksize, between
MIN_BLOCKSIZEandMAX_BLOCKSIZEboth inclusive. For a negativeinputLengththis method returnsMAX_BLOCKSIZEalways.
-
close
public void close() throws java.io.IOException
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classCompressFilterOutputStream<java.io.OutputStream>- Throws:
java.io.IOException
-
finish
public void finish() throws java.io.IOException
Description copied from class:CompressFilterOutputStreamFinishes the addition of entries to this stream, without closing it. Additional data can be written, if the format supports it.- Overrides:
finishin classCompressFilterOutputStream<java.io.OutputStream>- Throws:
java.io.IOException- Maybe thrown by subclasses if the user forgets to close the entry.
-
flush
public void flush() throws java.io.IOException
- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.FilterOutputStream- Throws:
java.io.IOException
-
getBlockSize
public final int getBlockSize()
Returns the blocksize parameter specified at construction time.- Returns:
- the blocksize parameter specified at construction time
-
write
public void write(byte[] buf, int offs, int len) throws java.io.IOException
- Overrides:
writein classjava.io.FilterOutputStream- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException
- Overrides:
writein classjava.io.FilterOutputStream- Throws:
java.io.IOException
-
-