Java Utililty Methods Object Serialize and Deserialize

List of utility methods to do Object Serialize and Deserialize

Description

The list of methods to do Object Serialize and Deserialize are organized into topic(s).

Method

PropertiesdeserializeProperties(byte[] bytes)
Tries to deserialize given bytes array to Properties instance
Properties properties = new Properties();
if (bytes != null) {
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        properties.load(bais);
        bais.close();
    } catch (IOException e) {
return properties;
SerializabledeserializeRaw(byte[] array)
deserialize Raw
byteInput = new ByteArrayInputStream(array);
objectInput = new ObjectInputStream(byteInput);
Serializable o = (Serializable) objectInput.readObject();
return o;
voiddeserializeSafe(byte[] buf, T obj)
deserialize Safe
ByteArrayInputStream bis = null;
try {
    bis = new ByteArrayInputStream(buf);
    ObjectInputStream oin = null;
    try {
        oin = new ObjectInputStream(bis);
        obj.readExternal(oin);
    } finally {
...
shortdeserializeShort(byte[] inbuf, int offset)
Deserialize a short, but do not convert it from little endian in the process
return (short) ((inbuf[offset] & 0xff) << 8 | (inbuf[offset + 1] & 0xff));
ObjectdeserializeStream(final String witness)
Deserializes a specified file.
FileInputStream fileIs = new FileInputStream(witness);
ObjectInputStream objIs = new ObjectInputStream(fileIs);
return objIs.readObject();
ObjectserializeAndDeserialize(Object o)
Serializes and deserializes the given object.
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bytes);
try {
    out.writeObject(o);
} finally {
    out.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
...
ObjectserializeAndDeserialize(Object o)
serialize And Deserialize
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.flush();
baos.flush();
byte[] bytes = baos.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is);
...
ObjectserializeAndDeserialize(Object obj)
serialize And Deserialize
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(obj);
out.flush();
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in = new ObjectInputStream(bin);
obj = in.readObject();
return obj;
...
TserializeAndDeserialize(T input)
Serialize and Deserialize an object Useful for testing if serialization is correctly handled for a class.
final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(byteArrayStream);
out.writeObject(input);
final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteArrayStream.toByteArray()));
@SuppressWarnings("unchecked")
final T result = (T) in.readObject();
out.close();
in.close();
...
TserializeAndDeserialize(T instance)
serialize And Deserialize
return (T) deserialize(serialize(instance));