This class may be removed in the future. Consider the replacements in JEP 193 'Variable Handles': http://openjdk.java.net/jeps/193
public static int arrayBaseOffset( Class klass ) { return unsafe.arrayBaseOffset( klass ); }
/** * Returns the offset of the first element in the storage allocation of a given array class. * * @param cls Class. * @return the offset of the first element in the storage allocation of a given array class. */ public static int arrayBaseOffset(Class cls) { return UNSAFE.arrayBaseOffset(cls); }
@Override int arrayHeaderSize() { return UnsafeAccess.theUnsafe.arrayBaseOffset(byte[].class); }
/** * Reports the offset of the first element in the storage allocation of a * given array class. */ public static int arrayBaseOffset(Class<?> clazz) { return unsafe != null ? unsafe.arrayBaseOffset(clazz) : -1; }
public final int arrayBaseOffset(Class<?> clazz) { return unsafe.arrayBaseOffset(clazz); }
public int arrayBaseOffset(Class c) { return this.unsafe.arrayBaseOffset(c); }
/** * Reports the offset of the first element in the storage allocation of a * given array class. */ public static int arrayBaseOffset(Class<?> clazz) { return unsafe != null ? unsafe.arrayBaseOffset(clazz) : -1; }
int testInt = unsafe.getInt(test, (long) unsafe.arrayBaseOffset(byte[].class)); if (testInt == 0xcafebabe) { if (BIG_ENDIAN) {
int testInt = unsafe.getInt(test, (long) unsafe.arrayBaseOffset(byte[].class)); if (testInt == 0xcafebabe) { if (BIG_ENDIAN) {
long baseOffset = unsafe.arrayBaseOffset(Object[].class); int addressSize = unsafe.addressSize(); long objectAddress;
System.out.print(label + ": 0x"); long last = 0; int offset = unsafe.arrayBaseOffset(objects.getClass()); int scale = unsafe.arrayIndexScale(objects.getClass()); switch (scale) {
int base = VMSupport.U.arrayBaseOffset(Class.forName(data.arrayClass())); int scale = VMSupport.U.arrayIndexScale(Class.forName(data.arrayClass()));
import sun.misc.Unsafe; import java.lang.reflect.Field; public class Main { public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InstantiationException { /* too lazy to run with VM args, use Reflection */ Field f = Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); /* get array address */ Unsafe unsafe = (Unsafe)f.get(null); byte four_bytes[] = {25, 25, 25, 25}; Object trash[] = new Object[] { four_bytes }; long base_offset_bytes = unsafe.arrayBaseOffset(Object[].class); long four_bytes_address = unsafe.getLong(trash, base_offset_bytes); // <- this is it long ints_addr = unsafe.allocateMemory(16); // allocate 4 * 4 bytes, i.e. 4 ints unsafe.copyMemory(four_bytes_address + base_offset_bytes, ints_addr, 4); // copy all four bytes for(int i = 0; i < 4; i++) { System.out.println(unsafe.getInt(ints_addr + i)); //run through entire allocated int[], // get some intestines } System.out.println("*****************************"); for(int i = 0; i < 16; i++) { System.out.println(unsafe.getByte(ints_addr + i)); //run through entire allocated int[], // get some intestines } } }
if (obj.getClass().isArray()) { Class<?> klazz = obj.getClass(); int base = UNSAFE.arrayBaseOffset(klazz); int scale = UNSAFE.arrayIndexScale(klazz); long size = base + (scale * Array.getLength(obj));
/** * Get the base offset for byte arrays, or {@code -1} if {@code sun.misc.Unsafe} is not available. */ private static int byteArrayBaseOffset() { return HAS_UNSAFE_ARRAY_OPERATIONS ? UNSAFE.arrayBaseOffset(byte[].class) : -1; }
/** * Get the base offset for byte arrays, or {@code -1} if {@code sun.misc.Unsafe} is not available. */ private static int byteArrayBaseOffset() { return HAS_UNSAFE_ARRAY_OPERATIONS ? UNSAFE.arrayBaseOffset(byte[].class) : -1; }
/** * Reports the offset of the first element in the storage allocation of a * given array class. */ public static int arrayBaseOffset(Class<?> clazz) { return unsafe != null ? unsafe.arrayBaseOffset(clazz) : -1; }
@Override public byte[] get(long pointer, final int length) { final byte[] result = new byte[length]; unsafe.copyMemory(null, pointer, result, unsafe.arrayBaseOffset(byte[].class), length); return result; }
public static Object fromAddress(long address) { Object[] array = new Object[]{null}; long baseOffset = getUnsafe().arrayBaseOffset(Object[].class); getUnsafe().putLong(array, baseOffset, address); return array[0]; }
public static long toAddress(Object obj) { Object[] array = new Object[]{obj}; long baseOffset = getUnsafe().arrayBaseOffset(Object[].class); return normalize(getUnsafe().getInt(array, baseOffset)); }