public class Kryo
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
Kryo.Listener
Provides notification of
Kryo events. |
static class |
Kryo.RegisteredClass
Holds the registration information for a class.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
version |
Constructor and Description |
---|
Kryo() |
Modifier and Type | Method and Description |
---|---|
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) |
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.
|
<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.
|
public static final java.lang.String version
public void setRegistrationOptional(boolean registrationOptional)
registered
will not throw an exception. Instead,
handleUnregisteredClass(Class)
will be called. Default is false.public Kryo.RegisteredClass register(java.lang.Class type, Serializer serializer, boolean useClassNameString)
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.
register(Class)
public Kryo.RegisteredClass register(java.lang.Class type, Serializer serializer)
register(Class, Serializer, boolean)
public Kryo.RegisteredClass register(java.lang.Class type)
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.
register(Class, Serializer, boolean)
public void register(java.lang.Class type, Kryo.RegisteredClass registeredClass)
public Serializer newSerializer(java.lang.Class type)
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) |
register(Class)
public Kryo.RegisteredClass getRegisteredClass(java.lang.Class type)
optional
registration
is true, handleUnregisteredClass(Class)
will be called if the class is not registered. Otherwise
IllegalArgumentException is thrown.public Kryo.RegisteredClass getRegisteredClass(int classID)
public void setClassLoader(java.lang.ClassLoader classLoader)
public java.lang.ClassLoader getClassLoader()
public Serializer getSerializer(java.lang.Class type)
public void setSerializer(java.lang.Class type, Serializer serializer)
public Kryo.RegisteredClass writeClass(java.nio.ByteBuffer buffer, java.lang.Class type)
registered
.type
- Can be null (writes a special ID for a null object).public Kryo.RegisteredClass readClass(java.nio.ByteBuffer buffer)
public void writeClassAndObject(java.nio.ByteBuffer buffer, java.lang.Object object)
object
- Can be null (writes a special ID for a null object instead).SerializationException
- if an error occurred during serialization.public void writeObject(java.nio.ByteBuffer buffer, java.lang.Object object)
object
- Can be null (writes a special ID for a null object instead).SerializationException
- if an error occurred during serialization.public void writeObjectData(java.nio.ByteBuffer buffer, java.lang.Object object)
object
- Cannot be null.SerializationException
- if an error occurred during serialization.public java.lang.Object readClassAndObject(java.nio.ByteBuffer buffer)
SerializationException
- if an error occurred during deserialization.public <T> T readObject(java.nio.ByteBuffer buffer, java.lang.Class<T> type)
SerializationException
- if an error occurred during deserialization.public <T> T readObjectData(java.nio.ByteBuffer buffer, java.lang.Class<T> type)
SerializationException
- if an error occurred during deserialization.public void addListener(Kryo.Listener listener)
public void removeListener(Kryo.Listener listener)
public void removeRemoteEntity(int remoteEntityID)
public <T> T newInstance(java.lang.Class<T> type)
Serializer.newInstance(Kryo, Class)
instead of calling this method directly.SerializationException
- if the class could not be constructed.public static boolean isFinal(java.lang.Class type)