I came across this query: Create ByteBuf in Netty 4.0 about converting from byte [] to ByteBuf and ByteBuffer to ByteBuf. I was curious to learn about the treatment differently:
io.netty.buffer.ByteBuf to java.nio.ByteBuffer
and how to do it efficiently, with minimal / no copying? I read a little, and with some trial and error, I found this inefficient way to convert 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 copies and make an internal ByteBuffer buffer to use the internal ByteBuf buffer.
Thanks!
source share