开发者

Convert a stream of bytes to a String (UTF-8 encoded)

开发者 https://www.devze.com 2023-03-17 23:15 出处:网络
I have a stream of bytes (given by a BufferedInputStream) which represents a sequence of UTF-8 strings. The protocol is a little crappy becauses it uses 0xFF between commands, which implies that the w

I have a stream of bytes (given by a BufferedInputStream) which represents a sequence of UTF-8 strings. The protocol is a little crappy becauses it uses 0xFF between commands, which implies that the whole stream is not a valid UTF-8 string, only the parts between the 0xFF are.

How can I extract those UTF-8 strings.

I don’t think BufferedReader would work, because the 0xFF will mess 开发者_C百科up everything and I have found no byte array to UTF-8 string conversion function.


byte[] to String

So would be new String(bytes, "UTF-8");

You would need to filter the Stream beforehand though.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号