nettynetty4

What is the difference between the ByteBuf retrieved from Context versus any other ByteBuf


I found out that using ByteBuf derived from ChannelHandlerContext behaves exactly the same as when using ByteBuf created via Unpooled for example.

That is, both of these two works:

ctx.write(ctx.alloc().buffer().writeBytes("Hello World".getBytes()))
ctx.write(Unpooled.wrappedBuffer("Hello World".getBytes()))

Is there any difference in the behaviour when ByteBuf retrieved from ctx.alloc().buffer() is used versus any other means of creating a BytBuf?


Solution

  • The ChannelHandlerContext uses the ByteBufAllocator that is configured for the Channel. Which is by default using the PooledByteBufAllocator which means buffers are pooled for performance reasons.