Netty java getting data from ByteBuf
Asked Answered
D

2

31

How to get a byte array from ByteBuf efficiently in the code below? I need to get the array and then serialize it.

package testingNetty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ServerHandler extends  ChannelInboundHandlerAdapter {
     @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
         System.out.println("Message receive");
         ByteBuf buff = (ByteBuf) msg;
             // There is I need get bytes from buff and make serialization
         byte[] bytes = BuffConvertor.GetBytes(buff);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 
            // Close the connection when an exception is raised.
            cause.printStackTrace();
            ctx.close();
        }

}
Darleen answered 10/10, 2013 at 12:57 Comment(4)
Can you please post the complete code. Also mention the which version of netty you are using?Guidepost
I use 4.0.9 version of NettyDarleen
So what's problem is? What do you mean by bytes massive in the above context?Guidepost
Bytes massive means a byte[] wich contains seriziable objectDarleen
S
90
ByteBuf buf = ...
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);

If you don't want the readerIndex to change:

ByteBuf buf = ...
byte[] bytes = new byte[buf.readableBytes()];
int readerIndex = buf.readerIndex();
buf.getBytes(readerIndex, bytes);

If you want to minimize the memory copy, you can use the backing array of the ByteBuf, if it's available:

ByteBuf buf = ...
byte[] bytes;
int offset;
int length = buf.readableBytes();

if (buf.hasArray()) {
    bytes = buf.array();
    offset = buf.arrayOffset();
} else {
    bytes = new byte[length];
    buf.getBytes(buf.readerIndex(), bytes);
    offset = 0;
}

Please note that you can't simply use buf.array(), because:

  • Not all ByteBufs have backing array. Some are off-heap buffers (i.e. direct memory)
  • Even if a ByteBuf has a backing array (i.e. buf.hasArray() returns true), the following isn't necessarily true because the buffer might be a slice of other buffer or a pooled buffer:
    • buf.array()[0] == buf.getByte(0)
    • buf.array().length == buf.capacity()
Socage answered 11/10, 2013 at 2:29 Comment(8)
@user2132106 would like to know if he can do byte[] ar= buf.array() (if I understand the post correctly).Zuckerman
Updated the answer for him.Socage
@Socage what's the purpose of the offset here?Reinold
I tested with real data: the second way (minimize the memory copy) is wrong. Java platform: Oracle 1.8; netty version: 4.1.58.Final.Lundquist
@Reinold offset tells at which position in the array the first byte starts.Socage
@Socage -- tested the second method and I occasionally got wrong byte array contents. May be need careful consideration and add some protection?Lundquist
@Lundquist May be you are not using offset while reading data from byte array.Omalley
What happens to bytes in third approach if it was initialized in first block i.e. bytes = buf.array() and later buf was released - buf.release() ?Pinstripe
V
6

Another option is ByteBufUtil.getBytes(ByteBuf buf, int start, int length, boolean copy)

See ByteBufUtil

Vinic answered 23/9, 2022 at 6:50 Comment(2)
There is indeed an option to share the array instead of copying it (if possible)Soucy
Thanks @TuomasKiviaho . I updated my answer per your comment.Vinic

© 2022 - 2025 — McMap. All rights reserved.