Java Object Deep Copy deepCopy(Object original)

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

Description

deep Copy

License

Open Source License

Declaration

public static Object deepCopy(Object original) throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {
    public static Object deepCopy(Object original) throws IOException, ClassNotFoundException {
        ObjectInputStream ois;//w  w  w . j a v  a 2 s .c om
        ObjectOutputStream oos;
        ByteArrayInputStream bais;
        ByteArrayOutputStream baos;
        byte[] data;
        Object copy;

        // write object to bytes
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(original);
        oos.close();

        // get the bytes
        data = baos.toByteArray();

        // construct an object from the bytes
        bais = new ByteArrayInputStream(data);
        ois = new ObjectInputStream(bais);
        copy = ois.readObject();
        ois.close();
        return copy;
    }
}

Related

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