ByteArrayOutputStream 到 CharBuffer
我有一个输入 ByteArrayOutputStream,需要将其转换为 CharBuffer。
我试图避免创建一个新字符串。 有没有办法做到这一点。
我试图执行以下操作,但我没有字符串的编码,因此下面的代码将不起作用(无效输出)。
ByteBuffer byteBuffer = ByteBuffer.wrap(byteOutputStream.toByteArray());
CharBuffer document = byteBuffer.asCharBuffer();
I have an input ByteArrayOutputStream and need to convert that to a CharBuffer.
I was trying to avoid creating a new string. Is there anyway to do this.
I was trying to do the following, but I don't have the encoding for the string so the code below will not work (invalid output).
ByteBuffer byteBuffer = ByteBuffer.wrap(byteOutputStream.toByteArray());
CharBuffer document = byteBuffer.asCharBuffer();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须提供编码。 类应该如何知道如何转换。 在写入 ByteOutputStream 或完全避免流之前,您不能确定编码吗? 如果不是,您必须做假设,并且可能会失败
提供编码,您可以将字节缓冲区转换为字符缓冲区
You have to provide an encoding. How should the class know how to convert. Can't you determine the encoding before writing to a ByteOutputStream or avoiding the stream completely. If not you have to do assumptions and might fail
Providing an encoding you can convert the byte buffer to a char buffer
ByteBuffer.asCharBuffer
为您提供了一个非常字面的两个byte
到char
(大端或小端),更新反映在两个缓冲区中。大概您想通过一些合理的编码从
byte
转换为char
。 有很多方法可以做到这一点。 例如[new String(byte[], String编码)][1]。 大多数情况下,他们会通过 CharsetEncoder 以某种形式。 然后应该可以直接从那里获取CharBuffer
。CharBuffer
是一种相当低级的东西。 你确定这真的是你想要的吗?[1]: http://file:///C:/Users/tackline/sun/docs/api/java/lang/String.html#String(byte[], java.lang.String)
ByteBuffer.asCharBuffer
gives you a very literal twobyte
s tochar
(big or little endian) with updates reflected in both buffers.Presumably you want to go from
byte
s tochar
s through some reasonable encoding. There are many ways to do that. For instance [new String(byte[], String encoding)][1]. Mostly they will go through CharsetEncoder in some form. Then it should be straightforard to get aCharBuffer
from there.CharBuffer
is a quite low-level sort of thing. Are you sure that is really what you want?[1]: http://file:///C:/Users/tackline/sun/docs/api/java/lang/String.html#String(byte[], java.lang.String)
ByteBuffer.asCharBuffer()
假定字节为 UTF-16。 (我在文档中找不到任何明确说明这一点的内容,但实现只是将字节对视为字符的低字节和高字节。)如果您需要另一种编码,则必须使用不同的方法。最简单的方法是创建一个
String
:我知道您说过“试图避免创建新字符串”,但您的代码片段无论如何都分配了一个单独的字节数组(
ByteArrayOutputStream. toByteArray()
根据文档“创建一个新分配的字节数组”)。 由于CharBuffer
支持随机访问,并且许多编码(尤其是 UTF-8)都是可变宽度的,因此最好将整个内容预先转换为字符。如果您确实只想对字符进行流式访问(而不是随机访问),那么
CharBuffer
可能不是底层代码接受的最佳接口。ByteBuffer.asCharBuffer()
assumes that the bytes are UTF-16. (I can't find anything in the docs that says this explicitly, but the implementation just treats pairs of bytes as the low and high bytes of chars.) If you need another encoding you'll have to use a different approach.The simplest approach is to just create a
String
:I know you said you were "trying to avoid creating a new string", but your code snippet was allocating a separate byte array anyway (
ByteArrayOutputStream.toByteArray()
"Creates a newly allocated byte array" according to the docs). BecauseCharBuffer
supports random access and many encodings (notably UTF-8) are variable-width, it's probably best to just convert the whole thing to chars upfront anyway.If you really just want streaming access (as opposed to random access) to the characters then perhaps
CharBuffer
isn't the best interface for the underlying code to accept.