LengthPrefixedCodec implements ProtocolCodec { public Object read() throws IOException { while(true) { if( read_buffer.remaining()!=0 ) { int count = read_channel.read(read_buffer); if (count == -1) { throw new EOFException("Peer disconnected"); } else if (count == 0) { return null; } read_counter += count; } else { read_buffer.flip(); if( read_buffer.capacity() == 4 ) { int size = read_buffer.getInt(0); if( size < 4 ) { throw new ProtocolException("Expecting a size greater than 3"); } else if( size > MAX_PACKET_SIZE ) { throw new ProtocolException("Packet length was declared as " + size + " but at most " + MAX_PACKET_SIZE + "is allowed. You can configure this limit with the system property aries.fastbin.max.packet.bytes"); } if( size == 4 ) { Buffer rc = new Buffer(read_buffer); read_buffer = ByteBuffer.allocate(4); return rc; } else { ByteBuffer next = ByteBuffer.allocate(size); next.putInt(size); read_buffer = next; } } else { Buffer rc = new Buffer(read_buffer); read_buffer = ByteBuffer.allocate(4); return rc; } } } }  boolean full(); void setWritableByteChannel(WritableByteChannel channel); BufferState write(Object value); BufferState flush(); long getWriteCounter(); void setReadableByteChannel(ReadableByteChannel channel); Object read(); long getReadCounter();  }
@Test(expected=ProtocolException.class) public void testReadEvilPackage() throws Exception { expect(readableByteChannel.read(EasyMock.anyObject())).andAnswer(new IAnswer<Integer>() { @Override public Integer answer() throws Throwable { ByteBuffer buffer = (ByteBuffer)EasyMock.getCurrentArguments()[0]; buffer.putInt(Integer.MAX_VALUE-1); return 1; } }); replay(readableByteChannel); codec.read(); }