Class BitwiseOutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
org.dellroad.stuff.io.BitwiseOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class BitwiseOutputStream extends FilterOutputStream
A bit-oriented OutputStream.

Instances support writing arbitrary numbers of individual bits. Bits are written to the underlying OutputStream in groups of eight (i.e., whole bytes, obviously), where the bits in each written byte are ordered from least significant to most significant bit.

Of course, instances also support writing traditional byte-oriented data: any bytes written are handled as if each of the eight bits were written individually, in order from least significant to most significant.

When instances are closed, if the output bitstream is not currently aligned to a byte boundary (i.e., bitOffset() would return a non-zero value), then padding of up to seven zero bits is written (as if by padToByteBoundary()), and then the underlying stream is closed.

As an example, writing the bits 0b101001101111 and then invoking close() would result in 0x6d 0x0e being written to the underlying stream. The same output would result if 0b111, 0x4d, and then 0b001 were written.

See Also:
  • Constructor Details

    • BitwiseOutputStream

      public BitwiseOutputStream(OutputStream out)
      Constructor.
      Parameters:
      out - underlying output
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterOutputStream
      Throws:
      IOException
    • write

      public void write(byte[] buf, int off, int len) throws IOException
      Overrides:
      write in class FilterOutputStream
      Throws:
      IOException
    • write

      public void write(int b) throws IOException
      Overrides:
      write in class FilterOutputStream
      Throws:
      IOException
    • writeBits

      public void writeBits(BitSet bits, int len) throws IOException
      Write some bits from a BitSet.
      Parameters:
      bits - where to get the bits
      len - the number of bits to write
      Throws:
      IOException - if an I/O error occurs
      IllegalArgumentException - if bits is null
      IllegalArgumentException - if len is negative
    • writeBits

      public void writeBits(long bits, int len) throws IOException
      Write bits in a long value.

      The first bit in bits written is at index zero, etc. Bits at index len and higher are ignored.

      Parameters:
      bits - value containing the bits to write (low-order bit first)
      len - the number of bits in bits to write
      Throws:
      IOException - if an I/O error occurs
      IllegalArgumentException - if len is negative or greater than 64
    • writeBit

      public void writeBit(boolean bit) throws IOException
      Write a single bit.
      Parameters:
      bit - the bit to write
      Throws:
      IOException - if an I/O error occurs
    • bitOffset

      public int bitOffset()
      Get the current bit offset.
      Returns:
      current bit offset (from zero to seven)
    • padToByteBoundary

      public int padToByteBoundary() throws IOException
      Write zero bits to this output stream up to the next byte boundary.

      If the number of bits written so far is a multiple of eight, this method does nothing. Otherwise it writes zero bits until the next write operation will be byte-aligned.

      Returns:
      the number of zero bits written (from zero to seven)
      Throws:
      IOException - if an I/O error occurs