Search in sources :

Example 6 with Unsafe

use of sun.misc.Unsafe in project h2o-3 by h2oai.

the class UtilUnsafe method getUnsafe.

/** Fetch the Unsafe.  Use With Caution. */
public static Unsafe getUnsafe() {
    // Not on bootclasspath
    if (UtilUnsafe.class.getClassLoader() == null)
        return Unsafe.getUnsafe();
    try {
        final Field fld = Unsafe.class.getDeclaredField("theUnsafe");
        fld.setAccessible(true);
        return (Unsafe) fld.get(UtilUnsafe.class);
    } catch (Exception e) {
        throw new RuntimeException("Could not obtain access to sun.misc.Unsafe", e);
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 7 with Unsafe

use of sun.misc.Unsafe in project hazelcast by hazelcast.

the class UnsafeHelper method findUnsafe.

private static Unsafe findUnsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        return AccessController.doPrivileged(new PrivilegedAction<Unsafe>() {

            @Override
            public Unsafe run() {
                try {
                    Class<Unsafe> type = Unsafe.class;
                    try {
                        Field field = type.getDeclaredField("theUnsafe");
                        field.setAccessible(true);
                        return type.cast(field.get(type));
                    } catch (Exception e) {
                        for (Field field : type.getDeclaredFields()) {
                            if (type.isAssignableFrom(field.getType())) {
                                field.setAccessible(true);
                                return type.cast(field.get(type));
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new RuntimeException("Unsafe unavailable", e);
                }
                throw new RuntimeException("Unsafe unavailable");
            }
        });
    }
}
Also used : Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Unsafe(sun.misc.Unsafe)

Example 8 with Unsafe

use of sun.misc.Unsafe in project pjson by gerritjvv.

the class StringUtil method loadUnsafe.

private static Unsafe loadUnsafe() {
    try {
        Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
        unsafeField.setAccessible(true);
        return (Unsafe) unsafeField.get(null);
    } catch (NoSuchFieldException e) {
        return null;
    } catch (IllegalAccessException e) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 9 with Unsafe

use of sun.misc.Unsafe in project jdk8u_jdk by JetBrains.

the class VMAnonymousClass method getUnsafe.

private static synchronized Unsafe getUnsafe() {
    try {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        return (Unsafe) f.get(null);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new RuntimeException("Unable to get Unsafe instance.", e);
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 10 with Unsafe

use of sun.misc.Unsafe in project jdk8u_jdk by JetBrains.

the class CopyMemory method main.

public static void main(String[] args) throws Exception {
    Unsafe unsafe = getUnsafe();
    testSetByteArray(unsafe);
    testSetRawMemory(unsafe);
    testCopyByteArrayToByteArray(unsafe);
    testCopyByteArrayToRawMemory(unsafe);
    testCopyRawMemoryToByteArray(unsafe);
    testCopyRawMemoryToRawMemory(unsafe);
    System.out.println("OK");
}
Also used : Unsafe(sun.misc.Unsafe)

Aggregations

Unsafe (sun.misc.Unsafe)35 Field (java.lang.reflect.Field)26 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)5 PrivilegedActionException (java.security.PrivilegedActionException)4 PrivilegedAction (java.security.PrivilegedAction)2 File (java.io.File)1 Options (org.rocksdb.Options)1 RocksDB (org.rocksdb.RocksDB)1 RocksIterator (org.rocksdb.RocksIterator)1 StringAppendOperator (org.rocksdb.StringAppendOperator)1 WriteOptions (org.rocksdb.WriteOptions)1