Top Description Inners Fields Constructors Methods
org.hibernate.util

public final Class SerializationHelper

extends Object
Class Inheritance
Imports
java.io.ByteArrayInputStream, .ByteArrayOutputStream, .IOException, .InputStream, .ObjectOutputStream, .OutputStream, .Serializable, .ObjectStreamClass, .ObjectInputStream, org.hibernate.type.SerializationException, org.hibernate.Hibernate, org.slf4j.Logger, .LoggerFactory

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

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

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SerializationHelper.CustomObjectInputStream

By default, to resolve the classes being deserialized JDK serialization uses the classes loader which loaded the class which initiated the deserialization call.

Field Summary

Modifier and TypeField and Description
private static final Logger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Object

Returns:

the cloned object
clone
(Serializable
the Serializable object to clone
object
)

Deep clone an Object using serialization.

public static ClassLoader

Returns:

The current TCCL
defaultClassLoader
()

Returns the Thread Context ClassLoader (TCCL).

public static Object

Returns:

the deserialized object
deserialize
(InputStream
the serialized object input stream, must not be null
inputStream
)

Deserializes an object from the specified stream using the Thread Context ClassLoader (TCCL).

public static Object

Returns:

the deserialized object
deserialize
(InputStream
the serialized object input stream, must not be null
inputStream
,
ClassLoader
The classloader to use
loader
)

Deserializes an object from the specified stream using the Thread Context ClassLoader (TCCL).

public static Object

Returns:

the deserialized object
deserialize
(byte[]
the serialized object, must not be null
objectData
)

Deserializes an object from an array of bytes using the Thread Context ClassLoader (TCCL).

public static Object

Returns:

the deserialized object
deserialize
(byte[]
the serialized object, must not be null
objectData
,
ClassLoader
The classloader to use
loader
)

Deserializes an object from an array of bytes.

public static Object
doDeserialize(InputStream inputStream, ClassLoader loader, ClassLoader fallbackLoader1, ClassLoader fallbackLoader2)

public static ClassLoader
public static void
serialize(Serializable
the object to serialize to bytes, may be null
obj
,
OutputStream
the stream to write to, must not be null
outputStream
)

Serializes an Object to the specified stream.

public static byte[]

Returns:

a byte[] with the converted Serializable
serialize
(Serializable
the object to serialize to bytes
obj
)

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

private static InputStream
wrap(byte[] objectData)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

logback to summary
private static final Logger log

Constructor Detail

SerializationHelperback to summary
private SerializationHelper()

Method Detail

cloneback to summary
public static Object clone(Serializable object) throws SerializationException

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.

Parameters
object:Serializable

the Serializable object to clone

Returns:Object

the cloned object

Exceptions
SerializationException:
(runtime) if the serialization fails
defaultClassLoaderback to summary
public static ClassLoader defaultClassLoader()

Returns the Thread Context ClassLoader (TCCL).

Returns:ClassLoader

The current TCCL

deserializeback to summary
public static Object deserialize(InputStream inputStream) throws SerializationException

Deserializes an object from the specified stream using the Thread Context ClassLoader (TCCL).

Delegates to doDeserialize

Parameters
inputStream:InputStream

the serialized object input stream, must not be null

Returns:Object

the deserialized object

Exceptions
SerializationException:
(runtime) if the serialization fails
IllegalArgumentException:
if inputStream is null
deserializeback to summary
public static Object deserialize(InputStream inputStream, ClassLoader loader) throws SerializationException

Deserializes an object from the specified stream using the Thread Context ClassLoader (TCCL). If there is no TCCL set, the classloader of the calling class is used.

The stream will be closed once the object is read. 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 the caller, if desired.

Parameters
inputStream:InputStream

the serialized object input stream, must not be null

loader:ClassLoader

The classloader to use

Returns:Object

the deserialized object

Exceptions
SerializationException:
(runtime) if the serialization fails
IllegalArgumentException:
if inputStream is null
deserializeback to summary
public static Object deserialize(byte[] objectData) throws SerializationException

Deserializes an object from an array of bytes using the Thread Context ClassLoader (TCCL). If there is no TCCL set, the classloader of the calling class is used.

Delegates to deserialize(byte[], ClassLoader)

Parameters
objectData:byte[]

the serialized object, must not be null

Returns:Object

the deserialized object

Exceptions
SerializationException:
(runtime) if the serialization fails
IllegalArgumentException:
if objectData is null
deserializeback to summary
public static Object deserialize(byte[] objectData, ClassLoader loader) throws SerializationException

Deserializes an object from an array of bytes.

Delegates to deserialize(java.io.InputStream, ClassLoader) using a ByteArrayInputStream to wrap the array.

Parameters
objectData:byte[]

the serialized object, must not be null

loader:ClassLoader

The classloader to use

Returns:Object

the deserialized object

Exceptions
SerializationException:
(runtime) if the serialization fails
IllegalArgumentException:
if objectData is null
doDeserializeback to summary
public static Object doDeserialize(InputStream inputStream, ClassLoader loader, ClassLoader fallbackLoader1, ClassLoader fallbackLoader2) throws SerializationException
hibernateClassLoaderback to summary
public static ClassLoader hibernateClassLoader()
serializeback to summary
public static void serialize(Serializable obj, OutputStream outputStream) throws SerializationException

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.

Parameters
obj:Serializable

the object to serialize to bytes, may be null

outputStream:OutputStream

the stream to write to, must not be null

Exceptions
SerializationException:
(runtime) if the serialization fails
IllegalArgumentException:
if outputStream is null
serializeback to summary
public static byte[] serialize(Serializable obj) throws SerializationException

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

Parameters
obj:Serializable

the object to serialize to bytes

Returns:byte[]

a byte[] with the converted Serializable

Exceptions
SerializationException:
(runtime) if the serialization fails
wrapback to summary
private static InputStream wrap(byte[] objectData)
org.hibernate.util back to summary

private final Class SerializationHelper.CustomObjectInputStream

extends ObjectInputStream
Class Inheritance

By default, to resolve the classes being deserialized JDK serialization uses the classes loader which loaded the class which initiated the deserialization call. Here that would be hibernate classes. However, there are cases where that is not the correct class loader to use; mainly here we are worried about deserializing user classes in environments (app servers, etc) where Hibernate is on a parent classes loader. To facilitate for that we allow passing in the class loader we should use.

Field Summary

Modifier and TypeField and Description
private final ClassLoader
private final ClassLoader
private final ClassLoader

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private boolean
protected Class<T>
resolveClass(ObjectStreamClass
an instance of class ObjectStreamClass
v
)

Overrides java.io.ObjectInputStream.resolveClass.

Load the local class equivalent of the specified stream class description.
Inherited from java.io.ObjectInputStream:
availableclosedefaultReadObjectenableResolveObjectgetObjectInputFilterreadreadreadBooleanreadBytereadCharreadClassDescriptorreadDoublereadFieldsreadFloatreadFullyreadFullyreadIntreadLinereadLongreadObjectreadObjectOverridereadShortreadStreamHeaderreadUnsharedreadUnsignedBytereadUnsignedShortreadUTFregisterValidationresolveObjectresolveProxyClasssetObjectInputFilterskipBytes

Field Detail

loader1back to summary
private final ClassLoader loader1
loader2back to summary
private final ClassLoader loader2
loader3back to summary
private final ClassLoader loader3

Constructor Detail

CustomObjectInputStreamback to summary
private CustomObjectInputStream(InputStream in, ClassLoader loader1, ClassLoader loader2, ClassLoader loader3) throws IOException

Method Detail

differentback to summary
private boolean different(ClassLoader one, ClassLoader other)
resolveClassback to summary
protected Class<T> resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException

Overrides java.io.ObjectInputStream.resolveClass.

Doc from java.io.ObjectInputStream.resolveClass.

Load the local class equivalent of the specified stream class description. Subclasses may implement this method to allow classes to be fetched from an alternate source.

The corresponding method in ObjectOutputStream is annotateClass. This method will be invoked only once for each unique class in the stream. This method can be implemented by subclasses to use an alternate loading mechanism but must return a Class object. Once returned, if the class is not an array class, its serialVersionUID is compared to the serialVersionUID of the serialized class, and if there is a mismatch, the deserialization fails and an InvalidClassException is thrown.

The default implementation of this method in ObjectInputStream returns the result of calling

Class.forName(desc.getName(), false, loader)
Class.forName(desc.getName(), false, loader)
where loader is the first class loader on the current thread's stack (starting from the currently executing method) that is neither the platform class loader nor its ancestor; otherwise, loader is the platform class loader. If this call results in a ClassNotFoundException and the name of the passed ObjectStreamClass instance is the Java language keyword for a primitive type or void, then the Class object representing that primitive type or void will be returned (e.g., an ObjectStreamClass with the name "int" will be resolved to Integer.TYPE). Otherwise, the ClassNotFoundException will be thrown to the caller of this method.
Parameters
v:ObjectStreamClass

an instance of class ObjectStreamClass

Returns:Class<T>

a Class object corresponding to desc

Exceptions
IOException:
any of the usual Input/Output exceptions.
ClassNotFoundException:
if class of a serialized object cannot be found.