Class Serializer
java.lang.Object
com.jme3.network.serializing.Serializer
- Direct Known Subclasses:
ArraySerializer, BooleanSerializer, ByteSerializer, CharSerializer, ClientRegistrationMessage.ClientRegistrationSerializer, CollectionSerializer, DateSerializer, DisconnectMessage.DisconnectSerializer, DoubleSerializer, EnumSerializer, FieldSerializer, FloatSerializer, GZIPSerializer, IntSerializer, LongSerializer, MapSerializer, RmiSerializer, SavableSerializer, SerializableSerializer, ShortSerializer, StringSerializer, Vector3Serializer, ZIPSerializer
The main serializer class, which will serialize objects such that
they can be sent across the network. Serializing classes should extend
this to provide their own serialization.
- Author:
- Lars Wesselius
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic SerializergetExactSerializer(Class cls) Returns the serializer registered directly for the specified class.static SerializerRegistrationReturns the registration directly associated with the specified class.static SerializergetSerializer(Class cls) Returns the serializer for the specified class using the configured miss behavior.static SerializergetSerializer(Class cls, boolean failOnMiss) Returns the serializer for the specified class.static SerializerRegistrationReturns the registration for the specified class using the configured miss behavior.static SerializerRegistrationgetSerializerRegistration(Class cls, boolean failOnMiss) Returns the registration for the specified class.static Collection<SerializerRegistration> Returns the ordered collection of current serializer registrations.static voidReinitializes the global serializer registry with the built-in registrations.voidinitialize(Class clazz) Registration for when a serializer may need to cache something.static booleanReturns whether the registry is locked against new registrations.static SerializerRegistrationreadClass(ByteBuffer buffer) Read the class from given buffer and return its SerializerRegistration.static ObjectreadClassAndObject(ByteBuffer buffer) Read the class and the object.abstract <T> TreadObject(ByteBuffer data, Class<T> c) Read an object from the buffer, effectively deserializing it.static SerializerRegistrationregisterClass(Class cls) Registers the specified serializable class.static SerializerRegistrationregisterClass(Class cls, boolean failOnMiss) Registers the specified class.static SerializerRegistrationregisterClass(Class cls, Serializer serializer) Registers a class using the specified serializer instance.static voidregisterClasses(Class... classes) Registers each of the specified classes.static SerializerRegistrationregisterClassForId(short id, Class cls, Serializer serializer) Directly registers a class for a specific ID.static SerializerRegistration[]registerPackage(String pkgName) Deprecated.This cannot be implemented in a reasonable way that works in all deployment methods.static voidsetReadOnly(boolean b) Can put the registry in a read-only state such that additional attempts to register classes will fail.static voidsetStrictRegistration(boolean b) When set to true, classes that do not have intrinsic IDs in their@Serializablewill not be auto-registered during write.static SerializerRegistrationwriteClass(ByteBuffer buffer, Class type) Write a class and return its SerializerRegistration.static voidwriteClassAndObject(ByteBuffer buffer, Object object) Write the class and object.abstract voidwriteObject(ByteBuffer buffer, Object object) Write an object to the buffer, effectively serializing it.
-
Field Details
-
log
Shared logger for serializer infrastructure.
-
-
Constructor Details
-
Serializer
public Serializer()Creates a serializer.
-
-
Method Details
-
initialize
public static void initialize()Reinitializes the global serializer registry with the built-in registrations. -
setStrictRegistration
public static void setStrictRegistration(boolean b) When set to true, classes that do not have intrinsic IDs in their@Serializablewill not be auto-registered during write. Defaults to true since this is almost never desired behavior with the way this code works. Set to false to get the old permissive behavior.- Parameters:
b- true to forbid automatic registration for classes without fixed ids
-
registerClass
Registers the specified serializable class.- Parameters:
cls- the class to register- Returns:
- the resulting serializer registration
-
registerClasses
Registers each of the specified classes.- Parameters:
classes- the classes to register
-
setReadOnly
public static void setReadOnly(boolean b) Can put the registry in a read-only state such that additional attempts to register classes will fail. This can be used by servers to lock the registry to avoid accidentally registering classes after a full registry set has been compiled.- Parameters:
b- true to lock the registry against future registrations
-
isReadOnly
public static boolean isReadOnly()Returns whether the registry is locked against new registrations.- Returns:
- true if the registry is read-only
-
registerClassForId
Directly registers a class for a specific ID. Generally, use the regular registerClass() method. This method is intended for framework code that might be maintaining specific ID maps across client and server.- Parameters:
id- the serializer id to bind to the classcls- the class being registeredserializer- the serializer instance to use for the class- Returns:
- the created serializer registration
-
registerClass
Registers the specified class. The failOnMiss flag controls whether this method returns null for failed registration or throws an exception.- Parameters:
cls- the class to registerfailOnMiss- true to throw if the class is not serializable- Returns:
- the serializer registration, or null if registration is skipped
-
registerPackage
Deprecated.This cannot be implemented in a reasonable way that works in all deployment methods.- Parameters:
pkgName- the package name to scan- Returns:
- the registrations created for classes found in the package
-
registerClass
Registers a class using the specified serializer instance.- Parameters:
cls- the class to registerserializer- the serializer instance to bind- Returns:
- the resulting serializer registration
-
getExactSerializer
Returns the serializer registered directly for the specified class.- Parameters:
cls- the class to resolve- Returns:
- the directly registered serializer
-
getSerializer
Returns the serializer for the specified class using the configured miss behavior.- Parameters:
cls- the class to resolve- Returns:
- the serializer for the class
-
getSerializer
Returns the serializer for the specified class.- Parameters:
cls- the class to resolvefailOnMiss- true to fail if no serializer can be resolved- Returns:
- the serializer for the class
-
getSerializerRegistrations
Returns the ordered collection of current serializer registrations.- Returns:
- the serializer registrations
-
getExactSerializerRegistration
Returns the registration directly associated with the specified class.- Parameters:
cls- the class to resolve- Returns:
- the direct registration, or
null
-
getSerializerRegistration
Returns the registration for the specified class using the configured miss behavior.- Parameters:
cls- the class to resolve- Returns:
- the resolved serializer registration
-
getSerializerRegistration
Returns the registration for the specified class.- Parameters:
cls- the class to resolvefailOnMiss- true to fail if no registration can be resolved- Returns:
- the resolved serializer registration
-
readClass
Read the class from given buffer and return its SerializerRegistration.- Parameters:
buffer- The buffer to read from.- Returns:
- The SerializerRegistration, or null if non-existent.
-
readClassAndObject
Read the class and the object.- Parameters:
buffer- Buffer to read from.- Returns:
- The Object that was read.
- Throws:
IOException- If serialization failed.
-
writeClass
Write a class and return its SerializerRegistration.- Parameters:
buffer- The buffer to write the given class to.type- The class to write.- Returns:
- The SerializerRegistration that's registered to the class.
- Throws:
IOException- If writing to the buffer fails.
-
writeClassAndObject
Write the class and object.- Parameters:
buffer- The buffer to write to.object- The object to write.- Throws:
IOException- If serializing fails.
-
readObject
Read an object from the buffer, effectively deserializing it.- Type Parameters:
T- The object type expected from the serializer.- Parameters:
data- The buffer to read from.c- The class of the object.- Returns:
- The object read.
- Throws:
IOException- If deserializing fails.
-
writeObject
Write an object to the buffer, effectively serializing it.- Parameters:
buffer- The buffer to write to.object- The object to serialize.- Throws:
IOException- If serializing fails.
-
initialize
Registration for when a serializer may need to cache something. Override to use.- Parameters:
clazz- The class that has been registered to the serializer.
-