Create a ByteBuf in Netty 4.0

The documentation seems pretty clear to me:

Creation of a buffer

It is recommended to create a new buffer using the helper methods in Unpooled rather than calling an individual implementation’s constructor.

Then in Unpooled, you’ve got options of wrapping or copying. For example:

  • Unpooled.copiedBuffer(ByteBuffer)
  • Unpooled.copiedBuffer(byte[])
  • Unpooled.wrappedBuffer(ByteBuffer)
  • Unpooled.wrappedBuffer(byte[])

Choose whichever method is appropriate based on whether you want changes made in the returned ByteBuf to be passed through to the original byte array/buffer.

Leave a Comment