当前位置: 首页>>代码示例>>Java>>正文


Java Unsafe类代码示例

本文整理汇总了Java中sun.misc.Unsafe的典型用法代码示例。如果您正苦于以下问题:Java Unsafe类的具体用法?Java Unsafe怎么用?Java Unsafe使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Unsafe类属于sun.misc包,在下文中一共展示了Unsafe类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: run

import sun.misc.Unsafe; //导入依赖的package包/类
@Override
public void run() {
    if (!hasWorkingJavac()) {
        ClassWriter w = new ClassWriter(0);
        w.visit(Opcodes.V1_8, Opcodes.ACC_ABSTRACT | Opcodes.ACC_PUBLIC, "com/sun/tools/javac/code/Scope$WriteableScope", null, "com/sun/tools/javac/code/Scope", null);
        byte[] classData = w.toByteArray();
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            Unsafe unsafe = (Unsafe) theUnsafe.get(null);
            Class scopeClass = Class.forName("com.sun.tools.javac.code.Scope");
            unsafe.defineClass("com.sun.tools.javac.code.Scope$WriteableScope", classData, 0, classData.length, scopeClass.getClassLoader(), scopeClass.getProtectionDomain());
        } catch (Throwable t) {
            //ignore...
            Logger.getLogger(NoJavacHelper.class.getName()).log(Level.FINE, null, t);
        }
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:NoJavacHelper.java

示例2: testCopyByteArrayToByteArray

import sun.misc.Unsafe; //导入依赖的package包/类
private static void testCopyByteArrayToByteArray(Unsafe unsafe) throws Exception {
    System.out.println("Testing copyMemory() for byte[] to byte[]...");
    byte[] b1 = new byte[BUFFER_SIZE];
    byte[] b2 = new byte[BUFFER_SIZE];
    for (int i = 0; i < N; i++) {
        set(b1, 0, BUFFER_SIZE, FILLER);
        set(b2, 0, BUFFER_SIZE, FILLER2);
        int ofs = random.nextInt(BUFFER_SIZE / 2);
        int len = random.nextInt(BUFFER_SIZE / 2);
        int val = random.nextInt(256);
        set(b1, ofs, len, val);
        int ofs2 = random.nextInt(BUFFER_SIZE / 2);
        unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs,
            b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len);
        check(b2, 0, ofs2 - 1, FILLER2);
        check(b2, ofs2, len, val);
        check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:CopyMemory.java

示例3: getUnsafe

import sun.misc.Unsafe; //导入依赖的package包/类
/**
 * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
 * Replace with a simple call to Unsafe.getUnsafe when integrating
 * into a jdk.
 *
 * @return a sun.misc.Unsafe
 */
private static sun.misc.Unsafe getUnsafe() {
    try {
        return sun.misc.Unsafe.getUnsafe();
    } catch (SecurityException tryReflectionInstead) {}
    try {
        return java.security.AccessController.doPrivileged
        (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
            public sun.misc.Unsafe run() throws Exception {
                Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
                for (java.lang.reflect.Field f : k.getDeclaredFields()) {
                    f.setAccessible(true);
                    Object x = f.get(null);
                    if (k.isInstance(x))
                        return k.cast(x);
                }
                throw new NoSuchFieldError("the Unsafe");
            }});
    } catch (java.security.PrivilegedActionException e) {
        throw new RuntimeException("Could not initialize intrinsics",
                                   e.getCause());
    }
}
 
开发者ID:s-store,项目名称:s-store,代码行数:30,代码来源:UnsignedBytes.java

示例4: load

import sun.misc.Unsafe; //导入依赖的package包/类
/**
 * Loads this buffer's content into physical memory.
 *
 * <p> This method makes a best effort to ensure that, when it returns,
 * this buffer's content is resident in physical memory.  Invoking this
 * method may cause some number of page faults and I/O operations to
 * occur. </p>
 *
 * @return  This buffer
 */
public final MappedByteBuffer load() {
    checkMapped();
    if ((address == 0) || (capacity() == 0))
        return this;
    long offset = mappingOffset();
    long length = mappingLength(offset);
    load0(mappingAddress(offset), length);

    // Read a byte from each page to bring it into memory. A checksum
    // is computed as we go along to prevent the compiler from otherwise
    // considering the loop as dead code.
    Unsafe unsafe = Unsafe.getUnsafe();
    int ps = Bits.pageSize();
    int count = Bits.pageCount(length);
    long a = mappingAddress(offset);
    byte x = 0;
    for (int i=0; i<count; i++) {
        x ^= unsafe.getByte(a);
        a += ps;
    }
    if (unused != 0)
        unused = x;

    return this;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:36,代码来源:MappedByteBuffer.java

示例5: asNativeBuffer

import sun.misc.Unsafe; //导入依赖的package包/类
static NativeBuffer asNativeBuffer(String s) {
    int stringLengthInBytes = s.length() << 1;
    int sizeInBytes = stringLengthInBytes + 2;  // char terminator

    // get a native buffer of sufficient size
    NativeBuffer buffer = NativeBuffers.getNativeBufferFromCache(sizeInBytes);
    if (buffer == null) {
        buffer = NativeBuffers.allocNativeBuffer(sizeInBytes);
    } else {
        // buffer already contains the string contents
        if (buffer.owner() == s)
            return buffer;
    }

    // copy into buffer and zero terminate
    char[] chars = s.toCharArray();
    unsafe.copyMemory(chars, Unsafe.ARRAY_CHAR_BASE_OFFSET, null,
        buffer.address(), (long)stringLengthInBytes);
    unsafe.putChar(buffer.address() + stringLengthInBytes, (char)0);
    buffer.setOwner(s);
    return buffer;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:23,代码来源:WindowsNativeDispatcher.java

示例6: asList

import sun.misc.Unsafe; //导入依赖的package包/类
private List<String> asList(long address, int size) {
    List<String> list = new ArrayList<>();
    int start = 0;
    int pos = 0;
    while (pos < size) {
        if (unsafe.getByte(address + pos) == 0) {
            int len = pos - start;
            byte[] value = new byte[len];
            unsafe.copyMemory(null, address+start, value,
                Unsafe.ARRAY_BYTE_BASE_OFFSET, len);
            String s = Util.toString(value);
            if (s.startsWith(USER_NAMESPACE)) {
                s = s.substring(USER_NAMESPACE.length());
                list.add(s);
            }
            start = pos + 1;
        }
        pos++;
    }
    return list;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:22,代码来源:LinuxUserDefinedFileAttributeView.java

示例7: verifyGet

import sun.misc.Unsafe; //导入依赖的package包/类
static boolean verifyGet(long referent_offset, Unsafe unsafe) throws Exception {
  // Access verification
  System.out.println("referent: " + str);
  Object obj = getRef0(ref);
  if (obj != str) {
    System.out.println("FAILED: weakRef.get() " + obj + " != " + str);
    return false;
  }
  obj = getRef1(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(weakRef, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  obj = getRef2(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(abstRef, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  obj = getRef3(unsafe, ref, referent_offset);
  if (obj != str) {
    System.out.println("FAILED: unsafe.getObject(Object, " + referent_offset + ") " + obj + " != " + str);
    return false;
  }
  return true;
}
 
开发者ID:arodchen,项目名称:MaxSim,代码行数:26,代码来源:Test7190310_unsafe.java

示例8: testCopyRawMemoryToRawMemory

import sun.misc.Unsafe; //导入依赖的package包/类
private static void testCopyRawMemoryToRawMemory(Unsafe unsafe) throws Exception {
    System.out.println("Testing copyMemory() for raw memory to raw memory...");
    long b1 = getMemory(BUFFER_SIZE);
    long b2 = getMemory(BUFFER_SIZE);
    for (int i = 0; i < N; i++) {
        set(unsafe, b1, 0, BUFFER_SIZE, FILLER);
        set(unsafe, b2, 0, BUFFER_SIZE, FILLER2);
        int ofs = random.nextInt(BUFFER_SIZE / 2);
        int len = random.nextInt(BUFFER_SIZE / 2);
        int val = random.nextInt(256);
        set(unsafe, b1, ofs, len, val);
        int ofs2 = random.nextInt(BUFFER_SIZE / 2);
        unsafe.copyMemory(null, b1 + ofs,
            null, b2 + ofs2, len);
        check(unsafe, b2, 0, ofs2 - 1, FILLER2);
        check(unsafe, b2, ofs2, len, val);
        check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:CopyMemory.java

示例9: testCopyByteArrayToRawMemory

import sun.misc.Unsafe; //导入依赖的package包/类
private static void testCopyByteArrayToRawMemory(Unsafe unsafe) throws Exception {
    System.out.println("Testing copyMemory() for byte[] to raw memory...");
    byte[] b1 = new byte[BUFFER_SIZE];
    long b2 = getMemory(BUFFER_SIZE);
    for (int i = 0; i < N; i++) {
        set(b1, 0, BUFFER_SIZE, FILLER);
        set(unsafe, b2, 0, BUFFER_SIZE, FILLER2);
        int ofs = random.nextInt(BUFFER_SIZE / 2);
        int len = random.nextInt(BUFFER_SIZE / 2);
        int val = random.nextInt(256);
        set(b1, ofs, len, val);
        int ofs2 = random.nextInt(BUFFER_SIZE / 2);
        unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs,
            null, b2 + ofs2, len);
        check(unsafe, b2, 0, ofs2 - 1, FILLER2);
        check(unsafe, b2, ofs2, len, val);
        check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:CopyMemory.java

示例10: getUnsafe

import sun.misc.Unsafe; //导入依赖的package包/类
/**
 * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. Replace with a simple
 * call to Unsafe.getUnsafe when integrating into a jdk.
 *
 * @return a sun.misc.Unsafe instance if successful
 */
private static sun.misc.Unsafe getUnsafe() {
  try {
    return sun.misc.Unsafe.getUnsafe();
  } catch (SecurityException tryReflectionInstead) {
    // We'll try reflection instead.
  }
  try {
    return java.security.AccessController.doPrivileged(
        new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
          @Override
          public sun.misc.Unsafe run() throws Exception {
            Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
            for (java.lang.reflect.Field f : k.getDeclaredFields()) {
              f.setAccessible(true);
              Object x = f.get(null);
              if (k.isInstance(x)) {
                return k.cast(x);
              }
            }
            throw new NoSuchFieldError("the Unsafe");
          }
        });
  } catch (java.security.PrivilegedActionException e) {
    throw new RuntimeException("Could not initialize intrinsics", e.getCause());
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:33,代码来源:LittleEndianByteArray.java

示例11: test1

import sun.misc.Unsafe; //导入依赖的package包/类
/**
 * 通过反射Constructor获得Unsafe的实例对象
 *
 * @throws Exception
 */
@Test
public void test1() throws Exception {
    Constructor<Unsafe> constructor = Unsafe.class.getDeclaredConstructor();
    constructor.setAccessible(true);
    Unsafe unsafe = constructor.newInstance();
    System.out.println(unsafe);
}
 
开发者ID:lihengming,项目名称:java-codes,代码行数:13,代码来源:UnsafeTest.java

示例12: initUnsafe

import sun.misc.Unsafe; //导入依赖的package包/类
private static Unsafe initUnsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            return (Unsafe) theUnsafe.get(Unsafe.class);
        } catch (Exception e) {
            throw new RuntimeException("exception while trying to get Unsafe", e);
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:SPARCArrayEqualsOp.java

示例13: CustomAtomicInteger

import sun.misc.Unsafe; //导入依赖的package包/类
public CustomAtomicInteger() {
    try {
        //获得Unsafe的构造器
        Constructor<Unsafe> constructor = Unsafe.class.getDeclaredConstructor();
        //突破私有访问权限
        constructor.setAccessible(true);
        //创建示例
        this.unsafe = constructor.newInstance();
        //获得value变量的内存偏移量即内存地址
        offset = unsafe.objectFieldOffset(CustomAtomicInteger.class.getDeclaredField("value"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:lihengming,项目名称:java-codes,代码行数:15,代码来源:CustomAtomicInteger.java

示例14: UnsafeLock

import sun.misc.Unsafe; //导入依赖的package包/类
public UnsafeLock() {
    try {
        //获得Unsafe的构造器
        Constructor<Unsafe> constructor = Unsafe.class.getDeclaredConstructor();
        //突破私有访问权限
        constructor.setAccessible(true);
        //创建示例
        this.unsafe = constructor.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:lihengming,项目名称:java-codes,代码行数:13,代码来源:UnsafeLock.java

示例15: getUnsafe

import sun.misc.Unsafe; //导入依赖的package包/类
private static Unsafe getUnsafe() {
    if (System.getSecurityManager() != null) {
        return new PrivilegedAction<Unsafe>() {
            public Unsafe run() {
                return getUnsafe0();
            }
        }.run();
    }
    return getUnsafe0();
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:11,代码来源:FastConcurrentDirectDeque.java


注:本文中的sun.misc.Unsafe类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。