Java Object Deep Copy deepCopy(Object oldObj)

Here you can find the source of deepCopy(Object oldObj)

Description

deep Copy

License

Open Source License

Declaration

static public Object deepCopy(Object oldObj) throws Exception 

Method Source Code


//package com.java2s;

import java.io.*;

public class Main {
    static public Object deepCopy(Object oldObj) throws Exception {
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {/*from   w  ww  .  j  av  a2s  . c om*/
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);

            // serialize and pass the object
            oos.writeObject(oldObj);
            oos.flush();
            ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bin);

            // return the new object
            return ois.readObject();
        } catch (Exception e) {
            System.out.println("Exception in ObjectCloner = " + e);
            throw (e);
        } finally {
            oos.close();
            ois.close();
        }
    }
}

Related

  1. deepCopy(final Object oldObj)
  2. deepCopy(List src)
  3. deepCopy(List src)
  4. deepCopy(Object o)
  5. deepCopy(Object obj1, Object obj2)
  6. deepCopy(Object orig)
  7. deepCopy(Object original)
  8. deepCopy(Object toCopy)
  9. deepCopy(Serializable source)