Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty java getting data from ByteBuf

Tags:

java

netty

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();
        }

}
like image 356
NiceTheo Avatar asked Oct 10 '13 12:10

NiceTheo


People also ask

What is Bytebuf in Netty?

A random and sequential accessible sequence of zero or more bytes (octets). This interface provides an abstract view for one or more primitive byte arrays ( byte[] ) and NIO buffers.

What is ByteBuf in Java?

ByteBuffer is among several buffers provided by Java NIO. Its just a container or holding tank to read data from or write data to. Above behavior is achieved by allocating a direct buffer using allocateDirect() API on Buffer. Java Documentation of Byte Buffer has useful information.

What is Netty buffer?

buffer Description. Abstraction of a byte buffer - the fundamental data structure to represent a low-level binary and text message. Netty uses its own buffer API instead of NIO ByteBuffer to represent a sequence of bytes. This approach has significant advantage over using ByteBuffer .


1 Answers

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()
like image 57
trustin Avatar answered Sep 19 '22 01:09

trustin