ByteArrayOutputStream 到 CharBuffer

发布于 2024-07-19 05:31:59 字数 293 浏览 2 评论 0原文

我有一个输入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难忘№最初的完美 2024-07-26 05:31:59

您必须提供编码。 类应该如何知道如何转换。 在写入 ByteOutputStream 或完全避免流之前,您不能确定编码吗? 如果不是,您必须做假设,并且可能会失败

提供编码,您可以将字节缓冲区转换为字符缓冲区

Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(yourByteBuffer);

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

Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(yourByteBuffer);
孤独患者 2024-07-26 05:31:59

ByteBuffer.asCharBuffer 为您提供了一个非常字面的两个 bytechar(大端或小端),更新反映在两个缓冲区中。

大概您想通过一些合理的编码从 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 two bytes to char (big or little endian) with updates reflected in both buffers.

Presumably you want to go from bytes to chars 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 a CharBuffer 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)

死开点丶别碍眼 2024-07-26 05:31:59

ByteBuffer.asCharBuffer() 假定字节为 UTF-16。 (我在文档中找不到任何明确说明这一点的内容,但实现只是将字节对视为字符的低字节和高字节。)如果您需要另一种编码,则必须使用不同的方法。

最简单的方法是创建一个 String

CharBuffer document = CharBuffer.wrap(byteOutputStream.toString(encoding));

我知道您说过“试图避免创建新字符串”,但您的代码片段无论如何都分配了一个单独的字节数组(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:

CharBuffer document = CharBuffer.wrap(byteOutputStream.toString(encoding));

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). Because CharBuffer 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文