Class BitWriter

java.lang.Object
dev.roanh.cpqindex.BitWriter

public class BitWriter extends Object
Utility class for writing a sequence of integers to an array while using a variable number of bits for each integer.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private byte[]
    The data array being written to.
    private int
    The current byte in the data array being written.
    private int
    The next bit in the data array to write to.
  • Constructor Summary

    Constructors
    Constructor
    Description
    BitWriter(int bits)
    Constructs a new BitWriter with enough space for at least the requested number of bits.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    Gets the array containing the written bits.
    Constructs a string version of the bits written.
    void
    writeInt(int i, int bits)
    Writes the bits of the given integer using exactly the given number of bits.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • data

      private byte[] data
      The data array being written to.
    • pos

      private int pos
      The current byte in the data array being written.
    • sub

      private int sub
      The next bit in the data array to write to.
  • Constructor Details

    • BitWriter

      public BitWriter(int bits)
      Constructs a new BitWriter with enough space for at least the requested number of bits.
      Parameters:
      bits - The minimum number of bits to allocate space for in the data array.
  • Method Details

    • writeInt

      public void writeInt(int i, int bits)
      Writes the bits of the given integer using exactly the given number of bits.
      Parameters:
      i - The integer to write.
      bits - The number of bit to use to write the given integer.
    • getData

      public byte[] getData()
      Gets the array containing the written bits.
      Returns:
      The data array being written to.
    • toBinaryString

      public String toBinaryString()
      Constructs a string version of the bits written.
      Returns:
      A string with the written bits.