Poison

ByteOrder

Bits 类中通过申请 8 字节的内存,然后填入一个 Long 类型的数字再根据头部字节判断出大端序还是小端序,源码位于 jdk/Bits.java at jdk8-b120:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
}
}