Create a ByteBuf in Netty 4.0

Dennis picture Dennis · Feb 26, 2013 · Viewed 24.7k times · Source

Two simple questions, which I am not able to solve by reading the documentation:

  1. I have a byte[]
    • How can i convert it to a ByteBuf?
  2. I have a NIO ByteBuffer
    • How can i convert it to a ByteBuf?

Answer

Jon Skeet picture Jon Skeet · Feb 26, 2013

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:

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.