code: public void writeBits(final int value, final JBBPBitNumber bitNumber) throws IOException {
      if (this.bitBufferCount == 0 && bitNumber == JBBPBitNumber.BITS_8) {
        write(value);
      } else {
        final int initialMask;
        int mask;
        initialMask = 1;
        mask = initialMask << this.bitBufferCount;
  
        int accum = value;
        int i = bitNumber.getBitNumber();
  
        while (i > 0) {
          this.bitBuffer = this.bitBuffer | ((accum & 1) == 0 ? 0 : mask);
          accum >>= 1;
  
          mask = mask << 1;
  
          i--;
          this.bitBufferCount++;
          if (this.bitBufferCount == 8) {
            this.bitBufferCount = 0;
            writeByte(this.bitBuffer);
            mask = initialMask;
            this.bitBuffer = 0;
          }
        }
      }
    }
docs: Write bits into the output stream.
  
  @param value     the value which bits will be written in the output stream
  @param bitNumber number of bits from the value to be written, must be in 1..8
  @throws IOException              it will be thrown for transport errors
  @throws IllegalArgumentException it will be thrown for wrong bit number