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

TserializeAndDeserialize(T obj)
serialize And Deserialize
ByteArrayOutputStream buf = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(buf)) {
    out.writeObject(obj);
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()))) {
    return (T) in.readObject();
TserializeAndDeserialize(T object)
serialize And Deserialize
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
ObjectOutputStream ostream = new ObjectOutputStream(byteOutputStream);
ostream.writeObject(object);
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteOutputStream.toByteArray());
ObjectInputStream istream = new ObjectInputStream(byteInputStream);
return (T) istream.readObject();
TserializeAndDeserialize(T object)
serialize And Deserialize
byte[] array = serialize(object);
return deserialize(array);
TserializeAndDeserialize(T object)
serialize And Deserialize
byte[] bytes = serialize(object);
return (T) deserialize(bytes);
voidserializeAndDeserializeObject(Object myObject)
serialize And Deserialize Object
try {
    deserialzeObject(serializeObject(myObject));
} catch (IOException e) {
    System.out.println("Exception: " + e.toString());
ObjectserializeAndGetObject(Object obj)
serialize And Get Object
return getObjectFromByteArray(getByteObject(obj));
ObjectserializeAndReadObject(File envHome, Object object)
serialize And Read Object
File output = new File(envHome, "configure.out");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(output));
out.writeObject(object);
out.close();
if (!output.exists()) {
    throw new IllegalStateException("Can't create the output for serialized object.");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(output));
...
ObjectserializeAndRecover(Object o)
Serializes an object to a bytes array and then recovers the object from the bytes array.
try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream so = new ObjectOutputStream(bos);
    so.writeObject(o);
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    ObjectInputStream si = new ObjectInputStream(bis);
    return si.readObject();
} catch (Exception e) {
...
ObjectserializeAndRecover(Object o)
serialize And Recover
Object result = null;
File tmp = null;
try {
    tmp = File.createTempFile("test", ".ser");
    FileOutputStream fo = new FileOutputStream(tmp);
    ObjectOutputStream so = new ObjectOutputStream(fo);
    so.writeObject(o);
    so.flush();
...
TserializeAndReturnDeserializedObject(T object)
serialize And Return Deserialized Object
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(object);
out.close();
ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bain);
@SuppressWarnings({ "unchecked" })
T managerRead = (T) in.readObject();
...