com.esotericsoftware.kryo
Class Kryo

java.lang.Object
  extended by com.esotericsoftware.kryo.Kryo

public class Kryo
extends java.lang.Object

Maps classes to serializers so object graphs can be serialized automatically.

Author:
Nathan Sweet

Nested Class Summary
static interface Kryo.Listener
          Provides notification of Kryo events.
static class Kryo.RegisteredClass
          Holds the registration information for a class.
 
Field Summary
static java.lang.String version
           
 
Constructor Summary
Kryo()
           
 
Method Summary
 void addListener(Kryo.Listener listener)
           
 java.lang.ClassLoader getClassLoader()
           
static Context getContext()
          Returns the thread local context for serialization and deserialization.
 Kryo.RegisteredClass getRegisteredClass(java.lang.Class type)
          Returns the registration information for the specified class.
 Kryo.RegisteredClass getRegisteredClass(int classID)
           
 Serializer getSerializer(java.lang.Class type)
           
protected  void handleUnregisteredClass(java.lang.Class type)
          If optional registration is true, this method is called the first time an unregistered class is encountered.
static boolean isFinal(java.lang.Class type)
          Returns true if the specified type is final, or if it is an array of a final type.
protected  Serializer newDefaultSerializer(java.lang.Class type)
          Called by newSerializer(Class) when a serializer could not otherwise be determined.
<T> T
newInstance(java.lang.Class<T> type)
          Returns an instance of the specified class.
 Serializer newSerializer(java.lang.Class type)
          Returns a serializer for the specified type, determined according to this table:
 Kryo.RegisteredClass readClass(java.nio.ByteBuffer buffer)
          Reads the class from the buffer.
 java.lang.Object readClassAndObject(java.nio.ByteBuffer buffer)
          Reads a class from the buffer and uses the serializer registered for that class to read an object from the buffer.
<T> T
readObject(java.nio.ByteBuffer buffer, java.lang.Class<T> type)
          Uses the serializer registered for the specified class to read an object from the buffer.
<T> T
readObjectData(java.nio.ByteBuffer buffer, java.lang.Class<T> type)
          Uses the serializer registered for the specified class to read an object from the buffer.
 Kryo.RegisteredClass register(java.lang.Class type)
          Registers a class with an ordinal, automatically determining the serializer to use.
 void register(java.lang.Class type, Kryo.RegisteredClass registeredClass)
          Registers a class with the ordinal of the specified registered class.
 Kryo.RegisteredClass register(java.lang.Class type, Serializer serializer)
          Registers a class with an ordinal.
 Kryo.RegisteredClass register(java.lang.Class type, Serializer serializer, boolean useClassNameString)
          Registers a class for serialization.
 void removeListener(Kryo.Listener listener)
           
 void removeRemoteEntity(int remoteEntityID)
          Notifies all listeners that the remote entity with the specified ID will no longer be available.
 void setClassLoader(java.lang.ClassLoader classLoader)
          Sets the class loader used to resolve class names when class name Strings are encountered in the serialized bytes.
 void setRegistrationOptional(boolean registrationOptional)
          When true, classes that have not been registered will not throw an exception.
 void setSerializer(java.lang.Class type, Serializer serializer)
           
 Kryo.RegisteredClass writeClass(java.nio.ByteBuffer buffer, java.lang.Class type)
          Writes the specified class to the buffer.
 void writeClassAndObject(java.nio.ByteBuffer buffer, java.lang.Object object)
          Writes the object's class to the buffer, then uses the serializer registered for that class to write the object to the buffer.
 void writeObject(java.nio.ByteBuffer buffer, java.lang.Object object)
          Uses the serializer registered for the object's class to write the object to the buffer.
 void writeObjectData(java.nio.ByteBuffer buffer, java.lang.Object object)
          Uses the serializer registered for the object's class to write the object to the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

version

public static final java.lang.String version
See Also:
Constant Field Values
Constructor Detail

Kryo

public Kryo()
Method Detail

setRegistrationOptional

public void setRegistrationOptional(boolean registrationOptional)
When true, classes that have not been registered will not throw an exception. Instead, handleUnregisteredClass(Class) will be called. Default is false.


register

public Kryo.RegisteredClass register(java.lang.Class type,
                                     Serializer serializer,
                                     boolean useClassNameString)
Registers a class for serialization.

If useClassNameString is true, the first time an object of the specified type is encountered, the class name String will be written to the serialized bytes. Each appearance in the graph after the first is stored as an integer ordinal.

If useClassNameString is false, the class is assigned an ordinal which will be written to the serialized bytes for objects of the specified type. This is more efficient than using the class name String, but has the drawback that the exact same classes must be registered in exactly the same order when the class is deserialized.

By default, primitive types, primitive wrappers, and java.lang.String are registered. All other classes must be registered before they can be serialized. Note that JDK classes such as ArrayList, HashMap, etc and even array classes such as "int[].class" or "short[][].class" must be registered. Optional registration can be enabled to handle unregistered classes as they are encountered.

The Serializer specified will be used to serialize and deserialize objects of the specified type. Note that a serializer can be wrapped with a Compressor for compression and/or encoding.

If the class is already registered, the serializer will be changed.

See Also:
register(Class)

register

public Kryo.RegisteredClass register(java.lang.Class type,
                                     Serializer serializer)
Registers a class with an ordinal.

See Also:
register(Class, Serializer, boolean)

register

public Kryo.RegisteredClass register(java.lang.Class type)
Registers a class with an ordinal, automatically determining the serializer to use. The serializer returned by newSerializer(Class) is used.

Note that some serializers allow additional information to be specified to make serialization more efficient in some cases (eg, ArraySerializer.setElementsCanBeNull(boolean)). To use these features, call register(Class, Serializer) with the configured serializer.

See Also:
register(Class, Serializer, boolean)

register

public void register(java.lang.Class type,
                     Kryo.RegisteredClass registeredClass)
Registers a class with the ordinal of the specified registered class. This is useful when many classes can be serialized with the same serializer instance, such as when code generation is being used to wrap the actual class being serialized.


newSerializer

public Serializer newSerializer(java.lang.Class type)
Returns a serializer for the specified type, determined according to this table:

Type Serializer
array (any number of dimensions) ArraySerializer
Enum EnumSerializer
Collection CollectionSerializer
Map MapSerializer
CustomSerialization CustomSerializer
class with DefaultSerializer annotation serializer specified in annotiation
any other class serializer returned by newDefaultSerializer(Class)

See Also:
register(Class)

newDefaultSerializer

protected Serializer newDefaultSerializer(java.lang.Class type)
Called by newSerializer(Class) when a serializer could not otherwise be determined. The default implementation returns a new FieldSerializer.


getRegisteredClass

public Kryo.RegisteredClass getRegisteredClass(java.lang.Class type)
Returns the registration information for the specified class. If optional registration is true, handleUnregisteredClass(Class) will be called if the class is not registered. Otherwise IllegalArgumentException is thrown.


handleUnregisteredClass

protected void handleUnregisteredClass(java.lang.Class type)
If optional registration is true, this method is called the first time an unregistered class is encountered. The default implementation registers the class to use the class name String in the serialized bytes.

See Also:
register(Class, Serializer, boolean)

getRegisteredClass

public Kryo.RegisteredClass getRegisteredClass(int classID)

setClassLoader

public void setClassLoader(java.lang.ClassLoader classLoader)
Sets the class loader used to resolve class names when class name Strings are encountered in the serialized bytes.


getClassLoader

public java.lang.ClassLoader getClassLoader()

getSerializer

public Serializer getSerializer(java.lang.Class type)

setSerializer

public void setSerializer(java.lang.Class type,
                          Serializer serializer)

writeClass

public Kryo.RegisteredClass writeClass(java.nio.ByteBuffer buffer,
                                       java.lang.Class type)
Writes the specified class to the buffer. Either a String or an int will be written, depending on how the class was registered.

Parameters:
type - Can be null (writes a special ID for a null object).
Returns:
The registered information for the class that was written, or null of the specified class was null.

readClass

public Kryo.RegisteredClass readClass(java.nio.ByteBuffer buffer)
Reads the class from the buffer.

Returns:
The registered information for the class that was read, or null if the data read from the buffer represented a null object.

writeClassAndObject

public void writeClassAndObject(java.nio.ByteBuffer buffer,
                                java.lang.Object object)
Writes the object's class to the buffer, then uses the serializer registered for that class to write the object to the buffer.

Parameters:
object - Can be null (writes a special ID for a null object instead).
Throws:
SerializationException - if an error occurred during serialization.

writeObject

public void writeObject(java.nio.ByteBuffer buffer,
                        java.lang.Object object)
Uses the serializer registered for the object's class to write the object to the buffer.

Parameters:
object - Can be null (writes a special ID for a null object instead).
Throws:
SerializationException - if an error occurred during serialization.

writeObjectData

public void writeObjectData(java.nio.ByteBuffer buffer,
                            java.lang.Object object)
Uses the serializer registered for the object's class to write the object to the buffer.

Parameters:
object - Cannot be null.
Throws:
SerializationException - if an error occurred during serialization.

readClassAndObject

public java.lang.Object readClassAndObject(java.nio.ByteBuffer buffer)
Reads a class from the buffer and uses the serializer registered for that class to read an object from the buffer.

Returns:
The deserialized object, or null if the object read from the buffer was null.
Throws:
SerializationException - if an error occurred during deserialization.

readObject

public <T> T readObject(java.nio.ByteBuffer buffer,
                        java.lang.Class<T> type)
Uses the serializer registered for the specified class to read an object from the buffer.

Returns:
The deserialized object, or null if the object read from the buffer was null.
Throws:
SerializationException - if an error occurred during deserialization.

readObjectData

public <T> T readObjectData(java.nio.ByteBuffer buffer,
                            java.lang.Class<T> type)
Uses the serializer registered for the specified class to read an object from the buffer.

Returns:
The deserialized object, never null.
Throws:
SerializationException - if an error occurred during deserialization.

addListener

public void addListener(Kryo.Listener listener)

removeListener

public void removeListener(Kryo.Listener listener)

removeRemoteEntity

public void removeRemoteEntity(int remoteEntityID)
Notifies all listeners that the remote entity with the specified ID will no longer be available.

See Also:
Context.getRemoteEntityID(), addListener(Listener)

newInstance

public <T> T newInstance(java.lang.Class<T> type)
Returns an instance of the specified class. Serializers that want to allow object construction to be customized by a subclass should use Serializer.newInstance(Kryo, Class) instead of calling this method directly.

Throws:
SerializationException - if the class could not be constructed.

isFinal

public static boolean isFinal(java.lang.Class type)
Returns true if the specified type is final, or if it is an array of a final type.


getContext

public static Context getContext()
Returns the thread local context for serialization and deserialization.

See Also:
Context


Copyright © 2011. All Rights Reserved.