Created
October 31, 2012 17:47
-
-
Save nitsanw/3988619 to your computer and use it in GitHub Desktop.
Using Unsafe to break encapsulation - step 3
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
public final static String buildUnsafe(char[] chars){ | |
String mutable = new String();// an empty string to hack | |
UnsafeAccess.unsafe.putObject(mutable,valueFieldOffset,chars); | |
UnsafeAccess.unsafe.putInt(mutable,countFieldOffset,chars.length); | |
return mutable; | |
} | |
public final static String buildUnsafe(char[] chars, int offset, int length){ | |
String mutable = new String();// an empty string to hack | |
UnsafeAccess.unsafe.putObject(mutable,valueFieldOffset,chars); | |
UnsafeAccess.unsafe.putInt(mutable,countFieldOffset,length); | |
UnsafeAccess.unsafe.putInt(mutable,offsetFieldOffset,offset); | |
return mutable; | |
} | |
public final static char[] getChars(String s){ | |
return (char[])UnsafeAccess.unsafe.getObject(s,valueFieldOffset); | |
} | |
public final static int getOffset(String s){ | |
return UnsafeAccess.unsafe.getInt(s,offsetFieldOffset); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for the blog post here: http://psy-lob-saw.blogspot.co.uk/2012/10/breaking-string-encapsulation-using.html