Java Object Deep Copy deepCopy(T src)

Here you can find the source of deepCopy(T src)

Description

deep Copy

License

Open Source License

Declaration

public static <T> T deepCopy(T src) throws IOException,
            CloneNotSupportedException, ClassNotFoundException 

Method Source Code

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

import java.io.*;

public class Main {
    public static <T> T deepCopy(T src) throws IOException,
            CloneNotSupportedException, ClassNotFoundException {
        if (!(src instanceof Serializable)) {
            throw new CloneNotSupportedException(
                    "src is not Serializable class");
        }/*  w w  w.ja v a 2 s  .  c  o m*/

        ByteArrayOutputStream bout = null;
        ObjectOutputStream oout = null;
        ByteArrayInputStream bin = null;
        ObjectInputStream oin = null;

        try {
            bout = new ByteArrayOutputStream();
            oout = new ObjectOutputStream(bout);
            oout.writeObject(src);
            oout.flush();

            byte[] serialized = bout.toByteArray();

            bin = new ByteArrayInputStream(serialized);
            oin = new ObjectInputStream(bin);
            T copyedObject = (T) oin.readObject();

            return copyedObject;
        } finally {
            bout.close();
            oout.close();
            bin.close();
            oin.close();
        }
    }
}

Related

  1. deepCopy(Object original)
  2. deepCopy(Object toCopy)
  3. deepCopy(Serializable source)
  4. deepCopy(T item)
  5. deepCopy(T originalObject)
  6. deepCopy(T t)
  7. deeplyCopy(Serializable serializable)