Getting string from netty ByteBuf

aries picture aries · Jul 3, 2016 · Viewed 11.6k times · Source

How can I get the string from a netty ByteBuf? As of now I am able to get it character by character. Is there a way to get the string object directly?

// message is of type ByteBuf
for (int i = 0; i < message.capacity(); i++) {
    byte b = message.payload().getByte(i);
    System.out.print((char) b);
}

Answer

mileippert picture mileippert · Jul 3, 2016

Use the provided method ByteBuf.toString(Charset) .