Class BZip2CompressorOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
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 | ||
---|---|---|
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
- This class is not thread-safe
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Constant 100000.static final int
Constant 50.static final int
Constant 258.static final int
The maximum supported blocksize== 9
.static final int
Constant 23.static final int
Constant 18002.static final int
The minimum supported blocksize== 1
.static final int
Constant 6.static final int
Constant 4.static final int
Constant 20.static final int
Constant 0.static final int
Constant 1.Fields inherited from class java.io.FilterOutputStream
out
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newBZip2CompressorOutputStream
with a blocksize of 900k.BZip2CompressorOutputStream
(OutputStream out, int blockSize) Constructs a newBZip2CompressorOutputStream
with specified blocksize. -
Method Summary
Modifier and TypeMethodDescriptionstatic int
chooseBlockSize
(long inputLength) Chooses a blocksize based on the given length of the data to compress.void
close()
void
finish()
Finishes the addition of entries to this stream, without closing it.void
flush()
final int
Returns the blocksize parameter specified at construction time.void
write
(byte[] buf, int offs, int len) void
write
(int b) Methods inherited from class org.apache.commons.compress.CompressFilterOutputStream
checkOpen, isClosed, isFinished, out, write, write, writeUsAscii, writeUsAsciiRaw, writeUtf8
Methods inherited from class java.io.FilterOutputStream
write
-
Field Details
-
MIN_BLOCKSIZE
The minimum supported blocksize== 1
.- See Also:
-
MAX_BLOCKSIZE
The maximum supported blocksize== 9
.- See Also:
-
BASEBLOCKSIZE
static final int BASEBLOCKSIZEConstant 100000.- See Also:
-
MAX_ALPHA_SIZE
static final int MAX_ALPHA_SIZEConstant 258.- See Also:
-
MAX_CODE_LEN
static final int MAX_CODE_LENConstant 23.- See Also:
-
RUNA
static final int RUNAConstant 0.- See Also:
-
RUNB
static final int RUNBConstant 1.- See Also:
-
N_GROUPS
static final int N_GROUPSConstant 6.- See Also:
-
G_SIZE
static final int G_SIZEConstant 50.- See Also:
-
N_ITERS
static final int N_ITERSConstant 4.- See Also:
-
MAX_SELECTORS
static final int MAX_SELECTORSConstant 18002.- See Also:
-
NUM_OVERSHOOT_BYTES
static final int NUM_OVERSHOOT_BYTESConstant 20.- See Also:
-
-
Constructor Details
-
BZip2CompressorOutputStream
Constructs a newBZip2CompressorOutputStream
with a blocksize of 900k.- Parameters:
out
- the destination stream.- Throws:
IOException
- if an I/O error occurs in the specified stream.NullPointerException
- ifout == null
.
-
BZip2CompressorOutputStream
Constructs a newBZip2CompressorOutputStream
with specified blocksize.- Parameters:
out
- the destination stream.blockSize
- the blockSize as 100k units.- Throws:
IOException
- if an I/O error occurs in the specified stream.IllegalArgumentException
- if(blockSize < 1) || (blockSize > 9)
.NullPointerException
- ifout == null
.- See Also:
-
-
Method Details
-
chooseBlockSize
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_BLOCKSIZE
andMAX_BLOCKSIZE
both inclusive. For a negativeinputLength
this method returnsMAX_BLOCKSIZE
always.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classCompressFilterOutputStream<OutputStream>
- Throws:
IOException
-
finish
Description copied from class:CompressFilterOutputStream
Finishes the addition of entries to this stream, without closing it. Additional data can be written, if the format supports it.- Overrides:
finish
in classCompressFilterOutputStream<OutputStream>
- Throws:
IOException
- Maybe thrown by subclasses if the user forgets to close the entry.
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classFilterOutputStream
- Throws:
IOException
-
getBlockSize
Returns the blocksize parameter specified at construction time.- Returns:
- the blocksize parameter specified at construction time
-
write
- Overrides:
write
in classFilterOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classFilterOutputStream
- Throws:
IOException
-