开发者

Fast ByteBuffer to CharBuffer or char[]

开发者 https://www.devze.com 2023-03-03 17:55 出处:网络
What is the fastest m开发者_运维技巧ethod to convert a java.nio.ByteBuffer a into a (newly created) CharBuffer b or char[] b.

What is the fastest m开发者_运维技巧ethod 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号