Class CollectionSerializer

java.lang.Object
com.esotericsoftware.kryo.Serializer<Collection>
com.esotericsoftware.kryo.serializers.CollectionSerializer
Direct Known Subclasses:
DefaultSerializers.TreeSetSerializer

public class CollectionSerializer extends Serializer<Collection>
Serializes objects that implement the Collection interface.

With the default constructor, a collection requires a 1-3 byte header and an extra 2-3 bytes is written for each element in the collection. The alternate constructor can be used to improve efficiency to match that of using an array instead of a collection.

  • Field Details

    • elementsCanBeNull

      private boolean elementsCanBeNull
    • serializer

      private Serializer serializer
    • elementClass

      private Class elementClass
    • genericType

      private Class genericType
  • Constructor Details

  • Method Details

    • setElementsCanBeNull

      public void setElementsCanBeNull(boolean elementsCanBeNull)
      Parameters:
      elementsCanBeNull - False if all elements are not null. This saves 1 byte per element if elementClass is set. True if it is not known (default).
    • setElementClass

      public void setElementClass(Class elementClass, Serializer serializer)
      Parameters:
      elementClass - The concrete class of each element. This saves 1-2 bytes per element. Set to null if the class is not known or varies per element (default).
      serializer - The serializer to use for each element.
    • setGenerics

      public void setGenerics(Kryo kryo, Class[] generics)
      Description copied from class: Serializer
      Sets the generic types of the field or method this serializer will be used for on the next call to read or write. Subsequent calls to read and write must not use this generic type information. The default implementation does nothing. Subclasses may use the information provided to this method for more efficient serialization, eg to use the same type for all items in a list.
      Overrides:
      setGenerics in class Serializer<Collection>
      Parameters:
      generics - Some (but never all) elements may be null if there is no generic type information at that index.
    • write

      public void write(Kryo kryo, Output output, Collection collection)
      Description copied from class: Serializer
      Writes the bytes for the object to the output.

      This method should not be called directly, instead this serializer can be passed to Kryo write methods that accept a serialier.

      Specified by:
      write in class Serializer<Collection>
      Parameters:
      collection - May be null if Serializer.getAcceptsNull() is true.
    • create

      protected Collection create(Kryo kryo, Input input, Class<Collection> type)
      Used by read(Kryo, Input, Class) to create the new object. This can be overridden to customize object creation, eg to call a constructor with arguments. The default implementation uses Kryo.newInstance(Class).
    • read

      public Collection read(Kryo kryo, Input input, Class<Collection> type)
      Description copied from class: Serializer
      Reads bytes and returns a new object of the specified concrete type.

      Before Kryo can be used to read child objects, Kryo.reference(Object) must be called with the parent object to ensure it can be referenced by the child objects. Any serializer that uses Kryo to read a child object may need to be reentrant.

      This method should not be called directly, instead this serializer can be passed to Kryo read methods that accept a serialier.

      Specified by:
      read in class Serializer<Collection>
      Returns:
      May be null if Serializer.getAcceptsNull() is true.
    • createCopy

      protected Collection createCopy(Kryo kryo, Collection original)
      Used by copy(Kryo, Collection) to create the new object. This can be overridden to customize object creation, eg to call a constructor with arguments. The default implementation uses Kryo.newInstance(Class).
    • copy

      public Collection copy(Kryo kryo, Collection original)
      Description copied from class: Serializer
      Returns a copy of the specified object. The default implementation returns the original if Serializer.isImmutable() is true, else throws KryoException. Subclasses should override this method if needed to support Kryo.copy(Object).

      Before Kryo can be used to copy child objects, Kryo.reference(Object) must be called with the copy to ensure it can be referenced by the child objects. Any serializer that uses Kryo to copy a child object may need to be reentrant.

      This method should not be called directly, instead this serializer can be passed to Kryo copy methods that accept a serialier.

      Overrides:
      copy in class Serializer<Collection>