class OutputStreamBitChannel implements BitChannel, Closeable {

    public void write(boolean value) throws IOException {
        if (value) {
            buffer = (byte) (0xff & ((buffer << 1) | 0x01));
        } else {
            buffer = (byte) (0xff & (buffer << 1));
        }
        if (++bitPos == 8) {
            bitPos = 0;
            out.write(buffer);
            buffer = 0;
        }
    }

}

class OutputStreamBitChannelTest {

    @Mock
    private OutputStream out;

    @Test
    public void shouldAcceptFullBytes() throws IOException {

        OutputStreamBitChannel channel = new OutputStreamBitChannel(out);
        channel.write(8, (byte) 32);
        verify(out).write((byte) 32);
        verifyNoMoreInteractions(out);
    }

}