快速 ByteBuffer 到 CharBuffer 或 char[]
将 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:-128
与 char: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,您想要的是使用 ISO-8859-1 编码进行转换。
我并没有声称任何关于效率的事情,但至少写起来很短:
另一个方向是:
请根据其他解决方案来衡量这一点。 (公平地说,不应包含
Charset.forName
部分,并且也应该只执行一次,而不是对每个缓冲区再次执行。)从 Java 7 开始,还有 StandardCharsets 类,具有预实例化的 Charset 实例,因此您可以使用
and
代替。 (这些行的作用与之前的行相同,只是查找更容易,并且不存在输入错误名称的风险,并且不需要捕获不可能的异常。)
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:
The other direction would be:
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
and
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.)
我同意@Ishtar 的观点,建议完全避免转换为新结构,并且仅在需要时进行转换。
但是,如果您有堆 ByteBuffer,则可以这样做。
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.
除了推迟创建 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.