1. Acquire Unsafe:
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 static final Unsafe unsafe; | |
static | |
{ | |
try | |
{ | |
// This is a bit of voodoo to force the unsafe object into | |
// visibility and acquire it. | |
// This is not playing nice, but as an established back door it is | |
// not likely to be taken away. | |
Field field = Unsafe.class.getDeclaredField("theUnsafe"); | |
field.setAccessible(true); | |
unsafe = (Unsafe)field.get(null); | |
} | |
catch (Exception e) | |
{ | |
throw new RuntimeException(e); | |
} | |
} |
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
private static final long valueFieldOffset; | |
private static final long countFieldOffset; | |
private static final long offsetFieldOffset; | |
static | |
{ | |
try | |
{ | |
valueFieldOffset = UnsafeAccess.unsafe.objectFieldOffset(String.class.getDeclaredField("value")); | |
countFieldOffset = UnsafeAccess.unsafe.objectFieldOffset(String.class.getDeclaredField("count")); | |
offsetFieldOffset = UnsafeAccess.unsafe.objectFieldOffset(String.class.getDeclaredField("offset")); | |
} | |
catch (Exception e) | |
{ | |
throw new RuntimeException(e); | |
} | |
} |
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); | |
} |
Getting the data out of String is far less questionable then altering it's internal final fields, just so we are clear. So it is not really recommended that you use the set functionality as illustrated above, unless you are sure it's going to work. Here is why it's generally a bad idea to write into final fields.
Using other techniques it should be possible to hack your way into the package private String constructor that would spare us that bit of hackiness to the same effect.
Measurements and a real world use case to follow...
Update(05/12/2012):
As per the source code used for the next post, the above source code would break for JDK7 as String no longer has the fields offset and count. The sentiment however stays the same and the final result is on GitHub.