Class KryoUtils

java.lang.Object
uno.anahata.asi.persistence.kryo.KryoUtils

public class KryoUtils extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    private static final ThreadLocal<com.esotericsoftware.kryo.Kryo>
    The core mechanism for managing thread-safe Kryo instances.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    clone(T object)
    Creates a deep clone of the given object using a serialization-deserialization cycle.
    static <T> T
    deserialize(byte[] bytes, Class<T> clazz)
    Deserializes a byte array into an object.
    static com.esotericsoftware.kryo.Kryo
    Retrieves the Kryo instance for the currently executing thread.
    static byte[]
    serialize(Object object)
    Serializes an object into a byte array.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • kryoThreadLocal

      private static final ThreadLocal<com.esotericsoftware.kryo.Kryo> 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 the transient modifier, 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

      public static byte[] serialize(Object object)
      Serializes an object into a byte array.
      Parameters:
      object - The object to serialize.
      Returns:
      A byte array representing the serialized object.
    • deserialize

      public static <T> T deserialize(byte[] bytes, Class<T> clazz)
      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.