Class BitOutputStream

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

    public class BitOutputStream
    extends java.io.OutputStream
    implements BitConstants
    The BitOutputStream allows varying bit sizes to be written to the wrapped OutputStream. This is useful for LZW type compression algorithms where 9-12 bit codes are used instead of the 8-bit byte.

    Warning: The write(byte[]) and write(byte[], int, int) methods of OutputStream will not work appropriately with any bit size > 8 bits.

    • Constructor Summary

      Constructors 
      Constructor Description
      BitOutputStream​(java.io.OutputStream os, int startingNumberOfBits)
      Create a BitOutpuStream wrapping the given OutputStream and writing the number of bits specified.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearRemainingBitsOfData()
      When shifting from buffer to buffer, this OutputStream also should be reset.
      void close()
      Close the output stream and write any remaining byte to the output.
      int getBitMask()
      Answer with the current bit mask for the current bit size.
      void increaseRequestedNumberOfBits()
      Increase the requested number of bits by one.
      void setRequestedNumberOfBits​(int numberOfBits)
      Set the number of bits to be write with each call to write(int).
      void write​(int b)
      Write the number of bits to the wrapped OutputStream.
      • Methods inherited from class java.io.OutputStream

        flush, nullOutputStream, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BitOutputStream

        public BitOutputStream​(java.io.OutputStream os,
                               int startingNumberOfBits)
        Create a BitOutpuStream wrapping the given OutputStream and writing the number of bits specified.
    • Method Detail

      • setRequestedNumberOfBits

        public void setRequestedNumberOfBits​(int numberOfBits)
        Set the number of bits to be write with each call to write(int).
      • increaseRequestedNumberOfBits

        public void increaseRequestedNumberOfBits()
        Increase the requested number of bits by one. This is the general usage and prevents client from needing to track the requested number of bits or from making various method calls.
      • getBitMask

        public int getBitMask()
        Answer with the current bit mask for the current bit size.
      • write

        public void write​(int b)
                   throws java.io.IOException
        Write the number of bits to the wrapped OutputStream.
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • clearRemainingBitsOfData

        public void clearRemainingBitsOfData()
        When shifting from buffer to buffer, this OutputStream also should be reset. This allows the "left over" bits to be cleared.
      • close

        public void close()
                   throws java.io.IOException
        Close the output stream and write any remaining byte to the output. Note that we may very well end up with extra bits if there are < 8 bits remaining.
        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