Class KryoUtils
java.lang.Object
uno.anahata.asi.persistence.kryo.KryoUtils
A utility class for thread-safe Kryo serialization and deserialization.
This class uses a ThreadLocal to manage Kryo instances. This is the standard,
enterprise-grade pattern for using Kryo in a multi-threaded environment because Kryo
instances are **not thread-safe**.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ThreadLocal<com.esotericsoftware.kryo.Kryo> The core mechanism for managing thread-safe Kryo instances. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tclone(T object) Creates a deep clone of the given object using a serialization-deserialization cycle.static <T> Tdeserialize(byte[] bytes, Class<T> clazz) Deserializes a byte array into an object.static com.esotericsoftware.kryo.KryogetKryo()Retrieves the Kryo instance for the currently executing thread.static byte[]Serializes an object into a byte array.
-
Field Details
-
kryoThreadLocal
The core mechanism for managing thread-safe Kryo instances.
-
-
Constructor Details
-
KryoUtils
public KryoUtils()
-
-
Method Details
-
getKryo
public static com.esotericsoftware.kryo.Kryo getKryo()Retrieves the Kryo instance for the currently executing thread.- Returns:
- A thread-safe Kryo instance.
-
clone
public static <T> T clone(T object) Creates a deep clone of the given object using a serialization-deserialization cycle.Technical Purity: This approach is preferred over
kryo.copy()because it authoritatively respects thetransientmodifier, ensuring that environmental references (like the AsiContainer or ThreadPools) are not accidentally cloned, which prevents circular dependencies and access violations in JDK 17+.- Type Parameters:
T- The type of the object.- Parameters:
object- The object to clone.- Returns:
- A deep clone of the object.
-
serialize
Serializes an object into a byte array.- Parameters:
object- The object to serialize.- Returns:
- A byte array representing the serialized object.
-
deserialize
Deserializes a byte array into an object.- Type Parameters:
T- The type of the object to deserialize.- Parameters:
bytes- The byte array to deserialize.clazz- The class of the object.- Returns:
- The deserialized object.
-

