Class KryoSerializer

java.lang.Object
cloud.opencode.base.serialization.binary.KryoSerializer
All Implemented Interfaces:
Serializer

public class KryoSerializer extends Object implements Serializer
KryoSerializer - High Performance Kryo Serialization Kryo 高性能序列化器

Uses Kryo library for fast binary serialization with pool management for thread safety.

使用 Kryo 库进行快速二进制序列化,通过池管理确保线程安全。

Features | 主要功能:

  • High performance serialization - 高性能序列化
  • Compact binary format - 紧凑的二进制格式
  • Thread-safe via Kryo Pool - 通过 Kryo 池实现线程安全
  • Optional class registration - 可选的类注册

Performance | 性能特性:

  • 10x faster than JDK serialization - 比 JDK 序列化快 10 倍
  • 5x smaller output than JDK - 输出比 JDK 小 5 倍

Usage Examples | 使用示例:

KryoSerializer kryo = new KryoSerializer();

// Optional: Register classes for better performance
kryo.register(User.class, Order.class);

// Serialize
byte[] data = kryo.serialize(user);

// Deserialize
User restored = kryo.deserialize(data, User.class);

Security | 安全性:

  • Thread-safe: Yes (via pool) - 线程安全: 是 (通过池)
Since:
JDK 25, opencode-base-serialization V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

  • Constructor Details

    • KryoSerializer

      public KryoSerializer()
      Creates a new KryoSerializer with default configuration (non-secure mode). 创建带默认配置的新 KryoSerializer(非安全模式)。
    • KryoSerializer

      public KryoSerializer(int poolSize)
      Creates a new KryoSerializer with custom pool size. 创建带自定义池大小的新 KryoSerializer。
      Parameters:
      poolSize - the pool size - 池大小
    • KryoSerializer

      public KryoSerializer(int poolSize, boolean secureMode)
      Creates a new KryoSerializer with secure mode option. 创建带安全模式选项的新 KryoSerializer。
      Parameters:
      poolSize - the pool size - 池大小
      secureMode - if true, only registered classes can be deserialized - 如果为true,只有注册的类可以反序列化
  • Method Details

    • secure

      public static KryoSerializer secure()
      Creates a secure KryoSerializer that requires class registration. 创建需要类注册的安全 KryoSerializer。
      Returns:
      a secure KryoSerializer instance - 安全的 KryoSerializer 实例
    • createKryo

      protected com.esotericsoftware.kryo.Kryo createKryo()
      Creates and configures a new Kryo instance. 创建并配置新的 Kryo 实例。
    • serialize

      public byte[] serialize(Object obj)
      Description copied from interface: Serializer
      Serializes an object to byte array. 将对象序列化为字节数组。
      Specified by:
      serialize in interface Serializer
      Parameters:
      obj - the object to serialize - 要序列化的对象
      Returns:
      the serialized bytes - 序列化后的字节数组
    • deserialize

      public <T> T deserialize(byte[] data, Class<T> type)
      Description copied from interface: Serializer
      Deserializes byte array to an object of the specified class. 将字节数组反序列化为指定类的对象。
      Specified by:
      deserialize in interface Serializer
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized data - 序列化的数据
      type - the target class - 目标类
      Returns:
      the deserialized object - 反序列化后的对象
    • deserialize

      public <T> T deserialize(byte[] data, TypeReference<T> typeRef)
      Description copied from interface: Serializer
      Deserializes byte array to a generic type using TypeReference. 使用 TypeReference 将字节数组反序列化为泛型类型。

      This method preserves generic type information that would otherwise be lost due to type erasure.

      此方法保留了由于类型擦除而丢失的泛型类型信息。

      Specified by:
      deserialize in interface Serializer
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized data - 序列化的数据
      typeRef - the type reference - 类型引用
      Returns:
      the deserialized object - 反序列化后的对象
    • deserialize

      public <T> T deserialize(byte[] data, Type type)
      Description copied from interface: Serializer
      Deserializes byte array using a Type. 使用 Type 反序列化字节数组。
      Specified by:
      deserialize in interface Serializer
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized data - 序列化的数据
      type - the target type - 目标类型
      Returns:
      the deserialized object - 反序列化后的对象
    • getFormat

      public String getFormat()
      Description copied from interface: Serializer
      Returns the format name of this serializer. 返回此序列化器的格式名称。

      Examples: "json", "xml", "kryo", "protobuf", "jdk"

      示例: "json", "xml", "kryo", "protobuf", "jdk"

      Specified by:
      getFormat in interface Serializer
      Returns:
      the format name - 格式名称
    • getMimeType

      public String getMimeType()
      Description copied from interface: Serializer
      Returns the MIME type for this serializer. 返回此序列化器的 MIME 类型。
      Specified by:
      getMimeType in interface Serializer
      Returns:
      the MIME type (default: "application/octet-stream") - MIME 类型
    • register

      public KryoSerializer register(Class<?>... classes)
      Registers classes for better serialization performance. 注册类以获得更好的序列化性能。

      Registration is optional but improves performance and reduces output size.

      注册是可选的,但可以提高性能并减少输出大小。

      Parameters:
      classes - the classes to register - 要注册的类
      Returns:
      this serializer - 此序列化器
    • register

      public KryoSerializer register(Class<?> clazz, int id)
      Registers a class with a specific ID. 注册带特定 ID 的类。
      Parameters:
      clazz - the class to register - 要注册的类
      id - the registration ID - 注册 ID
      Returns:
      this serializer - 此序列化器
    • allow

      public KryoSerializer allow(Class<?>... classes)
      Adds classes to the allowed whitelist (for secure mode). 将类添加到允许的白名单(用于安全模式)。
      Parameters:
      classes - the classes to allow - 要允许的类
      Returns:
      this serializer - 此序列化器
    • isSecureMode

      public boolean isSecureMode()
      Returns whether this serializer is in secure mode. 返回此序列化器是否处于安全模式。
      Returns:
      true if secure mode is enabled - 如果启用了安全模式则返回 true