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

只谈情不闲聊 提交于 2019-12-01 15:56:06

问题


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
}

回答1:


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

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




回答2:


You can create a ByteBuf from a String with Unpooled.copiedBuffer(String, Charset).



来源:https://stackoverflow.com/questions/40289204/how-to-convert-a-netty-bytebuf-to-a-string-and-vice-versa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!