Class KryoSerializer
java.lang.Object
cloud.opencode.base.serialization.binary.KryoSerializer
- All Implemented Interfaces:
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new KryoSerializer with default configuration (non-secure mode).KryoSerializer(int poolSize) Creates a new KryoSerializer with custom pool size.KryoSerializer(int poolSize, boolean secureMode) Creates a new KryoSerializer with secure mode option. -
Method Summary
Modifier and TypeMethodDescriptionAdds classes to the allowed whitelist (for secure mode).protected com.esotericsoftware.kryo.KryoCreates and configures a new Kryo instance.<T> Tdeserialize(byte[] data, TypeReference<T> typeRef) Deserializes byte array to a generic type using TypeReference.<T> Tdeserialize(byte[] data, Class<T> type) Deserializes byte array to an object of the specified class.<T> Tdeserialize(byte[] data, Type type) Deserializes byte array using a Type.Returns the format name of this serializer.Returns the MIME type for this serializer.booleanReturns whether this serializer is in secure mode.Registers classes for better serialization performance.Registers a class with a specific ID.static KryoSerializersecure()Creates a secure KryoSerializer that requires class registration.byte[]Serializes an object to byte array.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Serializer
isTextBased, supports
-
Field Details
-
FORMAT
-
-
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
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
Description copied from interface:SerializerSerializes an object to byte array. 将对象序列化为字节数组。- Specified by:
serializein interfaceSerializer- Parameters:
obj- the object to serialize - 要序列化的对象- Returns:
- the serialized bytes - 序列化后的字节数组
-
deserialize
Description copied from interface:SerializerDeserializes byte array to an object of the specified class. 将字节数组反序列化为指定类的对象。- Specified by:
deserializein interfaceSerializer- Type Parameters:
T- the target type - 目标类型- Parameters:
data- the serialized data - 序列化的数据type- the target class - 目标类- Returns:
- the deserialized object - 反序列化后的对象
-
deserialize
Description copied from interface:SerializerDeserializes 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:
deserializein interfaceSerializer- Type Parameters:
T- the target type - 目标类型- Parameters:
data- the serialized data - 序列化的数据typeRef- the type reference - 类型引用- Returns:
- the deserialized object - 反序列化后的对象
-
deserialize
Description copied from interface:SerializerDeserializes byte array using a Type. 使用 Type 反序列化字节数组。- Specified by:
deserializein interfaceSerializer- Type Parameters:
T- the target type - 目标类型- Parameters:
data- the serialized data - 序列化的数据type- the target type - 目标类型- Returns:
- the deserialized object - 反序列化后的对象
-
getFormat
Description copied from interface:SerializerReturns the format name of this serializer. 返回此序列化器的格式名称。Examples: "json", "xml", "kryo", "protobuf", "jdk"
示例: "json", "xml", "kryo", "protobuf", "jdk"
- Specified by:
getFormatin interfaceSerializer- Returns:
- the format name - 格式名称
-
getMimeType
Description copied from interface:SerializerReturns the MIME type for this serializer. 返回此序列化器的 MIME 类型。- Specified by:
getMimeTypein interfaceSerializer- Returns:
- the MIME type (default: "application/octet-stream") - MIME 类型
-
register
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
Registers a class with a specific ID. 注册带特定 ID 的类。- Parameters:
clazz- the class to register - 要注册的类id- the registration ID - 注册 ID- Returns:
- this serializer - 此序列化器
-
allow
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
-