Advertisement
anta40

TestPointers.java

Apr 28th, 2011
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import sun.misc.Unsafe;
  2. import java.lang.reflect.Field;
  3.  
  4. public class TestPointers {
  5.     public static void main(String[] args) throws Exception {
  6.          Unsafe unsafe = getUnsafe();
  7.          System.out.println("Unsafe = " + unsafe);
  8.          System.out.println(" addressSize() = " + unsafe.addressSize());
  9.          System.out.println(" pageSize() = " + unsafe.pageSize());
  10.          System.out.println(" pageSize() = " + unsafe.pageSize());
  11.          try {
  12.              unsafe.putByte((long) 0xa000, (byte) 47);
  13.          } catch(Throwable e) {
  14.              System.out.println("IN THE CATCH BLOCK");
  15.              e.printStackTrace();
  16.          } finally {
  17.              System.out.println("IN THE FINALLY BLOCK");
  18.          }
  19.          System.exit(0);
  20.     }
  21.  
  22.      public static Unsafe getUnsafe() {
  23.          Unsafe unsafe = null;
  24.          try {
  25.              Class uc = Unsafe.class;
  26.              Field[] fields = uc.getDeclaredFields();
  27.              for(int i = 0; i < fields.length; i++) {
  28.                  if(fields[i].getName().equals("theUnsafe")) {
  29.                      fields[i].setAccessible(true);
  30.                      unsafe = (Unsafe) fields[i].get(uc);
  31.                      break;
  32.                  }
  33.              }
  34.          } catch(Exception ignore) {
  35.      }
  36.      return unsafe;
  37.      }
  38.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement