开发者

What Charset does ByteBuffer.asCharBuffer() use?

开发者 https://www.devze.com 2023-03-21 04:05 出处:网络
What Charset does ByteBuffer.asCharBuffer() use? It seems to convert 3 bytes to one character on my system.

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

  1. X-Buffer.java.template maps ByteBuffer.allocate() to Heap-X-Buffer.java.template
  2. Heap-X-Buffer.java.template maps ByteBuffer.asCharBuffer() to ByteBufferAs-X-Buffer.java.template
  3. ByteBuffer.asCharBuffer().toString() invokes CharBuffer.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.

0

精彩评论

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

关注公众号