@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 java.lang.Object
ObjectSerializationHandler
class implements methods for
handling the serialization and deserialization of objects.Serializable
Constructor and Description |
---|
ObjectSerializationHandler() |
Modifier and Type | Method and Description |
---|---|
static java.lang.Object |
convertFromBytes(byte[] serialized)
Takes a byte array produced by convertToBytes and returns the Object from
the serialized byte array.
|
static byte[] |
convertToBytes(java.io.Serializable object)
Converts the given Object into an array of bytes.
|
static java.lang.Object |
readFromFile(java.io.File file)
Reads a Java serialized Object from the given File and returns it.
|
static java.lang.Object |
readFromStream(java.io.BufferedInputStream stream)
Reads a Java serialized Object from the given stream and returns it.
|
static void |
writeObjectToFile(java.io.File file,
java.io.Serializable object)
Writes a Java serialized Object to the given file.
|
public static byte[] convertToBytes(java.io.Serializable object) throws java.io.IOException
object
- The Object to convert to bytes. If the given object is null
then null is returned.java.io.IOException
- If there is an error in serialization. Typically from when
the object or one of the objects it contains is not
Serializable.public static java.lang.Object convertFromBytes(byte[] serialized) throws java.io.IOException, java.lang.ClassNotFoundException
serialized
- The array of bytes containing the Object (and only the
Object).java.io.IOException
- If there was an error in deserialization.java.lang.ClassNotFoundException
- If the class for the object could not be found.public static java.lang.Object readFromFile(java.io.File file) throws java.lang.ClassNotFoundException, java.io.IOException
If a null
file is passed as a parameter, a
null
value is returned.
file
- The File to read the Object from.java.lang.ClassNotFoundException
- If a class in the file cannot be found.java.io.IOException
- If there is any other type of error reading from the file.public static java.lang.Object readFromStream(java.io.BufferedInputStream stream) throws java.lang.ClassNotFoundException, java.io.IOException
If a null
stream is passed as a parameter, a
null
value is returned.
stream
- The BufferedInputStream to read the Object from.java.lang.ClassNotFoundException
- If a class in the stream cannot be found.java.io.IOException
- If there is any other type of error reading from the stream.public static void writeObjectToFile(java.io.File file, java.io.Serializable object) throws java.io.IOException
file
- File to write the object intoobject
- Serializable object to write into filejava.io.IOException
- if the object cannot be written into the file