JBBPBitOutputStream extends FilterOutputStream implements JBBPCountableBitStream { 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; } } } } JBBPBitOutputStream(final OutputStream out);  JBBPBitOutputStream(final OutputStream out, final JBBPBitOrder order); @Override JBBPBitOrder getBitOrder(); void writeShort(final int value, final JBBPByteOrder byteOrder); void writeInt(final int value, final JBBPByteOrder byteOrder); void writeFloat(final float value, final JBBPByteOrder byteOrder); void writeLong(final long value, final JBBPByteOrder byteOrder); void writeDouble(final double value, final JBBPByteOrder byteOrder); @Override long getCounter(); @Override int getBitBuffer(); @Override int getBufferedBitsNumber(); @Override void flush(); @SuppressWarnings("NullableProblems") @Override void write(final byte[] b, final int off, final int len); @SuppressWarnings("NullableProblems") @Override void write(final byte[] b); void writeBits(final int value, final JBBPBitNumber bitNumber); void align(final long alignByteNumber); @Override void close(); @Override void write(final int value); void writeBytes(final byte[] array, final int length, final JBBPByteOrder byteOrder); @Override void resetCounter(); void writeString(final String value, final JBBPByteOrder order); void writeStringArray(final String[] value, final JBBPByteOrder order);  }
@Test public void testWriteBit_ErrorForZeroSize() throws Exception { assertThrows(IllegalArgumentException.class, () -> new JBBPBitOutputStream(new ByteArrayOutputStream()).writeBits(4, JBBPBitNumber.decode(0))); }
@Test public void testWriteBit_ErrorForNegativeSize() throws Exception { assertThrows(IllegalArgumentException.class, () -> new JBBPBitOutputStream(new ByteArrayOutputStream()).writeBits(4, JBBPBitNumber.decode(-1))); }
@Test public void testWriteBit_ErrorForTooBigSize() throws Exception { assertThrows(IllegalArgumentException.class, () -> new JBBPBitOutputStream(new ByteArrayOutputStream()).writeBits(4, JBBPBitNumber.decode(9))); }