SerializationUtils¶
- public class org.apache.commons.lang3.SerializationUtils[source]¶
Performs 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#
- See also:
org.apache.commons.io.serialization.ValidatingObjectInputStream
- Since:
1.0
Constructors¶
Deprecated. SerializationUtils instances should NOT be constructed in standard programming. |
- @Deprecated public SerializationUtils()[source]¶
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.
- Since:
2.0
Methods¶
Deep clones an |
|
Deserializes a single |
|
Deserializes an |
|
Checks that the specified object reference is not |
|
Performs a serialization roundtrip. |
|
Serializes an |
|
Serializes an |
- public static <T extends Serializable> T clone(T object)[source]¶
Deep clones an
Objectusing 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 – The
Serializableobject to clone.- Returns:
The cloned object.
- Throws:
org.apache.commons.lang3.SerializationException – Thrown if the serialization fails.
- See also:
- public static <T> T deserialize(byte[] objectData)[source]¶
Deserializes a single
Objectfrom 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 if
objectDataisnull.org.apache.commons.lang3.SerializationException – Thrown if the serialization fails.
- See also:
org.apache.commons.io.serialization.ValidatingObjectInputStream
- @SuppressWarnings({"resource"}) public static <T> T deserialize(InputStream inputStream)[source]¶
Deserializes an
Objectfrom 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 if
inputStreamisnull.org.apache.commons.lang3.SerializationException – Thrown if the serialization fails.
- See also:
org.apache.commons.io.serialization.ValidatingObjectInputStream
- public static <T> T requireNonNull(T obj, String message) throws InvalidObjectException[source]¶
Checks that the specified object reference is not
nulland throws a customizedInvalidObjectExceptionif it is. This method is designed primarily for doing state validation inSerializableclass’sreadObject(ObjectInputStream)methods.- Parameters:
obj – The object reference to check for nullity.
message – detail message to be used in the event that a
InvalidObjectExceptionis thrown.
- Type parameters:
<T> – the type of the reference.
- Returns:
objif notnull.- Throws:
java.io.InvalidObjectException – Thrown if
objisnull.- See also:
- Since:
3.21.0
- @SuppressWarnings({"unchecked"}) public static <T extends Serializable> T roundtrip(T obj)[source]¶
Performs a serialization roundtrip. Serializes and deserializes the given object, great for testing objects that implement
Serializable.- Type parameters:
<T> – The type of the object involved.
- Parameters:
obj – The object to roundtrip.
- Returns:
The serialized and deserialized object.
- See also:
- Since:
3.3
- public static byte[] serialize(Serializable obj)[source]¶
Serializes an
Objectto a byte array for storage/serialization.- Parameters:
obj – The object to serialize to bytes.
- Returns:
A byte[] with the converted Serializable.
- Throws:
org.apache.commons.lang3.SerializationException – Thrown if the serialization fails.
- See also:
- @SuppressWarnings({"resource"}) public static void serialize(Serializable obj, OutputStream outputStream)[source]¶
Serializes an
Objectto 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 if
outputStreamisnull.org.apache.commons.lang3.SerializationException – Thrown if the serialization fails.
- See also: