### Question:
BoundedNetworkWritableChannel implements WritableChannel, ByteBufBacked { @Override public WritableChannel put( byte value ) throws MessageTooBigException { checkSize( Byte.BYTES ); delegate.writeByte( value ); return this; } BoundedNetworkWritableChannel( ByteBuf delegate );  BoundedNetworkWritableChannel( ByteBuf delegate, long sizeLimit ); @Override WritableChannel put( byte value ); @Override WritableChannel putShort( short value ); @Override WritableChannel putInt( int value ); @Override WritableChannel putLong( long value ); @Override WritableChannel putFloat( float value ); @Override WritableChannel putDouble( double value ); @Override WritableChannel put( byte[] value, int length ); @Override ByteBuf byteBuf();  }### Answer:
@Test public void shouldRespectSizeLimit() throws Exception { int sizeLimit = 100; BoundedNetworkWritableChannel channel = new BoundedNetworkWritableChannel( Unpooled.buffer(), sizeLimit ); for ( int i = 0; i < sizeLimit; i++ ) { channel.put( (byte) 1 ); } try { channel.put( (byte) 1 ); fail("Should not allow more bytes than what the limit dictates"); } catch ( MessageTooBigException e ) { } }
@Test public void sizeLimitShouldWorkWithArrays() throws Exception { int sizeLimit = 100; BoundedNetworkWritableChannel channel = new BoundedNetworkWritableChannel( Unpooled.buffer(), sizeLimit ); int padding = 10; for ( int i = 0; i < sizeLimit - padding; i++ ) { channel.put( (byte) 0 ); } try { channel.put( new byte[padding * 2], padding * 2 ); fail("Should not allow more bytes than what the limit dictates"); } catch ( MessageTooBigException e ) { } }
@Test public void shouldNotCountBytesAlreadyInBuffer() throws Exception { int sizeLimit = 100; ByteBuf buffer = Unpooled.buffer(); int padding = Long.BYTES; buffer.writeLong( 0 ); BoundedNetworkWritableChannel channel = new BoundedNetworkWritableChannel( buffer, sizeLimit ); for ( int i = 0; i < sizeLimit - padding; i++ ) { channel.put( (byte) 0 ); } for ( int i = 0; i < padding; i++ ) { channel.put( (byte) 0 ); } try { channel.put( (byte) 0 ); fail("Should not allow more bytes than what the limit dictates"); } catch ( MessageTooBigException e ) { } }