JBBPBitOutputStream extends FilterOutputStream implements JBBPCountableBitStream { @SuppressWarnings("NullableProblems") @Override public void write(final byte[] b, final int off, final int len) throws IOException { if (this.msb0 || this.bitBufferCount != 0) { int i = off; int cnt = len; while (cnt > 0) { this.write(b[i++]); cnt--; } } else { out.write(b, off, len); this.byteCounter += len; } } 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 testWrite() throws Exception { final ByteArrayOutputStream buff = new ByteArrayOutputStream(); final JBBPBitOutputStream out = new JBBPBitOutputStream(buff); for (int i = 0; i < 256; ) { out.write(i++); out.writeBits(i++, JBBPBitNumber.BITS_8); } assertEquals(256, out.getCounter()); final byte[] written = buff.toByteArray(); assertEquals(256, written.length); for (int i = 0; i < 256; i++) { assertEquals(i, written[i] & 0xFF, "Pos " + i); } }