Created
June 28, 2013 21:50
-
-
Save burtonator/5888432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.netty.buffer; | |
import sun.misc.Unsafe; | |
import java.lang.reflect.Field; | |
import java.nio.Buffer; | |
import java.nio.ByteBuffer; | |
/** | |
* | |
*/ | |
public class Test2 { | |
public static void main(String[] args) throws Exception { | |
Unsafe unsafe = getUnsafe(); | |
int capacity = 1000; | |
ByteBuffer buff = ByteBuffer.allocateDirect( capacity ); | |
Field addressField = Buffer.class.getDeclaredField("address"); | |
addressField.setAccessible(true); | |
long memoryAddress = addressField.getLong( buff ); | |
System.out.printf( "Working with memory address: %s\n", memoryAddress ); | |
long addr = memoryAddress; | |
unsafe.putOrderedInt( buff, addr, 666 ); | |
} | |
public static Unsafe getUnsafe() throws Exception { | |
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe"); | |
unsafeField.setAccessible(true); | |
return (Unsafe) unsafeField.get(null); | |
} | |
public static long getAddress( ByteBuffer buff ) throws Exception { | |
Field addressField = Buffer.class.getDeclaredField("address"); | |
addressField.setAccessible(true); | |
return addressField.getLong( buff ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment