Methods Summary |
---|
public static java.lang.Object | clone(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 .
log.trace("Starting clone through serialization");
return deserialize( serialize(object) );
|
public static java.lang.Object | deserialize(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.
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.Object | deserialize(byte[] objectData)Deserializes a single Object from an array of bytes.
if (objectData == null) {
throw new IllegalArgumentException("The byte[] must not be null");
}
ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
return deserialize(bais);
|
public static void | serialize(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.
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.
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
serialize(obj, baos);
return baos.toByteArray();
|