Java Object Deep Clone deepCloneObj(Object src)

Here you can find the source of deepCloneObj(Object src)

Description

deep Clone Obj

License

Apache License

Declaration

public static Object deepCloneObj(Object src) 

Method Source Code


//package com.java2s;
//License from project: Apache 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 deepCloneObj(Object src) {
        Object o = null;//from  w  ww  .  java2s . c o m
        try {
            if (src != null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(src);
                oos.close();
                ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
                ObjectInputStream ois = new ObjectInputStream(bais);
                o = ois.readObject();
                ois.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return o;
    }
}

Related

  1. deepClone(Object src)
  2. deepClone(Object src)
  3. deepClone(Serializable o)
  4. deepClone(Serializable serializable)
  5. deepClone(T toClone, final ClassLoader classLoader)
  6. deepCloneOnlyIfContains(final T objectToBeClonned, final T[] objects)