ByteToMessageDecoder extends ChannelInboundHandlerAdapter { protected ByteBuf internalBuffer() { if (cumulation != null) { return cumulation; } else { return Unpooled.EMPTY_BUFFER; } } protected  ByteToMessageDecoder(); void setSingleDecode(boolean singleDecode); boolean isSingleDecode(); void setCumulator(Cumulator cumulator); @Override final void handlerRemoved(ChannelHandlerContext ctx); @Override void channelRead(ChannelHandlerContext ctx, Object msg); @Override void channelReadComplete(ChannelHandlerContext ctx); @Override void channelInactive(ChannelHandlerContext ctx); static final Cumulator MERGE_CUMULATOR; static final Cumulator COMPOSITE_CUMULATOR; }
@Test public void testInternalBufferClearReadAll() { final ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer().writeBytes(new byte[]{'a'})); EmbeddedChannel channel = new EmbeddedChannel(new ByteToMessageDecoder() { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { ByteBuf byteBuf = internalBuffer(); Assert.assertEquals(1, byteBuf.refCnt()); in.readByte(); ctx.pipeline().remove(this); Assert.assertEquals(0, byteBuf.refCnt()); } }); Assert.assertFalse(channel.writeInbound(buf)); Assert.assertFalse(channel.finish()); }
@Test public void testInternalBufferClearReadPartly() { final ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer().writeBytes(new byte[]{'a', 'b'})); EmbeddedChannel channel = new EmbeddedChannel(new ByteToMessageDecoder() { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { ByteBuf byteBuf = internalBuffer(); Assert.assertEquals(1, byteBuf.refCnt()); in.readByte(); ctx.pipeline().remove(this); Assert.assertEquals(0, byteBuf.refCnt()); } }); Assert.assertTrue(channel.writeInbound(buf)); Assert.assertTrue(channel.finish()); Assert.assertEquals(channel.readInbound(), Unpooled.wrappedBuffer(new byte[] {'b'})); Assert.assertNull(channel.readInbound()); }