Class CompressedSerializer

java.lang.Object
cloud.opencode.base.serialization.compress.CompressedSerializer
All Implemented Interfaces:
Serializer

public class CompressedSerializer extends Object implements Serializer
CompressedSerializer - Compression Decorator for Serializers 压缩序列化装饰器

This decorator adds compression capability to any serializer using the Decorator pattern. It automatically compresses data exceeding the threshold and adds a header byte to identify the algorithm.

此装饰器使用装饰器模式为任意序列化器添加压缩能力。 它自动压缩超过阈值的数据并添加头部字节来标识算法。

Features | 主要功能:

  • Automatic compression based on threshold - 基于阈值的自动压缩
  • GZIP and Deflate algorithm support (JDK built-in) - GZIP 和 Deflate 算法支持(JDK 内置)
  • Transparent decompression - 透明解压
  • Header-based algorithm detection - 基于头部的算法检测

Usage Examples | 使用示例:

Serializer jsonSerializer = OpenSerializer.get("json");
CompressedSerializer compressed = new CompressedSerializer(
    jsonSerializer,
    CompressionAlgorithm.GZIP,
    1024  // Compress if > 1KB
);

byte[] data = compressed.serialize(largeObject);
Object restored = compressed.deserialize(data, LargeObject.class);

Data Format | 数据格式:

Below threshold / NONE:
  [raw delegate bytes]              — zero overhead, direct pass-through

Above threshold (compressed):
  +--------+------------------+
  | Header | Compressed Data  |     — 1-byte algorithm ID prefix
  | 1 byte | Variable length  |
  +--------+------------------+
  Header: Algorithm ID (1=GZIP, 5=DEFLATE)

Detection | 检测逻辑:

On deserialization, the first byte is checked against known compression algorithm IDs. GZIP (0x01) and DEFLATE (0x05) are never valid first bytes of JSON (0x7B, 0x5B, etc.) or JDK serialization (0xAC), so the detection is unambiguous.

反序列化时检查第一字节是否为已知压缩算法 ID。GZIP (0x01) 和 DEFLATE (0x05) 不会出现在 JSON 或 JDK 序列化输出的第一字节,因此检测无歧义。

Security | 安全性:

  • Thread-safe: Yes (if delegate is thread-safe) - 线程安全: 是(如果委托是线程安全的)

Performance | 性能特性:

  • Time complexity: O(n) for serialization + compression - 序列化+压缩 O(n)
  • Space complexity: O(n) for compressed output - 压缩输出 O(n)
Since:
JDK 25, opencode-base-serialization V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • DEFAULT_THRESHOLD

      public static final int DEFAULT_THRESHOLD
      Default compression threshold (1024 bytes) 默认压缩阈值(1024 字节)
      See Also:
  • Constructor Details

    • CompressedSerializer

      public CompressedSerializer(Serializer delegate, CompressionAlgorithm algorithm)
      Creates a compressed serializer with default threshold (1024 bytes). 创建带默认阈值(1024 字节)的压缩序列化器。
      Parameters:
      delegate - the delegate serializer - 委托的序列化器
      algorithm - the compression algorithm - 压缩算法
    • CompressedSerializer

      public CompressedSerializer(Serializer delegate, CompressionAlgorithm algorithm, int threshold)
      Creates a compressed serializer with custom threshold. 创建带自定义阈值的压缩序列化器。
      Parameters:
      delegate - the delegate serializer - 委托的序列化器
      algorithm - the compression algorithm - 压缩算法
      threshold - the compression threshold in bytes - 压缩阈值(字节)
  • Method Details

    • 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 类型
    • supports

      public boolean supports(Class<?> type)
      Description copied from interface: Serializer
      Checks if this serializer supports the given type. 检查此序列化器是否支持给定类型。

      Some serializers have specific type requirements.

      某些序列化器有特定的类型要求。

      Specified by:
      supports in interface Serializer
      Parameters:
      type - the type to check - 要检查的类型
      Returns:
      true if supported - 如果支持则返回 true
    • isTextBased

      public boolean isTextBased()
      Description copied from interface: Serializer
      Returns whether this serializer produces text output. 返回此序列化器是否产生文本输出。

      Text-based serializers (JSON, XML) can be converted to String without data loss.

      基于文本的序列化器(JSON、XML)可以无损转换为字符串。

      Specified by:
      isTextBased in interface Serializer
      Returns:
      true if text-based - 如果是基于文本的则返回 true
    • getDelegate

      public Serializer getDelegate()
      Returns the delegate serializer. 返回委托的序列化器。
      Returns:
      the delegate - 委托
    • getAlgorithm

      public CompressionAlgorithm getAlgorithm()
      Returns the compression algorithm. 返回压缩算法。
      Returns:
      the algorithm - 算法
    • getThreshold

      public int getThreshold()
      Returns the compression threshold. 返回压缩阈值。
      Returns:
      the threshold in bytes - 阈值(字节)