Class SerializationResult

java.lang.Object
cloud.opencode.base.serialization.SerializationResult

public final class SerializationResult extends Object
SerializationResult - Immutable Serialization Result Wrapper 序列化结果包装类 - 不可变

Wraps the result of a serialization operation, including the serialized data, format, size information, compression status, and timing information.

包装序列化操作的结果,包括序列化数据、格式、大小信息、压缩状态和计时信息。

Features | 主要功能:

  • Immutable result wrapper - 不可变结果包装
  • Timing support via timed() factory - 通过 timed() 工厂方法支持计时
  • Format and compression metadata - 格式和压缩元数据
  • Defensive copy of byte array data - 字节数组数据的防御性拷贝

Usage Examples | 使用示例:

// Basic result
SerializationResult result = SerializationResult.of(data, "json");

// With timing
SerializationResult result = SerializationResult.of(data, "json", durationNanos);

// Auto-timed serialization
SerializationResult result = SerializationResult.timed(
    () -> serializer.serialize(obj), "json");

// Via OpenSerializer
SerializationResult result = OpenSerializer.serializeWithResult(obj);
System.out.println(result.format());        // "json"
System.out.println(result.data().length);   // byte count
System.out.println(result.durationNanos()); // nanoseconds

Security | 安全性:

  • Thread-safe: Yes (immutable with defensive copies) - 线程安全: 是(不可变,防御性拷贝)
Since:
JDK 25, opencode-base-serialization V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    SerializationResult(byte[] data, String format, int originalSize, boolean compressed, long durationNanos)
    Public constructor with validation and defensive copy.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the serialized data as a UTF-8 string.
    boolean
    Returns whether the data is compressed.
    byte[]
    Returns a defensive copy of the serialized data.
    byte[]
    Returns the internal byte array without copying.
    long
    Returns the serialization duration in nanoseconds, 0 if not measured.
    boolean
     
    Returns the serialization format.
    int
     
    of(byte[] data, String format)
    Creates a basic serialization result.
    of(byte[] data, String format, long durationNanos)
    Creates a serialization result with timing information.
    int
    Returns the estimated original size, or -1 if unknown.
    int
    Returns the size of the serialized data in bytes.
    timed(Supplier<byte[]> serialization, String format)
    Creates a timed serialization result by executing the supplier and measuring elapsed time.
     

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • SerializationResult

      public SerializationResult(byte[] data, String format, int originalSize, boolean compressed, long durationNanos)
      Public constructor with validation and defensive copy. 带验证和防御性拷贝的公共构造函数。
      Parameters:
      data - the serialized data | 序列化后的数据
      format - the format | 格式
      originalSize - the original size estimate (-1 if unknown) | 原始大小估计
      compressed - whether compressed | 是否压缩
      durationNanos - duration in nanoseconds | 耗时(纳秒)
  • Method Details

    • data

      public byte[] data()
      Returns a defensive copy of the serialized data. 返回序列化数据的防御性拷贝。
      Returns:
      a copy of the data array | 数据数组的拷贝
    • dataUnsafe

      public byte[] dataUnsafe()
      Returns the internal byte array without copying. 返回内部字节数组,不进行拷贝。

      Warning: The returned array is the internal representation. Callers must not modify it. Use data() if you need a mutable copy.

      警告:返回的数组是内部表示。 调用方不得修改它。如需可修改副本请使用 data()

      Returns:
      the internal data array (read-only by contract) | 内部数据数组(约定只读)
      Since:
      JDK 25, opencode-base-serialization V1.0.3
    • format

      public String format()
      Returns the serialization format. 返回序列化格式。
      Returns:
      the format | 格式
    • originalSize

      public int originalSize()
      Returns the estimated original size, or -1 if unknown. 返回估计的原始大小,未知则为 -1。
      Returns:
      the original size | 原始大小
    • compressed

      public boolean compressed()
      Returns whether the data is compressed. 返回数据是否被压缩。
      Returns:
      true if compressed | 如果已压缩则为 true
    • durationNanos

      public long durationNanos()
      Returns the serialization duration in nanoseconds, 0 if not measured. 返回序列化耗时(纳秒),未测量则为 0。
      Returns:
      the duration in nanoseconds | 耗时(纳秒)
    • size

      public int size()
      Returns the size of the serialized data in bytes. 返回序列化数据的字节大小。
      Returns:
      the data size in bytes | 数据字节大小
    • asString

      public String asString()
      Returns the serialized data as a UTF-8 string. 将序列化数据作为 UTF-8 字符串返回。
      Returns:
      the data as a UTF-8 string | 数据的 UTF-8 字符串表示
    • of

      public static SerializationResult of(byte[] data, String format)
      Creates a basic serialization result. 创建基本的序列化结果。
      Parameters:
      data - the serialized data | 序列化后的数据
      format - the format name | 格式名称
      Returns:
      the result | 结果
    • of

      public static SerializationResult of(byte[] data, String format, long durationNanos)
      Creates a serialization result with timing information. 创建带计时信息的序列化结果。
      Parameters:
      data - the serialized data | 序列化后的数据
      format - the format name | 格式名称
      durationNanos - the duration in nanoseconds | 耗时(纳秒)
      Returns:
      the result | 结果
    • timed

      public static SerializationResult timed(Supplier<byte[]> serialization, String format)
      Creates a timed serialization result by executing the supplier and measuring elapsed time. 通过执行 supplier 并测量耗时来创建计时序列化结果。

      The byte array returned by the supplier is used directly without defensive copy, since it is freshly created and not retained by the caller.

      供应者返回的字节数组直接使用,不做防御性拷贝,因为它是新创建的且调用方不会持有引用。

      Parameters:
      serialization - the serialization supplier | 序列化供应者
      format - the format name | 格式名称
      Returns:
      the result with timing | 带计时的结果
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object