Class BitOutputBuffer

java.lang.Object
jj2000.j2k.codestream.writer.BitOutputBuffer

public class BitOutputBuffer extends Object
This class implements a buffer for writing bits, with the required bit stuffing policy for the packet headers. The bits are stored in a byte array in the order in which they are written. The byte array is automatically reallocated and enlarged whenever necessary. A BitOutputBuffer object may be reused by calling its 'reset()' method.

NOTE: The methods implemented in this class are intended to be used only in writing packet heads, since a special bit stuffing procedure is used, as required for the packet heads.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The increment size for the buffer, 16 bytes.
    static final int
    The initial size for the buffer, 32 bytes.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new BitOutputBuffer width a buffer of length 'SZ_INIT'.
  • Method Summary

    Modifier and Type
    Method
    Description
    final byte[]
    Returns the byte buffer.
    final int
    Returns the current length of the buffer, in bytes.
    void
    Resets the buffer.
    byte[]
    toByteArray(byte[] data)
    Returns the byte buffer data in a new array.
    Prints information about this object for debugging purposes
    final void
    writeBit(int bit)
    Writes a bit to the buffer at the current position.
    final void
    writeBits(int bits, int n)
    Writes the n least significant bits of 'bits' to the buffer at the current position.

    Methods inherited from class java.lang.Object

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

    • SZ_INCR

      public static final int SZ_INCR
      The increment size for the buffer, 16 bytes. This is the number of bytes that are added to the buffer each time it is needed to enlarge it.
      See Also:
    • SZ_INIT

      public static final int SZ_INIT
      The initial size for the buffer, 32 bytes.
      See Also:
  • Constructor Details

    • BitOutputBuffer

      public BitOutputBuffer()
      Creates a new BitOutputBuffer width a buffer of length 'SZ_INIT'.
  • Method Details

    • reset

      public void reset()
      Resets the buffer. This rewinds the current position to the start of the buffer and sets all tha data to 0. Note that no new buffer is allocated, so this will affect any data that was returned by the 'getBuffer()' method.
    • writeBit

      public final void writeBit(int bit)
      Writes a bit to the buffer at the current position. The value 'bit' must be either 0 or 1, otherwise it corrupts the bits that have been already written. The buffer is enlarged, by 'SZ_INCR' bytes, if necessary.

      This method is declared final to increase performance.

      Parameters:
      bit - The bit to write, 0 or 1.
    • writeBits

      public final void writeBits(int bits, int n)
      Writes the n least significant bits of 'bits' to the buffer at the current position. The least significant bit is written last. The 32-n most significant bits of 'bits' must be 0, otherwise corruption of the buffer will result. The buffer is enlarged, by 'SZ_INCR' bytes, if necessary.

      This method is declared final to increase performance.

      Parameters:
      bits - The bits to write.
      n - The number of LSBs in 'bits' to write.
    • getLength

      public final int getLength()
      Returns the current length of the buffer, in bytes.

      This method is declared final to increase performance.

      Returns:
      The currebt length of the buffer in bytes.
    • getBuffer

      public final byte[] getBuffer()
      Returns the byte buffer. This is the internal byte buffer so it should not be modified. Only the first N elements have valid data, where N is the value returned by 'getLength()'

      This method is declared final to increase performance.

      Returns:
      The internal byte buffer.
    • toByteArray

      public byte[] toByteArray(byte[] data)
      Returns the byte buffer data in a new array. This is a copy of the internal byte buffer. If 'data' is non-null it is used to return the data. This array should be large enough to contain all the data, otherwise a IndexOutOfBoundsException is thrown by the Java system. The number of elements returned is what 'getLength()' returns.
      Parameters:
      data - If non-null this array is used to return the data, which mus be large enough. Otherwise a new one is created and returned.
      Returns:
      The byte buffer data.
    • toString

      public String toString()
      Prints information about this object for debugging purposes
      Overrides:
      toString in class Object
      Returns:
      Information about the object.