What Charset does ByteBuffer.asCharBuffer() use? It seems to convert 3 bytes to one character on my system.
On a related note, how does CharsetDecoder relate to ByteBuffer.asCharBuffer()?
UPDATE: With respect to what implementation of ByteBuffer I am using, I am invoking ByteBuffer.allocate(1024).asCharBuffer()
. I can't comment on what implementation gets used 开发者_如何学Cunder the hood.
For the first question - I believe it uses native character encoding of Java (UTF-16).
As I understand it, it doesn't use anything. It just assumes it is already correctly decoded as a string for Java, which means UTF-16. This can be shown by looking at the source for the HeapByteBuffer, where the returned charbuffer finally calls (little endian version):
static private char makeChar(byte b1, byte b0) {
return (char)((b1 << 8) | (b0 & 0xff));
}
So the only thing that is handled here is the endianness for the rest you're responsible. Which also means it's usually much more useful to use the Decoder class where you can specify the encoding.
Looking at jdk7, jdk/src/share/classes/java/nio
X-Buffer.java.template
mapsByteBuffer.allocate()
toHeap-X-Buffer.java.template
Heap-X-Buffer.java.template
mapsByteBuffer.asCharBuffer()
toByteBufferAs-X-Buffer.java.template
ByteBuffer.asCharBuffer().toString()
invokesCharBuffer.put(CharBuffer)
but I can't figure out where this leads
Eventually this probably leads to Bits.makeChar()
which is defined as:
static private char makeChar(byte b1, byte b0) {
return (char)((b1 << 8) | (b0 & 0xff));
}
but I can't figure out how.
精彩评论