Class DataSerializer

java.lang.Object
org.codelibs.fess.crawler.serializer.DataSerializer

public class DataSerializer extends Object
A serializer class for handling object serialization and deserialization.

This class provides serialization capabilities using different serializers, currently supporting Kryo and JavaBin serialization formats. The serializer type is determined by the crawler data serializer configuration.

The class is thread-safe and uses ThreadLocal to maintain Kryo instances per thread to avoid synchronization overhead.

  • Field Details

    • JAVABIN

      protected static final String JAVABIN
      Constant for JavaBin serializer type.
      See Also:
    • KRYO

      protected static final String KRYO
      Constant for Kryo serializer type.
      See Also:
    • kryoThreadLocal

      protected final ThreadLocal<com.esotericsoftware.kryo.Kryo> kryoThreadLocal
      ThreadLocal container for Kryo instances to ensure thread safety.
  • Constructor Details

    • DataSerializer

      public DataSerializer()
      Constructs a new DataSerializer.

      Initializes the ThreadLocal Kryo instances with appropriate configuration. The Kryo instances are configured to not require class registration for flexibility, and debug mode warnings are enabled when debug logging is active.

  • Method Details

    • getSerializerType

      protected String getSerializerType()
      Gets the configured serializer type from the Fess configuration.
      Returns:
      the serializer type (either "kryo" or "javabin")
    • fromObjectToBinary

      public byte[] fromObjectToBinary(Object obj)
      Serializes an object to a byte array.

      The serialization method used depends on the configured serializer type. Supported types are Kryo and JavaBin serialization.

      Parameters:
      obj - the object to serialize
      Returns:
      the serialized object as a byte array
      Throws:
      IllegalArgumentException - if an unsupported serializer type is configured
      org.codelibs.core.exception.IORuntimeException - if an I/O error occurs during serialization
    • fromBinaryToObject

      public Object fromBinaryToObject(byte[] bytes)
      Deserializes a byte array back to an object.

      The deserialization method used depends on the configured serializer type. Supported types are Kryo and JavaBin deserialization.

      Parameters:
      bytes - the byte array to deserialize
      Returns:
      the deserialized object
      Throws:
      IllegalArgumentException - if an unsupported serializer type is configured
      org.codelibs.core.exception.IORuntimeException - if an I/O error occurs during deserialization
    • serializeWithKryo

      protected byte[] serializeWithKryo(Object obj)
      Serializes an object using Kryo serialization.

      Uses the thread-local Kryo instance to serialize the object along with its class information. The serialized data is written to a byte array output stream.

      Parameters:
      obj - the object to serialize
      Returns:
      the serialized object as a byte array
      Throws:
      org.codelibs.core.exception.IORuntimeException - if an I/O error occurs during serialization
    • deserializeWithKryo

      protected Object deserializeWithKryo(byte[] bytes)
      Deserializes a byte array using Kryo deserialization.

      Uses the thread-local Kryo instance to read both the class information and object data from the byte array input stream.

      Parameters:
      bytes - the byte array to deserialize
      Returns:
      the deserialized object