package com.example.core.cas;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.util.Arrays;
public class UseUnsafe {
private static int byteArrayBaseOffset;
public static void main(String[] args) throws NoSuchFieldException,SecurityException,IllegalArgumentException,IllegalAccessException{
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe UNSAFE = (Unsafe) theUnsafe.get(null);
System.out.println(UNSAFE);
byte[] data = new byte[10];
System.out.println(Arrays.toString(data));
byteArrayBaseOffset = UNSAFE.arrayBaseOffset(byte[].class);
System.out.println("byte[]字节数组的第一个元素的便宜地址:" + byteArrayBaseOffset);
UNSAFE.putByte(data,byteArrayBaseOffset,(byte)1);
UNSAFE.putByte(data,byteArrayBaseOffset+6,(byte)8);
System.out.println(Arrays.toString(data));
}
}