-
Notifications
You must be signed in to change notification settings - Fork 11
UnsafeUtil
UnsafeUtil is a cross platform* utility class for "safe" Unsafe usage.
serializing arrays of primitives is often slow, as u need to iterate through the entire array, break each piece into it's bytes, and then write them. What if instead, you could just cast a long array to a byte one, C style?
UnsafeUtil#upcastArray
and UnsafeUtil#downcastArray
are your friends.
keep in mind this uses whatever the system's native endian order is..
Every object stores a "pointer" to it's "class", the offset and size of this pointer depends on the JVM, OS, and JVM args, but UnsafeUtil handles all of that for you. the Klass pointer is used by multiple utility methods in UnsafeUtil
(String[])new Object[] {"hey!"}
is invalid, not if UnsafeUtil#arrayCast
has anything to say about it!
As the Object stores it's class pointer on it, you can also change it, effectively casting any object to any other type, even if the object wasn't originally that type. Keep in mind this doesn't magically convert anything, all the data in the object is preserved. This is used by GrossFabricHacks in conjunction with runtime class generation to create a class that extends MixinTransformer, and then use Unsafe casting to convert the global instance to one that is an instance of our generated class. (this needed b/c the methods are package private, and we don't want to depend on Knot)
UnsafeUtil#unsafeCast