11

I came across this query: Create a ByteBuf in Netty 4.0 about conversion from byte[] to ByteBuf and ByteBuffer to ByteBuf. I was curious to know about the conversion the other way:

io.netty.buffer.ByteBuf to java.nio.ByteBuffer

and how to do it efficiently, with minimal/no copying? I did some reading and with some trial and error I found this inefficient way of converting it (with two copies):

// io.netty.handler.codec.http.FullHttpRequest fullHttpRequest;
ByteBuf conByteBuf = fullHttpRequest.content ();                  
int numReadBytes = conByteBuf.readableBytes ();
conBytes = new byte[numReadBytes];
conByteBuf .readBytes (conBytes);                                // First Copy
ByteBuffer conByteBuffer = ByteBuffer.allocate (conBytes.length);
conByteBuffer.put (conByteBuf);                                  // Second Copy

My question is, can we avoid one or both the copies and make the internal buffer of ByteBuffer to use the internal buffer of ByteBuf.

Thanks!

Community
  • 1
  • 1
Corehacker
  • 267
  • 1
  • 2
  • 7

3 Answers3

12

You should be able to use ByteBuf.nioBuffers(). Which will return a view of the ByteBuf as an array of ByteBuffer objects.

In most cases this array will only have one element, but in some of the more complicated implementations of ByteBuf there may be multiple underlying ByteBuffer objects and ByteBuf.nioBuffers() can return them as-is instead of merging them as would a call to ByteBuf.nioBuffer().

You can tell ahead of time what the array length will be by using ByteBuf.nioBufferCount()

Dev
  • 11,919
  • 3
  • 40
  • 53
  • This doesn't address when it is backed by a byte[] – ruckc Mar 26 '15 at 01:09
  • @Dev, When you use `ByteBuf.nioBuffers()` or `ByteBuf.nioBuffer()` to return a view of the ByteBuf as an array of ByteBuffer objects or a single ByteBuffer object, what happens if you were to release the ByteBuf? It appears that problems will occur with the created ByteBuffer object when this happens since the ByteBuf and ByteBuffer share the underlying data. – mcarlin Jun 15 '16 at 00:35
  • So `ByteBuf.nioBuffer()` automatically merges them into one? I could just call this instead of iterating through the array in `nioBuffers()`? – Dmitry Minkovsky Jan 21 '19 at 17:34
7

You can at least use ByteBuffer.wrap() to avoid the second copying.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
2

Not particularly efficient but doing the trick:

public static ByteBuffer toNioBuffer(ByteBuf buffer) {
    if (buffer.isDirect()) {
        return buffer.nioBuffer();
    }
    final byte[] bytes = new byte[buffer.readableBytes()];
    buffer.getBytes(buffer.readerIndex(), bytes);
    return ByteBuffer.wrap(bytes);
}
Hbf
  • 3,074
  • 3
  • 23
  • 32