Class Kryo


  • public class Kryo
    extends java.lang.Object
    Maps classes to serializers so object graphs can be serialized automatically.
    • Field Detail

      • lowPriorityDefaultSerializerCount

        private final int lowPriorityDefaultSerializerCount
      • nextRegisterID

        private int nextRegisterID
      • classLoader

        private java.lang.ClassLoader classLoader
      • strategy

        private org.objenesis.strategy.InstantiatorStrategy strategy
      • registrationRequired

        private boolean registrationRequired
      • warnUnregisteredClasses

        private boolean warnUnregisteredClasses
      • depth

        private int depth
      • maxDepth

        private int maxDepth
      • autoReset

        private boolean autoReset
      • thread

        private volatile java.lang.Thread thread
      • graphContext

        private ObjectMap graphContext
      • readReferenceIds

        private final IntArray readReferenceIds
      • references

        private boolean references
      • copyReferences

        private boolean copyReferences
      • readObject

        private java.lang.Object readObject
      • copyDepth

        private int copyDepth
      • copyShallow

        private boolean copyShallow
      • needsCopyReference

        private java.lang.Object needsCopyReference
    • Method Detail

      • addDefaultSerializer

        public void addDefaultSerializer​(java.lang.Class type,
                                         java.lang.Class<? extends Serializer> serializerClass)
        Instances of the specified class will use the specified serializer when register(Class) or register(Class, int) are called. Serializer instances are created as needed via SerializerFactory.ReflectionSerializerFactory.newSerializer(Kryo, Class, Class). By default, the following classes have a default serializer set:

        boolean Boolean byte Byte char
        Character short Short int Integer
        long Long float Float double
        Double String byte[] char[] short[]
        int[] long[] float[] double[] String[]
        Object[] Map BigInteger BigDecimal KryoSerializable
        Collection Date Collections.emptyList Collections.singleton Currency
        StringBuilder Enum Collections.emptyMap Collections.emptySet Calendar
        StringBuffer Class Collections.singletonList Collections.singletonMap TimeZone
        TreeMap EnumSet

        Note that the order default serializers are added is important for a class that may match multiple types. The above default serializers always have a lower priority than subsequent default serializers that are added.

      • getDefaultSerializer

        public Serializer getDefaultSerializer​(java.lang.Class type)
        Returns the best matching serializer for a class. This method can be overridden to implement custom logic to choose a serializer.
      • getDefaultSerializerForAnnotatedType

        protected Serializer getDefaultSerializerForAnnotatedType​(java.lang.Class type)
      • register

        public Registration register​(java.lang.Class type)
        Registers the class using the lowest, next available integer ID and the default serializer. If the class is already registered, no change will be made and the existing registration will be returned. Registering a primitive also affects the corresponding primitive wrapper.

        Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when using this method. The order must be the same at deserialization as it was for serialization.

      • register

        public Registration register​(java.lang.Class type,
                                     int id)
        Registers the class using the specified ID and the default serializer. If the class is already registered this has no effect and the existing registration is returned. Registering a primitive also affects the corresponding primitive wrapper.

        IDs must be the same at deserialization as they were for serialization.

        Parameters:
        id - Must be >= 0. Smaller IDs are serialized more efficiently. IDs 0-8 are used by default for primitive types and String, but these IDs can be repurposed.
      • register

        public Registration register​(java.lang.Class type,
                                     Serializer serializer)
        Registers the class using the lowest, next available integer ID and the specified serializer. If the class is already registered, the existing entry is updated with the new serializer. Registering a primitive also affects the corresponding primitive wrapper.

        Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when using this method. The order must be the same at deserialization as it was for serialization.

      • register

        public Registration register​(java.lang.Class type,
                                     Serializer serializer,
                                     int id)
        Registers the class using the specified ID and serializer. Providing an ID that is already in use by the same type will cause the old entry to be overwritten. Registering a primitive also affects the corresponding primitive wrapper.

        IDs must be the same at deserialization as they were for serialization.

        Parameters:
        id - Must be >= 0. Smaller IDs are serialized more efficiently. IDs 0-9 are used by default for primitive types and their wrappers, String, and void, but these IDs can be repurposed.
      • register

        public Registration register​(Registration registration)
        Stores the specified registration. If the ID is already in use by the same type, the old entry is overwritten. Registering a primitive also affects the corresponding primitive wrapper.

        IDs must be the same at deserialization as they were for serialization.

        Registration can be suclassed to efficiently store per type information, accessible in serializers via getRegistration(Class).

      • getNextRegistrationId

        public int getNextRegistrationId()
        Returns the lowest, next available integer ID.
      • unregisteredClassMessage

        protected java.lang.String unregisteredClassMessage​(java.lang.Class type)
      • writeObject

        public void writeObject​(Output output,
                                java.lang.Object object)
        Writes an object using the registered serializer.
      • writeObject

        public void writeObject​(Output output,
                                java.lang.Object object,
                                Serializer serializer)
        Writes an object using the specified serializer. The registered serializer is ignored.
      • writeObjectOrNull

        public void writeObjectOrNull​(Output output,
                                      java.lang.Object object,
                                      java.lang.Class type)
        Writes an object or null using the registered serializer for the specified type.
        Parameters:
        object - May be null.
      • writeObjectOrNull

        public void writeObjectOrNull​(Output output,
                                      java.lang.Object object,
                                      Serializer serializer)
        Writes an object or null using the specified serializer. The registered serializer is ignored.
        Parameters:
        object - May be null.
      • writeClassAndObject

        public void writeClassAndObject​(Output output,
                                        java.lang.Object object)
        Writes the class and object or null using the registered serializer.
        Parameters:
        object - May be null.
      • writeReferenceOrNull

        boolean writeReferenceOrNull​(Output output,
                                     java.lang.Object object,
                                     boolean mayBeNull)
        Parameters:
        object - May be null if mayBeNull is true.
        Returns:
        true if no bytes need to be written for the object.
      • readObject

        public <T> T readObject​(Input input,
                                java.lang.Class<T> type)
        Reads an object using the registered serializer.
      • readObject

        public <T> T readObject​(Input input,
                                java.lang.Class<T> type,
                                Serializer serializer)
        Reads an object using the specified serializer. The registered serializer is ignored.
      • readObjectOrNull

        public <T> T readObjectOrNull​(Input input,
                                      java.lang.Class<T> type)
        Reads an object or null using the registered serializer.
        Returns:
        May be null.
      • readObjectOrNull

        public <T> T readObjectOrNull​(Input input,
                                      java.lang.Class<T> type,
                                      Serializer serializer)
        Reads an object or null using the specified serializer. The registered serializer is ignored.
        Returns:
        May be null.
      • readClassAndObject

        public java.lang.Object readClassAndObject​(Input input)
        Reads the class and object or null using the registered serializer.
        Returns:
        May be null.
      • readReferenceOrNull

        int readReferenceOrNull​(Input input,
                                java.lang.Class type,
                                boolean mayBeNull)
        Returns REF if a reference to a previously read object was read, which is stored in readObject. Returns a stack size (> 0) if a reference ID has been put on the stack.
      • reset

        public void reset()
        Resets unregistered class names, references to previously serialized or deserialized objects, and the graph context. If auto reset is true, this method is called automatically when an object graph has been completely serialized or deserialized. If overridden, the super method must be called.
      • copy

        public <T> T copy​(T object)
        Returns a deep copy of the object. Serializers for the classes involved must support Serializer.copy(Kryo, Object).
        Parameters:
        object - May be null.
      • copy

        public <T> T copy​(T object,
                          Serializer serializer)
        Returns a deep copy of the object using the specified serializer. Serializers for the classes involved must support Serializer.copy(Kryo, Object).
        Parameters:
        object - May be null.
      • copyShallow

        public <T> T copyShallow​(T object)
        Returns a shallow copy of the object. Serializers for the classes involved must support Serializer.copy(Kryo, Object).
        Parameters:
        object - May be null.
      • copyShallow

        public <T> T copyShallow​(T object,
                                 Serializer serializer)
        Returns a shallow copy of the object using the specified serializer. Serializers for the classes involved must support Serializer.copy(Kryo, Object).
        Parameters:
        object - May be null.
      • beginObject

        private void beginObject()
      • getReferenceResolver

        public ReferenceResolver getReferenceResolver()
        Returns:
        May be null.
      • setClassLoader

        public void setClassLoader​(java.lang.ClassLoader classLoader)
        Sets the classloader to resolve unregistered class names to classes. The default is the loader that loaded the Kryo class.
      • getClassLoader

        public java.lang.ClassLoader getClassLoader()
      • setRegistrationRequired

        public void setRegistrationRequired​(boolean registrationRequired)
        If true, an exception is thrown when an unregistered class is encountered. Default is false.

        If false, when an unregistered class is encountered, its fully qualified class name will be serialized and the default serializer for the class used to serialize the object. Subsequent appearances of the class within the same object graph are serialized as an int id.

        Registered classes are serialized as an int id, avoiding the overhead of serializing the class name, but have the drawback of needing to know the classes to be serialized up front.

      • isRegistrationRequired

        public boolean isRegistrationRequired()
      • setWarnUnregisteredClasses

        public void setWarnUnregisteredClasses​(boolean warnUnregisteredClasses)
        If true, kryo writes a warn log telling about the classes unregistered. Default is false.

        If false, no log are written when unregistered classes are encountered.

      • isWarnUnregisteredClasses

        public boolean isWarnUnregisteredClasses()
      • setReferences

        public boolean setReferences​(boolean references)
        If true, each appearance of an object in the graph after the first is stored as an integer ordinal. When set to true, MapReferenceResolver is used. This enables references to the same object and cyclic graphs to be serialized, but typically adds overhead of one byte per object. Default is true.
        Returns:
        The previous value.
      • setCopyReferences

        public void setCopyReferences​(boolean copyReferences)
        If true, when copy(Object) and other copy methods encounter an object for the first time the object is copied and on subsequent encounters the copied object is used. If false, the overhead of tracking which objects have already been copied is avoided because each object is copied every time it is encountered, however a stack overflow will occur if an object graph is copied that contains a circular reference. Default is true.
      • setReferenceResolver

        public void setReferenceResolver​(ReferenceResolver referenceResolver)
        Sets the reference resolver and enables references.
      • getReferences

        public boolean getReferences()
      • setInstantiatorStrategy

        public void setInstantiatorStrategy​(org.objenesis.strategy.InstantiatorStrategy strategy)
        Sets the strategy used by newInstantiator(Class) for creating objects. See StdInstantiatorStrategy to create objects via without calling any constructor. See SerializingInstantiatorStrategy to mimic Java's built-in serialization.
        Parameters:
        strategy - May be null.
      • getInstantiatorStrategy

        public org.objenesis.strategy.InstantiatorStrategy getInstantiatorStrategy()
      • newInstantiator

        protected org.objenesis.instantiator.ObjectInstantiator newInstantiator​(java.lang.Class type)
        Returns a new instantiator for creating new instances of the specified type. By default, an instantiator is returned that uses reflection if the class has a zero argument constructor, an exception is thrown. If a strategy is set, it will be used instead of throwing an exception.
      • getContext

        public ObjectMap getContext()
        Name/value pairs that are available to all serializers.
      • getGraphContext

        public ObjectMap getGraphContext()
        Name/value pairs that are available to all serializers and are cleared after each object graph is serialized or deserialized.
      • getDepth

        public int getDepth()
        Returns the number of child objects away from the object graph root.
      • getOriginalToCopyMap

        public IdentityMap getOriginalToCopyMap()
        Returns the internal map of original to copy objects when a copy method is used. This can be used after a copy to map old objects to the copies, however it is cleared automatically by reset() so this is only useful when setAutoReset(boolean) is false.
      • setAutoReset

        public void setAutoReset​(boolean autoReset)
        If true (the default), reset() is called automatically after an entire object graph has been read or written. If false, reset() must be called manually, which allows unregistered class names, references, and other information to span multiple object graphs.
      • setMaxDepth

        public void setMaxDepth​(int maxDepth)
        Sets the maxiumum depth of an object graph. This can be used to prevent malicious data from causing a stack overflow. Default is Integer.MAX_VALUE.
      • isFinal

        public boolean isFinal​(java.lang.Class type)
        Returns true if the specified type is final. Final types can be serialized more efficiently because they are non-polymorphic.

        This can be overridden to force non-final classes to be treated as final. Eg, if an application uses ArrayList extensively but never uses an ArrayList subclass, treating ArrayList as final could allow FieldSerializer to save 1-2 bytes per ArrayList field.

      • isClosure

        protected boolean isClosure​(java.lang.Class type)
        Returns true if the specified type is a closure.

        This can be overridden to support alternative implementations of clousres. Current version supports Oracle's Java8 only