Fast ByteBuffer to CharBuffer or char[]

towi picture towi · May 9, 2011 · Viewed 16.2k times · Source

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.

Answer

Paŭlo Ebermann picture Paŭlo Ebermann · May 9, 2011

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