ByteBuffer.asCharBuffer()
assumes that the bytes are UTF-16. (I can't find anything in the docs that says this explicitly, but the implementation just treats pairs of bytes as the low and high bytes of chars.) If you need another encoding you'll have to use a different approach.
The simplest approach is to just create a String
:
CharBuffer document = CharBuffer.wrap(byteOutputStream.toString(encoding));
I know you said you were "trying to avoid creating a new string", but your code snippet was allocating a separate byte array anyway (ByteArrayOutputStream.toByteArray()
"Creates a newly allocated byte array" according to the docs). Because CharBuffer
supports random access and many encodings (notably UTF-8) are variable-width, it's probably best to just convert the whole thing to chars upfront anyway.
If you really just want streaming access (as opposed to random access) to the characters then perhaps CharBuffer
isn't the best interface for the underlying code to accept.