Java Object Deep Copy deepCopy(T item)

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

Description

deep Copy

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <T extends Serializable> T deepCopy(T item) 

Method Source Code

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

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

public class Main {
    @SuppressWarnings("unchecked")
    public static <T extends Serializable> T deepCopy(T item) {
        if (item == null)
            return null;

        try {//from   w  ww .ja v  a2  s  .c o m
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(item);
            oos.flush();

            byte[] rawData = baos.toByteArray();

            ByteArrayInputStream bais = new ByteArrayInputStream(rawData);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return (T) ois.readObject();
        } catch (Exception e) {
        }

        return null;
    }
}

Related

  1. deepCopy(Object oldObj)
  2. deepCopy(Object orig)
  3. deepCopy(Object original)
  4. deepCopy(Object toCopy)
  5. deepCopy(Serializable source)
  6. deepCopy(T originalObject)
  7. deepCopy(T src)
  8. deepCopy(T t)
  9. deeplyCopy(Serializable serializable)