messif.objects.nio
Class MultiClassSerializator<T>

java.lang.Object
  extended by messif.objects.nio.BinarySerializator
      extended by messif.objects.nio.MultiClassSerializator<T>
Type Parameters:
T - default class used when reading serialized object
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
CachingSerializator

public class MultiClassSerializator<T>
extends BinarySerializator
implements java.io.Serializable

This implements a full-featured BinarySerializator which works on all classes. There is a cost of the class name and constructor/factory method lookup when the objects are deserialized for other than the default class. This serializator checks the serialUIDs of the default class for changes.

See Also:
Serialized Form

Field Summary
protected static byte CLASSNAME_SERIALIZATION
          Constant indicating that the name of the class is written in the stream
protected  java.lang.reflect.Constructor<?> constructor
          The cached constructor for the default class
protected  java.lang.Class<? extends T> defaultClass
          The first class in the predefinedClasses is used as default
protected static byte DEFAULTCLASS_SERIALIZATION
          Constant indicating that the default class is written in the stream
protected  java.lang.reflect.Method factoryMethod
          The cached factory method for the default class
protected static byte JAVA_SERIALIZATION
          Constant indicating that the standard Java serialization is used
 
Fields inherited from class messif.objects.nio.BinarySerializator
log
 
Constructor Summary
MultiClassSerializator(java.lang.Class<? extends T> defaultClass)
          Creates a new instance of MultiClassSerializator.
 
Method Summary
 boolean equals(java.lang.Object obj)
          Indicates whether some other object is "equal to" this serializator.
protected  int getBinarySize(BinarySerializable object)
          Returns the size of the binary-serialized object in bytes.
protected  int getClassIndex(BinarySerializable object)
          Returns the index of the predefined class for this object.
protected  java.lang.reflect.Constructor<?> getConstructor(byte flag, java.lang.Class<?> objectClass)
          Returns a constructor for the specified flag and object class.
 java.lang.Class<? extends T> getDefaultClass()
          Returns a default class that is used for deserialization when a class is not specified.
protected  java.lang.reflect.Method getFactoryMethod(byte flag, java.lang.Class<?> objectClass)
          Returns a factory method for the specified flag and object class.
 int hashCode()
          Returns a hash code value for this serializator.
 T readObject(BinaryInput input)
          Reads an instance from the input using this serializator.
protected
<E> E
readObjectImpl(BinaryInput input, java.lang.Class<E> expectedClass)
          Reads an instance using the proper constructor/factory method.
protected  int write(BinaryOutput output, BinarySerializable object)
          Writes object to this output buffer using binary serialization.
 
Methods inherited from class messif.objects.nio.BinarySerializator
getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getBinarySize, getNativeSerializableConstructor, getNativeSerializableFactoryMethod, getSerialVersionUID, getSerialVersionUIDHash, readBoolean, readBooleanArray, readByte, readByteArray, readChar, readCharArray, readDouble, readDoubleArray, readFloat, readFloatArray, readInt, readIntArray, readLong, readLongArray, readObject, readObject, readObjectSize, readShort, readShortArray, readString, skipObject, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JAVA_SERIALIZATION

protected static final byte JAVA_SERIALIZATION
Constant indicating that the standard Java serialization is used

See Also:
Constant Field Values

DEFAULTCLASS_SERIALIZATION

protected static final byte DEFAULTCLASS_SERIALIZATION
Constant indicating that the default class is written in the stream

See Also:
Constant Field Values

CLASSNAME_SERIALIZATION

protected static final byte CLASSNAME_SERIALIZATION
Constant indicating that the name of the class is written in the stream

See Also:
Constant Field Values

defaultClass

protected final java.lang.Class<? extends T> defaultClass
The first class in the predefinedClasses is used as default


constructor

protected final transient java.lang.reflect.Constructor<?> constructor
The cached constructor for the default class


factoryMethod

protected final transient java.lang.reflect.Method factoryMethod
The cached factory method for the default class

Constructor Detail

MultiClassSerializator

public MultiClassSerializator(java.lang.Class<? extends T> defaultClass)
                       throws java.lang.IllegalArgumentException
Creates a new instance of MultiClassSerializator. If the defaultClass implements the BinarySerializable interface, the constructor/factory method is extracted for deserializing. Otherwise a standard Java serialization will be used. The constructor/factory method of the defaultClass is cached.

Parameters:
defaultClass - the default class that is used for deserialization when a class is not specified
Throws:
java.lang.IllegalArgumentException - if there is an invalid value in predefinedClasses
Method Detail

readObject

public T readObject(BinaryInput input)
             throws java.io.IOException,
                    java.lang.IllegalArgumentException
Reads an instance from the input using this serializator. The default class that is expected to be in the buffer.

Parameters:
input - the buffer to read the instance from
Returns:
an instance of the deserialized object
Throws:
java.io.IOException - if there was an I/O error
java.lang.IllegalArgumentException - if the constructor or the factory method has a wrong prototype

getBinarySize

protected int getBinarySize(BinarySerializable object)
                     throws java.lang.IllegalArgumentException
Returns the size of the binary-serialized object in bytes.

Specified by:
getBinarySize in class BinarySerializator
Parameters:
object - the object to get the size for
Returns:
the size of the binary-serialized object
Throws:
java.lang.IllegalArgumentException - if there was an error using Java standard serialization on the object

write

protected int write(BinaryOutput output,
                    BinarySerializable object)
             throws java.io.IOException
Description copied from class: BinarySerializator
Writes object to this output buffer using binary serialization. The following rules must hold:

Specified by:
write in class BinarySerializator
Parameters:
output - the buffer to write the object to
object - the object to write
Returns:
the number of bytes actually written
Throws:
java.io.IOException - if there was an error using flushChannel

readObjectImpl

protected <E> E readObjectImpl(BinaryInput input,
                               java.lang.Class<E> expectedClass)
                    throws java.io.IOException,
                           java.lang.IllegalArgumentException
Reads an instance using the proper constructor/factory method. First, a byte flag resolving the type of deserialization is read. If the flag indicates that the class name is stored in the input, it is read and checked. Then, the constructor and/or factory method is retrieved via getConstructor(byte, java.lang.Class) and getFactoryMethod(byte, java.lang.Class). Finally, the object is deserialized using the constructor, the factory method or standard Java serialization.

Specified by:
readObjectImpl in class BinarySerializator
Type Parameters:
E - the class that is expected to be in the input
Parameters:
input - the buffer to read the instance from
expectedClass - the class that is expected to be in the buffer
Returns:
an instance of the deserialized object
Throws:
java.io.IOException - if there was an I/O error
java.lang.IllegalArgumentException - if there was a problem getting a valid constructor/factory method

getConstructor

protected java.lang.reflect.Constructor<?> getConstructor(byte flag,
                                                          java.lang.Class<?> objectClass)
                                                   throws java.lang.IllegalArgumentException
Returns a constructor for the specified flag and object class.

Parameters:
flag - the type of deserialization (see constants)
objectClass - the class that is expected to be in the stream
Returns:
a constructor for the specified flag and object class
Throws:
java.lang.IllegalArgumentException - if there was a problem getting the constructor

getFactoryMethod

protected java.lang.reflect.Method getFactoryMethod(byte flag,
                                                    java.lang.Class<?> objectClass)
                                             throws java.lang.IllegalArgumentException
Returns a factory method for the specified flag and object class.

Parameters:
flag - the type of deserialization (see constants)
objectClass - the class that is expected to be in the stream
Returns:
a constructor for the specified flag and object class
Throws:
java.lang.IllegalArgumentException - if there was a problem getting the factory method

getClassIndex

protected int getClassIndex(BinarySerializable object)
Returns the index of the predefined class for this object.

Parameters:
object - the object whose class is looked up
Returns:
the index of the predefined class for this object

getDefaultClass

public java.lang.Class<? extends T> getDefaultClass()
Returns a default class that is used for deserialization when a class is not specified.

Returns:
a default class that is used for deserialization

hashCode

public int hashCode()
Returns a hash code value for this serializator. It is based on the serialVersionUID of the defaultClass.

Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this serializator

equals

public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this serializator. In particular, the other object must be MultiClassSerializator and have the same default class.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the reference object with which to compare
Returns:
true if this serializator is the same as the obj argument; false otherwise.