快速 ByteBuffer 到 CharBuffer 或 char[]

发布于 2024-11-06 09:45:09 字数 674 浏览 1 评论 0原文

将 java.nio.ByteBuffer a 转换为(新创建的)CharBuffer b 或 char[] b 的最快方法是什么。

这样做很重要,a[i] == b[i]。这意味着,a[i]a[i+1] 不是一起构成一个值 b[j],而是 >getChar(i) 可以,但值应该是“分散”的。

byte a[] = { 1,2,3, 125,126,127, -128,-127,-126 } // each a byte (which are signed)
char b[] = { 1,2,3, 125,126,127,  128, 129, 130 } // each a char (which are unsigned)

请注意,byte:-128char:128 具有相同的(低 8)位。因此,我认为“最佳”解释将如我上面所述,因为这些位是相同的。

之后,我还需要反之亦然翻译:将 char[]java.nio.CharBuffer 返回到 a 的最有效方法java.nio.ByteBuffer

What is the fastest method to convert a java.nio.ByteBuffer a into a (newly created) CharBuffer b or char[] b.

By doing this it is important, that a[i] == b[i]. This means, that not a[i] and a[i+1] together make up a value b[j], what getChar(i) would do, but the values should be "spread".

byte a[] = { 1,2,3, 125,126,127, -128,-127,-126 } // each a byte (which are signed)
char b[] = { 1,2,3, 125,126,127,  128, 129, 130 } // each a char (which are unsigned)

Note that byte:-128 has the same (lower 8) bits as char:128. Therefore I assume the "best" interpretation would be as I noted it above, because the bits are the same.

After that I also need the vice versa translation: The most efficient way to get a char[] or java.nio.CharBuffer back into a java.nio.ByteBuffer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

感情洁癖 2024-11-13 09:45:09

因此,您想要的是使用 ISO-8859-1 编码进行转换。

我并没有声称任何关于效率的事情,但至少写起来很短:

CharBuffer result = Charset.forName("ISO-8859-1").decode(byteBuffer);

另一个方向是:

ByteBuffer result = Charset.forName("ISO-8859-1").encode(charBuffer);

请根据其他解决方案来衡量这一点。 (公平地说,不应包含 Charset.forName 部分,并且也应该只执行一次,而不是对每个缓冲区再次执行。)

从 Java 7 开始,还有 StandardCharsets 类,具有预实例化的 Charset 实例,因此您可以使用

CharBuffer result = StandardCharsets.ISO_8859_1.decode(byteBuffer);

and

ByteBuffer result = StandardCharsets.ISO_8859_1.encode(charBuffer);

代替。 (这些行的作用与之前的行相同,只是查找更容易,并且不存在输入错误名称的风险,并且不需要捕获不可能的异常。)

So, what you want is to convert using the encoding ISO-8859-1.

I don't claim anything about efficiency, but at least it is quite short to write:

CharBuffer result = Charset.forName("ISO-8859-1").decode(byteBuffer);

The other direction would be:

ByteBuffer result = Charset.forName("ISO-8859-1").encode(charBuffer);

Please measure this against other solutions. (To be fair, the Charset.forName part should not be included, and should also be done only once, not for each buffer again.)

From Java 7 on there also is the StandardCharsets class with pre-instantiated Charset instances, so you can use

CharBuffer result = StandardCharsets.ISO_8859_1.decode(byteBuffer);

and

ByteBuffer result = StandardCharsets.ISO_8859_1.encode(charBuffer);

instead. (These lines do the same as the ones before, just the lookup is easier and there is no risk to mistype the names, and no need to catch the impossible exceptions.)

晨曦慕雪 2024-11-13 09:45:09

我同意@Ishtar 的观点,建议完全避免转换为新结构,并且仅在需要时进行转换。

但是,如果您有堆 ByteBuffer,则可以这样做。

ByteBuffer bb = ...
byte[] array = bb.array();
char[] chars = new char[bb.remaining()];
for (int i = 0; i < chars.length; i++)
    chars[i] = (char) (array[i + bb.position()] & 0xFF);

I would agree with @Ishtar's, suggest to avoid converting to a new structure at all and only convert as you need it.

However if you have a heap ByteBuffer you can do.

ByteBuffer bb = ...
byte[] array = bb.array();
char[] chars = new char[bb.remaining()];
for (int i = 0; i < chars.length; i++)
    chars[i] = (char) (array[i + bb.position()] & 0xFF);
放赐 2024-11-13 09:45:09

除了推迟创建 CharBuffer 之外,您也许无需创建 CharBuffer 也能过得去。
如果使用数据作为字符的代码并不严格需要 CharBuffer 或 char[],只需执行简单的即时转换即可;使用 ByteBuffer.get() (相对或绝对),转换为 char (注意:正如所指出的,不幸的是,您必须显式屏蔽内容;否则值 128-255 将被符号扩展为不正确的值,0xFF80 - 0xFFFF;不需要7 位 ASCII),然后使用它。

Aside from deferring creation of CharBuffer, you may be able to get by without one.
If code that is using data as characters does not strictly need a CharBuffer or char[], just do simple on-the-fly conversion; use ByteBuffer.get() (relative or absolute), convert to char (note: as pointed out, you MUST unfortunately explicitly mask things; otherwise values 128-255 will be sign-extended to incorrect values, 0xFF80 - 0xFFFF; not needed for 7-bit ASCII), and use that.

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