本文整理匯總了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);
}
}
}
示例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);
}
}
示例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());
}
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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());
}
}
示例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);
}
示例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);
}
}
}
示例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();
}
}
示例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();
}
}
示例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();
}