Class SerializationUtils
- java.lang.Object
-
- org.apache.commons.lang3.SerializationUtils
-
public class SerializationUtils extends java.lang.ObjectPerforms additional functionality for serialization.- Deep clone using serialization
- Serialize managing finally and IOException
- Deserialize managing finally and IOException
This class throws exceptions for invalid
nullinputs. Each method documents its behavior in more detail.If you want to secure deserialization with a whitelist or blacklist, please use Apache Commons IO's
ValidatingObjectInputStream.#ThreadSafe#
- Since:
- 1.0
- See Also:
org.apache.commons.io.serialization.ValidatingObjectInputStream, Java Object Serialization Specification
-
-
Constructor Summary
Constructors Constructor Description SerializationUtils()Deprecated.TODO Make private in 4.0.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends java.io.Serializable>
Tclone(T object)Deep clones anObjectusing serialization.static <T> Tdeserialize(byte[] objectData)Deserializes a singleObjectfrom an array of bytes.static <T> Tdeserialize(java.io.InputStream inputStream)Deserializes anObjectfrom the specified stream.static <T> TrequireNonNull(T obj, java.lang.String message)Checks that the specified object reference is notnulland throws a customizedInvalidObjectExceptionif it is.static <T extends java.io.Serializable>
Troundtrip(T obj)Performs a serialization roundtrip.static byte[]serialize(java.io.Serializable obj)Serializes anObjectto a byte array for storage/serialization.static voidserialize(java.io.Serializable obj, java.io.OutputStream outputStream)Serializes anObjectto the specified stream.
-
-
-
Constructor Detail
-
SerializationUtils
@Deprecated public SerializationUtils()
Deprecated.TODO Make private in 4.0.SerializationUtils instances should NOT be constructed in standard programming. Instead, the class should be used asSerializationUtils.clone(object).This constructor is public to permit tools that require a JavaBean instance to operate.
- Since:
- 2.0
-
-
Method Detail
-
clone
public static <T extends java.io.Serializable> T clone(T object)
Deep clones anObjectusing 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.- Type Parameters:
T- the type of the object involved.- Parameters:
object- TheSerializableobject to clone.- Returns:
- The cloned object.
- Throws:
SerializationException- Thrown if the serialization fails.- See Also:
- Java Object Serialization Specification
-
deserialize
public static <T> T deserialize(byte[] objectData)
Deserializes a singleObjectfrom an array of bytes.If the call site incorrectly types the return value, a
ClassCastExceptionis 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.If you want to secure deserialization with a whitelist or blacklist, please use Apache Commons IO's
ValidatingObjectInputStream.- Type Parameters:
T- the object type to be deserialized.- Parameters:
objectData- The serialized object, must not be null.- Returns:
- The deserialized object.
- Throws:
java.lang.NullPointerException- Thrown ifobjectDataisnull.SerializationException- Thrown if the serialization fails.- See Also:
org.apache.commons.io.serialization.ValidatingObjectInputStream, Java Object Serialization Specification
-
deserialize
public static <T> T deserialize(java.io.InputStream inputStream)
Deserializes anObjectfrom 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
ClassCastExceptionis 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.If you want to secure deserialization with a whitelist or blacklist, please use Apache Commons IO's
ValidatingObjectInputStream.- Type Parameters:
T- the object type to be deserialized.- Parameters:
inputStream- The serialized object input stream, must not be null.- Returns:
- The deserialized object.
- Throws:
java.lang.NullPointerException- Thrown ifinputStreamisnull.SerializationException- Thrown if the serialization fails.- See Also:
org.apache.commons.io.serialization.ValidatingObjectInputStream, Java Object Serialization Specification
-
requireNonNull
public static <T> T requireNonNull(T obj, java.lang.String message) throws java.io.InvalidObjectExceptionChecks that the specified object reference is notnulland throws a customizedInvalidObjectExceptionif it is. This method is designed primarily for doing state validation inSerializableclass'sreadObject(ObjectInputStream)methods.- Type Parameters:
T- the type of the reference.- Parameters:
obj- The object reference to check for nullity.message- detail message to be used in the event that aInvalidObjectExceptionis thrown.- Returns:
objif notnull.- Throws:
java.io.InvalidObjectException- Thrown ifobjisnull.- Since:
- 3.21.0
- See Also:
Serializable, Java Object Serialization Specification
-
roundtrip
public static <T extends java.io.Serializable> T roundtrip(T obj)
Performs a serialization roundtrip. Serializes and deserializes the given object, great for testing objects that implementSerializable.- Type Parameters:
T- The type of the object involved.- Parameters:
obj- The object to roundtrip.- Returns:
- The serialized and deserialized object.
- Since:
- 3.3
- See Also:
- Java Object Serialization Specification
-
serialize
public static byte[] serialize(java.io.Serializable obj)
Serializes anObjectto a byte array for storage/serialization.- Parameters:
obj- The object to serialize to bytes.- Returns:
- A byte[] with the converted Serializable.
- Throws:
SerializationException- Thrown if the serialization fails.- See Also:
- Java Object Serialization Specification
-
serialize
public static void serialize(java.io.Serializable obj, java.io.OutputStream outputStream)Serializes anObjectto 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- The object to serialize to bytes, may be null.outputStream- The stream to write to, must not be null.- Throws:
java.lang.NullPointerException- Thrown ifoutputStreamisnull.SerializationException- Thrown if the serialization fails.- See Also:
- Java Object Serialization Specification
-
-