Java Object Deep Clone deepClone(Serializable o)

Here you can find the source of deepClone(Serializable o)

Description

deep Clone

License

Open Source License

Declaration

public static Object deepClone(Serializable o) 

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 {
    public static Object deepClone(Serializable o) {
        return deserialize(serialize(o));
    }//from w w w. j av a 2 s.  c om

    /** @author Found this one on StackOverflow */
    public static Object deserialize(byte[] bytes) {
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /** @author Found this one on StackOverflow */
    public static byte[] serialize(Serializable o) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(o);
            return baos.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. deepClone(final T objectToBeClonned)
  2. deepClone(Object obj)
  3. deepClone(Object objToClone)
  4. deepClone(Object src)
  5. deepClone(Object src)
  6. deepClone(Serializable serializable)
  7. deepClone(T toClone, final ClassLoader classLoader)
  8. deepCloneObj(Object src)
  9. deepCloneOnlyIfContains(final T objectToBeClonned, final T[] objects)