Fast ByteBuffer to CharBuffer or char[]

[亡魂溺海] 提交于 2019-12-03 15:41:18

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

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);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!