Class BZip2CompressorOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class BZip2CompressorOutputStream
    extends 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

    • Constructor Detail

      • BZip2CompressorOutputStream

        public BZip2CompressorOutputStream​(java.io.OutputStream out)
                                    throws java.io.IOException
        Constructs a new BZip2CompressorOutputStream with 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 - if out == null.
      • BZip2CompressorOutputStream

        public BZip2CompressorOutputStream​(java.io.OutputStream out,
                                           int blockSize)
                                    throws java.io.IOException
        Constructs a new BZip2CompressorOutputStream with 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 - if out == 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 by BZip2CompressorOutputStream.
        Returns:
        The blocksize, between MIN_BLOCKSIZE and MAX_BLOCKSIZE both inclusive. For a negative inputLength this method returns MAX_BLOCKSIZE always.
      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • finish

        public void finish()
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        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:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException