BoundedBitChannel implements BitChannel { public void write(boolean value) throws IOException { if (written + 1 <= maxBits) { channel.write(value); written += 1; } else { throw new IOException(OVERRUN_MESSAGE); } } BoundedBitChannel(@Nonnull BitChannel channel, @Nonnegative long maxBits); void write(boolean value); void write(int nrbits, byte value); void write(int nrbits, int value, ByteOrder byteOrder); void write(int nrbits, long value, ByteOrder byteOrder); void write(int nrbits, short value, ByteOrder byteOrder); void write(byte[] src, int offset, int length); long write(ByteBuffer buffer); int getRelativeBitPos(); void close();  }
@Test public void shouldAccept9BitsAsInt() throws IOException { boundedChannel.write(9, Integer.MAX_VALUE, ByteOrder.BigEndian); verify(channel).write(9, Integer.MAX_VALUE, ByteOrder.BigEndian); verifyNoMoreInteractions(channel); }
@Test(expected = IOException.class) public void shouldReject10BitsAsInt() throws IOException { boundedChannel.write(10, Integer.MAX_VALUE, ByteOrder.BigEndian); }
@Test public void shouldAccept9BitsAsLong() throws IOException { boundedChannel.write(9, Long.MAX_VALUE, ByteOrder.BigEndian); verify(channel).write(9, Long.MAX_VALUE, ByteOrder.BigEndian); verifyNoMoreInteractions(channel); }
@Test(expected = IOException.class) public void shouldReject10BitsAsLong() throws IOException { boundedChannel.write(10, Long.MAX_VALUE, ByteOrder.BigEndian); }
@Test public void shouldAccept9BitsAsShort() throws IOException { boundedChannel.write(9, Short.MAX_VALUE, ByteOrder.BigEndian); verify(channel).write(9, Short.MAX_VALUE, ByteOrder.BigEndian); verifyNoMoreInteractions(channel); }
@Test(expected = IOException.class) public void shouldReject10BitsAsShort() throws IOException { boundedChannel.write(10, Short.MAX_VALUE, ByteOrder.BigEndian); }
@Test public void shouldAccept9BitsAsBytesAccummulated() throws IOException { boundedChannel.write(8, Byte.MAX_VALUE, ByteOrder.BigEndian); boundedChannel.write(1, Byte.MAX_VALUE, ByteOrder.BigEndian); verify(channel).write(8, Byte.MAX_VALUE, ByteOrder.BigEndian); verify(channel).write(1, Byte.MAX_VALUE, ByteOrder.BigEndian); verifyNoMoreInteractions(channel); }
@Test(expected = IOException.class) public void shouldReject10BitsAsBytesAccummulated() throws IOException { boundedChannel.write(8, Byte.MAX_VALUE, ByteOrder.BigEndian); boundedChannel.write(2, Byte.MAX_VALUE, ByteOrder.BigEndian); }