FileDocCategorySizeDatePackage
SerializationHelper.javaAPI DocHibernate 3.2.510723Mon Jan 30 16:51:28 GMT 2006org.hibernate.util

SerializationHelper

public final class SerializationHelper extends Object

Assists with the serialization process and performs additional functionality based on serialization.

  • Deep clone using serialization
  • Serialize managing finally and IOException
  • Deserialize managing finally and IOException

This class throws exceptions for invalid null inputs. Each method documents its behaviour in more detail.

author
Nissim Karpenstein
author
Janek Bogucki
author
Daniel Rall
author
Stephen Colebourne
author
Jeff Varszegi
author
Gary Gregory
since
1.0
version
$Id: SerializationHelper.java 9180 2006-01-30 23:51:27Z steveebersole $

Fields Summary
private static final Log
log
Constructors Summary
private SerializationHelper()


      
Methods Summary
public static java.lang.Objectclone(java.io.Serializable object)

Deep clone an Object using serialization.

This is many times slower than writing clone methods by hand on all objects in your object graph. However, for complex object graphs, or for those that don't support deep cloning this can be a simple alternative implementation. Of course all the objects must be Serializable.

param
object the Serializable object to clone
return
the cloned object
throws
SerializationException (runtime) if the serialization fails

	    log.trace("Starting clone through serialization");
        return deserialize( serialize(object) );
    
public static java.lang.Objectdeserialize(java.io.InputStream inputStream)

Deserializes an Object from the specified stream.

The stream will be closed once the object is written. This avoids the need for a finally clause, and maybe also exception handling, in the application code.

The stream passed in is not buffered internally within this method. This is the responsibility of your application if desired.

param
inputStream the serialized object input stream, must not be null
return
the deserialized object
throws
IllegalArgumentException if inputStream is null
throws
SerializationException (runtime) if the serialization fails

        if (inputStream == null) {
            throw new IllegalArgumentException("The InputStream must not be null");
        }

		log.trace("Starting deserialization of object");

        CustomObjectInputStream in = null;
        try {
            // stream closed in the finally
            in = new CustomObjectInputStream(inputStream);
            return in.readObject();

        }
        catch (ClassNotFoundException ex) {
            throw new SerializationException("could not deserialize", ex);
        }
        catch (IOException ex) {
            throw new SerializationException("could not deserialize", ex);
        }
        finally {
            try {
                if (in != null) in.close();
            }
            catch (IOException ex) {}
        }
    
public static java.lang.Objectdeserialize(byte[] objectData)

Deserializes a single Object from an array of bytes.

param
objectData the serialized object, must not be null
return
the deserialized object
throws
IllegalArgumentException if objectData is null
throws
SerializationException (runtime) if the serialization fails

        if (objectData == null) {
            throw new IllegalArgumentException("The byte[] must not be null");
        }
        ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
        return deserialize(bais);
    
public static voidserialize(java.io.Serializable obj, java.io.OutputStream outputStream)

Serializes an Object to the specified stream.

The stream will be closed once the object is written. This avoids the need for a finally clause, and maybe also exception handling, in the application code.

The stream passed in is not buffered internally within this method. This is the responsibility of your application if desired.

param
obj the object to serialize to bytes, may be null
param
outputStream the stream to write to, must not be null
throws
IllegalArgumentException if outputStream is null
throws
SerializationException (runtime) if the serialization fails

        if (outputStream == null) {
            throw new IllegalArgumentException("The OutputStream must not be null");
        }

	    if ( log.isTraceEnabled() ) {
		    if ( Hibernate.isInitialized( obj ) ) {
	            log.trace( "Starting serialization of object [" + obj + "]" );
		    }
		    else {
			    log.trace( "Starting serialization of [uninitialized proxy]" );
		    }
	    }

        ObjectOutputStream out = null;
        try {
            // stream closed in the finally
            out = new ObjectOutputStream(outputStream);
            out.writeObject(obj);

        }
        catch (IOException ex) {
            throw new SerializationException("could not serialize", ex);
        }
        finally {
            try {
                if (out != null) out.close();
            }
            catch (IOException ignored) {}
        }
    
public static byte[]serialize(java.io.Serializable obj)

Serializes an Object to a byte array for storage/serialization.

param
obj the object to serialize to bytes
return
a byte[] with the converted Serializable
throws
SerializationException (runtime) if the serialization fails

        ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
        serialize(obj, baos);
        return baos.toByteArray();