Class NonBlockingBitOutputStream

java.lang.Object
com.helger.commons.io.stream.NonBlockingBitOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
BitOutputStream

public class NonBlockingBitOutputStream extends Object implements Closeable, Flushable
The NonBlockingBitOutputStream allows writing individual bits to a general Java OutputStream. Like the various Stream-classes from Java, the BitOutputStream has to be created based on another OutputStream. This class is able to write a single bit to a stream (even though a byte has to be filled until the data is flushed to the underlying output stream). It is also able to write an integer value to the stream using the specified number of bits.
For a thread-safe version see BitOutputStream
Author:
Andreas Jakl, Philip Helger
  • Constructor Details

    • NonBlockingBitOutputStream

      public NonBlockingBitOutputStream(@Nonnull OutputStream aOS, @Nonnull ByteOrder aByteOrder)
      Create a new bit output stream based on an existing Java OutputStream.
      Parameters:
      aOS - the output stream this class should use. May not be null.
      aByteOrder - The non-null byte order to use.
  • Method Details

    • getByteOrder

      @Nonnull public ByteOrder getByteOrder()
      Returns:
      The byte order used. Never null.
    • writeBit

      public void writeBit(int aBit) throws IOException
      Write a single bit to the stream. It will only be flushed to the underlying OutputStream when a byte has been completed or when flush() manually.
      Parameters:
      aBit - 1 if the bit should be set, 0 if not
      Throws:
      IOException - In case writing to the output stream failed
    • writeBits

      public void writeBits(int aValue, @Nonnegative int nNumBits) throws IOException
      Write the specified number of bits from the int value to the stream. Corresponding to the InputStream, the bits are written starting at the highest bit ( >> aNumberOfBits ), going down to the lowest bit ( >> 0 ).
      Parameters:
      aValue - the int containing the bits that should be written to the stream.
      nNumBits - how many bits of the integer should be written to the stream.
      Throws:
      IOException - In case writing to the output stream failed
    • flush

      public void flush() throws IOException
      Write the current cache to the stream and reset the buffer.
      Specified by:
      flush in interface Flushable
      Throws:
      IOException - In case writing to the output stream failed
    • close

      public void close()
      Flush the data and close the underlying output stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • toString

      public String toString()
      Overrides:
      toString in class Object