Top Description Inners Constructors Methods
org.apache.commons.lang3

public Class SerializationUtils

extends Object
Class Inheritance
Imports
java.io.ByteArrayInputStream, .ByteArrayOutputStream, .IOException, .InputStream, .ObjectInputStream, .ObjectOutputStream, .ObjectStreamClass, .OutputStream, .Serializable, java.util.Objects

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

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

#ThreadSafe#

Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
SerializationUtils.ClassLoaderAwareObjectInputStream

Custom specialization of the standard JDK ObjectInputStream that uses a custom ClassLoader to resolve a class.

Constructor Summary

AccessConstructor and Description
public
SerializationUtils()
Deprecated TODO Make private in 4.0.

SerializationUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static <
the type of the object involved.
T extends Serializable
>
T

Returns:

the cloned object.
clone
(final T
the Serializable object to clone.
object
)

Deep clones an Object using serialization.

public static <
the object type to be deserialized.
T
>
T

Returns:

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

Deserializes a single Object from an array of bytes.

public static <
the object type to be deserialized.
T
>
T

Returns:

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

Deserializes an Object from the specified stream.

public static <
the type of the object involved.
T extends Serializable
>
T

Returns:

the serialized and deserialized object.
roundtrip
(final T
the object to roundtrip.
obj
)

Performs a serialization roundtrip.

public static byte[]

Returns:

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

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

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

Serializes an Object to the specified stream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

SerializationUtilsback to summary
public SerializationUtils()

Deprecated

TODO Make private in 4.0.

SerializationUtils instances should NOT be constructed in standard programming. Instead, the class should be used as SerializationUtils.clone(object).

This constructor is public to permit tools that require a JavaBean instance to operate.

Annotations
@Deprecated
Since
2.0

Method Detail

cloneback to summary
public static <T extends Serializable> T clone(final T object)

Deep clones 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
<T>
the type of the object involved.
object:T

the Serializable object to clone.

Returns:T

the cloned object.

Exceptions
SerializationException:
(runtime) if the serialization fails.
deserializeback to summary
public static <T> T deserialize(final byte[] objectData)

Deserializes a single Object from an array of bytes.

If the call site incorrectly types the return value, a ClassCastException is thrown from the call site. Without Generics in this declaration, the call site must type cast and can cause the same ClassCastException. Note that in both cases, the ClassCastException is in the call site, not in this method.

Parameters
<T>
the object type to be deserialized.
objectData:byte[]

the serialized object, must not be null.

Returns:T

the deserialized object.

Exceptions
NullPointerException:
if objectData is null.
SerializationException:
(runtime) if the serialization fails.
deserializeback to summary
public static <T> T deserialize(final 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 the call site incorrectly types the return value, a ClassCastException is thrown from the call site. Without Generics in this declaration, the call site must type cast and can cause the same ClassCastException. Note that in both cases, the ClassCastException is in the call site, not in this method.

Parameters
<T>
the object type to be deserialized.
inputStream:InputStream

the serialized object input stream, must not be null.

Returns:T

the deserialized object.

Annotations
@SuppressWarnings:resource
Exceptions
NullPointerException:
if inputStream is null.
SerializationException:
(runtime) if the serialization fails.
roundtripback to summary
public static <T extends Serializable> T roundtrip(final T obj)

Performs a serialization roundtrip. Serializes and deserializes the given object, great for testing objects that implement Serializable.

Parameters
<T>
the type of the object involved.
obj:T

the object to roundtrip.

Returns:T

the serialized and deserialized object.

Annotations
@SuppressWarnings:unchecked
Since
3.3
serializeback to summary
public static byte[] serialize(final Serializable obj)

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.
serializeback to summary
public static void serialize(final Serializable obj, final 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.

Parameters
obj:Serializable

the object to serialize to bytes, may be null.

outputStream:OutputStream

the stream to write to, must not be null.

Annotations
@SuppressWarnings:resource
Exceptions
NullPointerException:
if outputStream is null.
SerializationException:
(runtime) if the serialization fails.
org.apache.commons.lang3 back to summary

pack-priv final Class SerializationUtils.ClassLoaderAwareObjectInputStream

extends ObjectInputStream
Class Inheritance

Custom specialization of the standard JDK ObjectInputStream that uses a custom ClassLoader to resolve a class. If the specified ClassLoader is not able to resolve the class, the context classloader of the current thread will be used. This way, the standard deserialization work also in web-application containers and application servers, no matter in which of the ClassLoader the particular class that encapsulates serialization/deserialization lives.

For more in-depth information about the problem for which this class here is a workaround, see the JIRA issue LANG-626.

Field Summary

Modifier and TypeField and Description
private final ClassLoader

Constructor Summary

AccessConstructor and Description
pack-priv
ClassLoaderAwareObjectInputStream(final InputStream in, final ClassLoader
classloader to use
classLoader
)

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
protected Class<?>

Returns:

A Class object corresponding to desc.
resolveClass
(final ObjectStreamClass
An instance of class ObjectStreamClass.
desc
)

Overrides java.io.ObjectInputStream.resolveClass.

Overridden version that uses the parameterized ClassLoader or the ClassLoader of the current Thread to resolve the class.

Inherited from java.io.ObjectInputStream:
availableclosedefaultReadObjectenableResolveObjectgetObjectInputFilterreadreadreadBooleanreadBytereadCharreadClassDescriptorreadDoublereadFieldsreadFloatreadFullyreadFullyreadIntreadLinereadLongreadObjectreadObjectOverridereadShortreadStreamHeaderreadUnsharedreadUnsignedBytereadUnsignedShortreadUTFregisterValidationresolveObjectresolveProxyClasssetObjectInputFilterskipBytes

Field Detail

classLoaderback to summary
private final ClassLoader classLoader

Constructor Detail

ClassLoaderAwareObjectInputStreamback to summary
pack-priv ClassLoaderAwareObjectInputStream(final InputStream in, final ClassLoader classLoader) throws IOException

Constructs a new instance.

Parameters
in:InputStream

The InputStream.

classLoader:ClassLoader

classloader to use

Exceptions
IOException:
if an I/O error occurs while reading stream header.
See Also
java.io.ObjectInputStream

Method Detail

resolveClassback to summary
protected Class<?> resolveClass(final ObjectStreamClass desc) throws IOException, ClassNotFoundException

Overrides java.io.ObjectInputStream.resolveClass.

Overridden version that uses the parameterized ClassLoader or the ClassLoader of the current Thread to resolve the class.

Parameters
desc:ObjectStreamClass

An instance of class ObjectStreamClass.

Returns:Class<?>

A Class object corresponding to desc.

Annotations
@Override
Exceptions
IOException:
Any of the usual Input/Output exceptions.
ClassNotFoundException:
If class of a serialized object cannot be found.