ByteBuffer to CharBuffer

Jonas Konrad me at yawk.at
Mon Feb 6 13:35:05 UTC 2017


 From what I can tell, asCharBuffer behaves exactly like the other 
as*Buffer methods, meaning every char in the CharBuffer is represented 
by two bytes in the original ByteBuffer (big- or little-endian depending 
on ByteOrder of the ByteBuffer). This means bb.asCharBuffer().get() 
behaves like bb.getChar(). asCharBuffer does *not* decode using any 
Charset (not the platform charset either), it just interprets the bytes 
as 16-bit unsigned integers which happen to be the char type in java. I 
guess this technically counts as UTF-16? It doesn't do validity checks 
either, though.

If you wish to decode a ByteBuffer, you can use the CharsetDecoder API 
directly. I do not believe an asCharBuffer-like method which decodes on 
the fly is possible generally for all charsets, because decoding is not 
necessarily possible in a random-access buffer due to dynamic-width 
encoding.

- Jonas Konrad

On 02/06/2017 11:57 AM, Jay wrote:
> java.nio.ByteBuffer provides a method asCharBuffer() which returns a
> "view" of the ByteBuffer as a character buffer. It however does not
> take any arguments. And there is no mention in the Javadocs as to how
> it converts from bytes to chars.
>
> 1. There should be a method ByteBuffer.asCharBuffer(CharSet) and/or
> ByteBuffer.asCharBuffer(String charSet) which allows the user to
> explicitly specify the character set to be used for conversion.
>
> 2. The method ByteBuffer.asCharBuffer() should mention that this
> method uses the platform default charset for bytes-to-chars
> conversion.
>
> I can provide a patch with these two changes. What do you guys think?
>


More information about the core-libs-dev mailing list