com.cometway.util
Class ObjectSerializer

java.lang.Object
  extended by com.cometway.util.ObjectSerializer

public class ObjectSerializer
extends java.lang.Object

This class has static methods that serialize and unserialize Objects to and from byte[] arrays. This array will also contain the ObjectHeader with the magic numbers. I highly suggest not modifying the byte[] array unless you know what you are doing.

See Also:
Serializable

Constructor Summary
ObjectSerializer()
           
 
Method Summary
static byte[] serialize(java.lang.Object o)
          This method serializes an Object into a byte[] array.
static java.lang.Object unserialize(byte[] data)
          This method unserializes a byte[] array into a single Object.
static java.lang.Object unserialize(byte[] data, int offset, int length)
          This method unserializes a byte[] array into a single Object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectSerializer

public ObjectSerializer()
Method Detail

unserialize

public static java.lang.Object unserialize(byte[] data)
                                    throws java.io.IOException,
                                           java.io.OptionalDataException,
                                           java.lang.ClassNotFoundException
This method unserializes a byte[] array into a single Object. Any other Objects will not be read.

Parameters:
data - This is the serialied data to unserialize. The entire length of the array is used.
Returns:
Returns the first Objects serialized in the byte[] array.
Throws:
java.io.IOException - Thrown when an I/O Exception occurs while creating the ObjectStream or reading the Object
java.io.OptionalDataException - Thrown when unexpected data is in the byte[] array.
java.lang.ClassNotFoundException - Thrown when the unserialzed object's Class is not found.

unserialize

public static java.lang.Object unserialize(byte[] data,
                                           int offset,
                                           int length)
                                    throws java.io.IOException,
                                           java.io.OptionalDataException,
                                           java.lang.ClassNotFoundException
This method unserializes a byte[] array into a single Object. Any other Objects will not be read.

Parameters:
data - This is the serialied data to unserialize.
length - Up to this many bytes in the array are to be unserialized.
Returns:
Returns the first Objects serialized in the byte[] array.
Throws:
java.io.IOException - Thrown when an I/O Exception occurs while creating the ObjectStream or reading the Object
java.io.OptionalDataException - Thrown when unexpected data is in the byte[] array.
java.lang.ClassNotFoundException - Thrown when the unserialzed object's Class is not found.

serialize

public static byte[] serialize(java.lang.Object o)
                        throws java.io.IOException,
                               java.io.NotSerializableException
This method serializes an Object into a byte[] array. The length of the array is the number of valid bytes read from the ObjectStream.

Parameters:
o - This is the object to serialize.
Returns:
Returns a byte[] array containing the serialized Object.
Throws:
java.io.IOException - Thrown when an I/O Exception occurs while creating the ObjectStream or writing the Object
java.io.NotSerializableException - Thrown if the Object cannot be serializable.