public void writeBits (final int aValue, @Nonnegative final int nNumBits) throws IOException
  {
    ValueEnforcer.isBetweenInclusive (nNumBits, "NumberOfBits", 1, CGlobal.BITS_PER_INT);

    for (int i = nNumBits - 1; i >= 0; i--)
      writeBit ((aValue >> i) & 1);
  }

  /**
   * Write the current cache to the stream and reset the buffer.
   *
   * @throws IOException
   *         In case writing to the output stream failed
   */
  public void flush () throws IOException
  {
    if (m_nBufferedBitCount > 0)
    {
      if (m_nBufferedBitCount != CGlobal.BITS_PER_BYTE)
        if (LOGGER.isDebugEnabled ())
          LOGGER.debug ("Flushing BitOutputStream with only " + m_nBufferedBitCount + " bits");
      m_aOS.write ((byte) m_nBuffer);
      m_nBufferedBitCount = 0;
      m_nBuffer = 0;
    }
  }

  /**
   * Flush the data and close the underlying output stream.
   */
  public void close ()
  {
    StreamHelper.flush (this);
    StreamHelper.close (m_aOS);
    m_aOS = null;
  }

  @Override
  public String toString ()
  {
    return new ToStringGenerator (this).append ("OS", m_aOS)
                                       .append ("highOrderBitFirst", m_bHighOrderBitFirst)
                                       .append ("buffer", m_nBuffer)
                                       .append ("bitCount", m_nBufferedBitCount)
                                       .getToString ();
  }
}
public void writeBits ( final int aValue , @ Nonnegative final int nNumBits ) throws IOException { ValueEnforcer . isBetweenInclusive ( nNumBits , "NumberOfBits" , 1 , CGlobal . BITS_PER_INT ) ; for ( int i = nNumBits - 1 ; i >= 0 ; i -- ) writeBit ( ( aValue > > i ) & 1 ) ; } public void flush ( ) throws IOException { if ( m_nBufferedBitCount > 0 ) { if ( m_nBufferedBitCount != CGlobal . BITS_PER_BYTE ) if ( LOGGER . isDebugEnabled ( ) ) LOGGER . debug ( "Flushing BitOutputStream with only " + m_nBufferedBitCount + " bits" ) ; m_aOS . write ( ( byte ) m_nBuffer ) ; m_nBufferedBitCount = 0 ; m_nBuffer = 0 ; } } public void close ( ) { StreamHelper . flush ( this ) ; StreamHelper . close ( m_aOS ) ; m_aOS = null ; } @ Override public String toString ( ) { return new ToStringGenerator ( this ) . append ( "OS" , m_aOS ) . append ( "highOrderBitFirst" , m_bHighOrderBitFirst ) . append ( "buffer" , m_nBuffer ) . append ( "bitCount" , m_nBufferedBitCount ) . getToString ( ) ; } }
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 ( &gt;&gt; aNumberOfBits ), going down to the lowest bit (
&gt;&gt; 0 ).

@param aValue
the int containing the bits that should be written to the stream.
@param nNumBits
how many bits of the integer should be written to the stream.
@throws IOException
In case writing to the output stream failed