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

TserializeAndUnserialize(T t)
serialize And Unserialize
try {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    new ObjectOutputStream(os).writeObject(t);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    @SuppressWarnings("unchecked")
    T ret = (T) (new ObjectInputStream(is)).readObject();
    return ret;
} catch (IOException e) {
...
voidserializeDeserialize(final Serializable object)
serialize Deserialize
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1000);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
objectOutputStream.close();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
Object deserialized = objectInputStream.readObject();
objectInputStream.close();
...
ObjectserializeDeserialize(Object object)
Serializes and deserializes an object.
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(byteOutput));
out.writeObject(object);
out.flush();
byte[] outputBytes = byteOutput.toByteArray();
ObjectInputStream in = new ObjectInputStream(
        new BufferedInputStream(new ByteArrayInputStream(outputBytes)));
return in.readObject();
...
TserializeDeserialize(T obj)
Tests if something is serializable.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);
    final byte[] data = baos.toByteArray();
    ByteArrayInputStream baip = new ByteArrayInputStream(data);
    ObjectInputStream ois = new ObjectInputStream(baip);
    return (T) ois.readObject();
...
TserializeThenDeserialize(T object)
serialize Then Deserialize
return deserialize((Class<T>) object.getClass(), serialize(object));