将 ByteBuffer 的一部分转换为字符串
我有一个 ByteBuffer ,其中包含由 String.getBytes(charsetName)
,其中“containing”表示字符串包含 ByteBuffer 的 position( )
和 limit()
。
对我来说,取回绳子的最佳方法是什么? (假设我知道编码字符集)有什么比下面更好的(这看起来有点笨拙)
byte[] ba = new byte[bbuf.remaining()];
bbuf.get(ba);
try {
String s = new String(ba, charsetName);
}
catch (UnsupportedEncodingException e) {
/* take appropriate action */
}
I have a ByteBuffer containing bytes that were derived by String.getBytes(charsetName)
, where "containing" means that the string comprises the entire sequence of bytes between the ByteBuffer's position()
and limit()
.
What's the best way for me to get the string back? (assuming I know the encoding charset) Is there anything better than the following (which seems a little clunky)
byte[] ba = new byte[bbuf.remaining()];
bbuf.get(ba);
try {
String s = new String(ba, charsetName);
}
catch (UnsupportedEncodingException e) {
/* take appropriate action */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)