ByteBuffer
public abstract class ByteBuffer extends Buffer implements Comparable<ByteBuffer> {
boolean bigEndian = true;
boolean nativeByteOrder = (Bits.byteOrder() == ByteOrder.BIG_ENDIAN);
public final ByteBuffer order(ByteOrder bo) {
bigEndian = (bo == ByteOrder.BIG_ENDIAN);
nativeByteOrder = (bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN));
return this;
}
public final ByteOrder order() {
return bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
}
public final ByteBuffer order(ByteOrder bo) {
bigEndian = (bo == ByteOrder.BIG_ENDIAN);
nativeByteOrder =
(bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN));
return this;
}
public static ByteBuffer allocateDirect(int capacity) {
return new DirectByteBuffer(capacity);
}
}
ByteOrder
public final class ByteOrder {
private String name;
private ByteOrder(String name) {
this.name = name;
}
public static final ByteOrder BIG_ENDIAN = new ByteOrder("BIG_ENDIAN");
public static final ByteOrder LITTLE_ENDIAN = new ByteOrder("LITTLE_ENDIAN");
public static ByteOrder nativeOrder() {
return Bits.byteOrder();
}
public String toString() {
return name;
}
}
Bits
class Bits {
static ByteOrder byteOrder() {
if (byteOrder == null)
throw new Error("Unknown byte order");
return byteOrder;
}
static {
long a = unsafe.allocateMemory(8);
try {
unsafe.putLong(a, 0x0102030405060708L);
byte b = unsafe.getByte(a);
switch (b) {
case 0x01: byteOrder = ByteOrder.BIG_ENDIAN; break;
case 0x08: byteOrder = ByteOrder.LITTLE_ENDIAN; break;
default:
assert false;
byteOrder = null;
}
} finally {
unsafe.freeMemory(a);
}
}
}
HeapByteBuffer
class HeapByteBuffer extends ByteBuffer {
public CharBuffer asCharBuffer() {
int size = this.remaining() >> 1;
int off = offset + position();
return (bigEndian
? (CharBuffer)(new ByteBufferAsCharBufferB(this, -1, 0, size, size, off))
: (CharBuffer)(new ByteBufferAsCharBufferL(this, -1, 0, size, size, off)));
}
public ShortBuffer asShortBuffer() {
int size = this.remaining() >> 1;
int off = offset + position();
return (bigEndian
? (ShortBuffer)(new ByteBufferAsShortBufferB(this, -1, 0, size, size, off))
: (ShortBuffer)(new ByteBufferAsShortBufferL(this, -1, 0, size, size, off)));
}
public IntBuffer asIntBuffer() {
int size = this.remaining() >> 2;
int off = offset + position();
return (bigEndian
? (IntBuffer)(new ByteBufferAsIntBufferB(this, -1, 0, size, size, off))
: (IntBuffer)(new ByteBufferAsIntBufferL(this, -1, 0, size, size, off)));
}
public LongBuffer asLongBuffer() {
int size = this.remaining() >> 3;
int off = offset + position();
return (bigEndian
? (LongBuffer)(new ByteBufferAsLongBufferB(this, -1, 0, size, size, off))
: (LongBuffer)(new ByteBufferAsLongBufferL(this, -1, 0, size, size, off)));
}
public FloatBuffer asFloatBuffer() {
int size = this.remaining() >> 2;
int off = offset + position();
return (bigEndian
? (FloatBuffer)(new ByteBufferAsFloatBufferB(this, -1, 0, size, size, off))
: (FloatBuffer)(new ByteBufferAsFloatBufferL(this, -1, 0, size, size, off)));
}
public DoubleBuffer asDoubleBuffer() {
int size = this.remaining() >> 3;
int off = offset + position();
return (bigEndian
? (DoubleBuffer)(new ByteBufferAsDoubleBufferB(this, -1, 0, size, size, off))
: (DoubleBuffer)(new ByteBufferAsDoubleBufferL(this, -1, 0, size, size, off)));
}
/**
* 以多字节数据类型的形式存取 byte 数据组
*/
public int getInt() {
return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian);
}
public int getInt(int i) {
return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian);
}
public ByteBuffer putInt(int x) {
Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian);
return this;
}
public ByteBuffer putInt(int i, int x) {
Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian);
return this;
}
}