简体   繁体   中英

Getting string from netty ByteBuf

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

使用提供的方法ByteBuf.toString(Charset)

use ByteBuf.readCharSequence()

example is here:

// here we use utf-8 decoding, you can choose the charset you want
String s = ByteBuf.readCharSequence(length, Charset.forName("utf-8")).toString()

note that ByteBuf.readCharSequence() returns java.lang.CharSequence , use toString() to get String obejct

Take a look at this

From there you can use new String(byte[]) or some Base64 solution (since it's binary data I'm assuming)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM