Skip to content

Instantly share code, notes, and snippets.

@burtonator
Created June 28, 2013 21:50
Show Gist options
  • Save burtonator/5888435 to your computer and use it in GitHub Desktop.
Save burtonator/5888435 to your computer and use it in GitHub Desktop.
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