目录
简介
DirectCharBufferU和DirectByteBuffer基本没啥区别都是先定位一个address,然后根据索引i,对对应的地址进行操作,不同的是char是address + i << 1,byte是address + i << 0。
其余的就是把对byte操作变成对char进行操作。
DirectCharBufferS和DirectCharBufferU也基本没啥区别,就是读取和写入char时,要调用Bits.swap((x)),将char字符的二进制进行翻转。
所以就不对代码进行注释和翻译,直接看代码即可
DirectCharBufferU
package java.nio;
import java.io.FileDescriptor;
import sun.misc.Cleaner;
import sun.misc.Unsafe;
import sun.misc.VM;
import sun.nio.ch.DirectBuffer;
class DirectCharBufferU
extends CharBuffer
implements DirectBuffer {
// Cached unsafe-access object
protected static final Unsafe unsafe = Bits.unsafe();
// Cached array base offset
private static final long arrayBaseOffset = (long) unsafe.arrayBaseOffset(char[].class);
// Cached unaligned-access capability
protected static final boolean unaligned = Bits.unaligned();
// Base address, used in all indexing calculations
// NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
// protected long address;
// An object attached to this buffer. If this buffer is a view of another
// buffer then we use this field to keep a reference to that buffer to
// ensure that its memory isn't freed before we are done with it.
private final Object att;
public Object attachment() {
return att;
}
public Cleaner cleaner() {
return null;
}
// For duplicates and slices
//
DirectCharBufferU(DirectBuffer db, // package-private
int mark, int pos, int lim, int cap, int off) {
super(mark, pos, lim, cap);
address = db.address() + off;
att = db;
}
public CharBuffer slice() {
int pos = this.position();
int lim = this.limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
int off = (pos << 1);
assert (off >= 0);
return new DirectCharBufferU(this, -1, 0, rem, rem, off);
}
public CharBuffer duplicate() {
return new DirectCharBufferU(this, this.markValue(), this.position(), this.limit(),