Java Utililty Methods Object Deep Copy

List of utility methods to do Object Deep Copy

Description

The list of methods to do Object Deep Copy are organized into topic(s).

Method

voiddeepCopy(File fromDir, File toDir)
Copy the contents of fromDir into toDir (if the latter is missing it will be created)
if (!fromDir.isDirectory() || !fromDir.exists())
    throw new IllegalArgumentException(
            "Invalid source directory " + "(it's either not a directory, or does not exist");
if (toDir.exists() && toDir.isFile())
    throw new IllegalArgumentException(
            "Invalid destination directory, " + "it happens to be a file instead");
if (!toDir.exists())
    if (!toDir.mkdir())
...
VdeepCopy(final Class c, final Serializable s)
deep Copy
if (c == null || s == null)
    throw new NullPointerException();
final ByteArrayOutputStream bs = new ByteArrayOutputStream();
new ObjectOutputStream(bs).writeObject(s);
final ByteArrayInputStream bi = new ByteArrayInputStream(bs.toByteArray());
final V v = c.cast(new ObjectInputStream(bi).readObject());
bs.close();
bi.close();
...
ObjectdeepCopy(final Object oldObj)
deep Copy
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    oos = new ObjectOutputStream(bos);
    oos.writeObject(oldObj);
    oos.flush();
    final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
...
ListdeepCopy(List src)
deep Copy
ByteArrayOutputStream byteOut = null;
ObjectOutputStream out = null;
try {
    byteOut = new ByteArrayOutputStream();
    out = new ObjectOutputStream(byteOut);
    out.writeObject(src);
    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    ObjectInputStream in = new ObjectInputStream(byteIn);
...
ListdeepCopy(List src)
deep Copy
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) in.readObject();
return dest;
...
ObjectdeepCopy(Object o)
deep Copy
byte[] bytes = serializeToByteArray(o);
return deserializeFromByteArray(bytes);
Object[]deepCopy(Object obj1, Object obj2)
Method obtains deep copies for provided instances.
Object obj[] = { null, null };
try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bos);
    out.writeObject(obj2);
    out.writeObject(obj1);
    out.flush();
    out.close();
...
ObjectdeepCopy(Object oldObj)
deep Copy
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    oos = new ObjectOutputStream(bos);
    oos.writeObject(oldObj);
    oos.flush();
    ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
...
ObjectdeepCopy(Object orig)
NOTE: when updating the cloned object, the cloned associated collection in particular, Before persist using Hibernate, need to create a new collection using the cloned collection otherwise Hibernate complains.
Object obj = null;
try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bos);
    out.writeObject(orig);
    out.flush();
    out.close();
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
...
ObjectdeepCopy(Object original)
deep Copy
ObjectInputStream ois;
ObjectOutputStream oos;
ByteArrayInputStream bais;
ByteArrayOutputStream baos;
byte[] data;
Object copy;
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
...