gov.sandia.cognition.io
Class ObjectSerializationHandler

java.lang.Object
  extended by gov.sandia.cognition.io.ObjectSerializationHandler

@CodeReviews(reviews={@CodeReview(reviewer="Kevin R. Dixon",date="2008-02-08",changesNeeded=false,comments={"Added some calls to close() after Streams were done.","Minor cosmetic changes.","Otherwise, looks fine."}),@CodeReview(reviewer="Jonathan McClain",date="2006-05-11",changesNeeded=true,comments="A few undocumented behaviors, and one bug.",response=@CodeReviewResponse(respondent="Justin Basilico",date="2006-05-16",comments="Bug fixed.",moreChangesNeeded=false))})
public class ObjectSerializationHandler
extends Object

The ObjectSerializationHandler class implements methods for handling the serialization and deserialization of objects.

Since:
1.0
Author:
Justin Basilico
See Also:
Serializable

Constructor Summary
ObjectSerializationHandler()
           
 
Method Summary
static Object convertFromBytes(byte[] serialized)
          Takes a byte array produced by convertToBytes and returns the Object from the serialized byte array.
static byte[] convertToBytes(Serializable object)
          Converts the given Object into an array of bytes.
static Object readFromFile(File file)
          Reads a Java serialized Object from the given File and returns it.
static Object readFromStream(BufferedInputStream stream)
          Reads a Java serialized Object from the given stream and returns it.
static void writeObjectToFile(File file, Serializable object)
          Writes a Java serialized Object to the given file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectSerializationHandler

public ObjectSerializationHandler()
Method Detail

convertToBytes

public static byte[] convertToBytes(Serializable object)
                             throws IOException
Converts the given Object into an array of bytes. The object must be Serializable for this to work.

Parameters:
object - The Object to convert to bytes. If the given object is null then null is returned.
Returns:
The byte array containing the serialized Object.
Throws:
IOException - If there is an error in serialization. Typically from when the object or one of the objects it contains is not Serializable.

convertFromBytes

public static Object convertFromBytes(byte[] serialized)
                               throws IOException,
                                      ClassNotFoundException
Takes a byte array produced by convertToBytes and returns the Object from the serialized byte array.

Parameters:
serialized - The array of bytes containing the Object (and only the Object).
Returns:
The Object deserialized from the given byte array. If the given array is null then null is returned.
Throws:
IOException - If there was an error in deserialization.
ClassNotFoundException - If the class for the object could not be found.

readFromFile

public static Object readFromFile(File file)
                           throws ClassNotFoundException,
                                  IOException
Reads a Java serialized Object from the given File and returns it.

If a null file is passed as a parameter, a null value is returned.

Parameters:
file - The File to read the Object from.
Returns:
The Object read from the given file.
Throws:
ClassNotFoundException - If a class in the file cannot be found.
IOException - If there is any other type of error reading from the file.

readFromStream

public static Object readFromStream(BufferedInputStream stream)
                             throws ClassNotFoundException,
                                    IOException
Reads a Java serialized Object from the given stream and returns it.

If a null stream is passed as a parameter, a null value is returned.

Parameters:
stream - The BufferedInputStream to read the Object from.
Returns:
The Object read from the given stream.
Throws:
ClassNotFoundException - If a class in the stream cannot be found.
IOException - If there is any other type of error reading from the stream.

writeObjectToFile

public static void writeObjectToFile(File file,
                                     Serializable object)
                              throws IOException
Writes a Java serialized Object to the given file.

Parameters:
file - File to write the object into
object - Serializable object to write into file
Throws:
IOException - if the object cannot be written into the file