How to convert a netty ByteBuf to a String and vice versa

Kaleb Blue picture Kaleb Blue · Oct 27, 2016 · Viewed 7.8k times · Source

Is there a way to convert a netty ByteBuf to a String and vice versa?

public String toString(ByteBuf b){

 //return b enconded to a String
}

public Bytebuf ToByteBuff(String s){

  //return s decoded to Bytebuf
}

Answer

Chris O'Toole picture Chris O'Toole · Oct 27, 2016

You can use ByteBuf.toString(Charset) to convert to string.

You can use String.getBytes(Charset) and Unpooled.wrappedBuffer(byte[]) to convert to ByteBuf.