Java Object Deep Copy deepCopy(Object obj1, Object obj2)

Here you can find the source of deepCopy(Object obj1, Object obj2)

Description

Method obtains deep copies for provided instances.

License

Open Source License

Parameter

Parameter Description
obj1 represents the first object which is to be streamed
obj2 represents the second object which is to be streamed

Return

an array of deserialized, objects representing deep copies of provided objects obj1 and obj2

Declaration

public static Object[] deepCopy(Object obj1, Object obj2) 

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 {
    /**//from   w ww  . j a  v a2  s  . co  m
     * Method obtains deep copies for provided instances.
     * @param obj1 represents the first object which is to be streamed
     * @param obj2 represents the second object which is to be streamed
     * @return an array of deserialized, objects representing deep copies
     * of provided objects <code>obj1</code> and </code>obj2</code>
     * @author specijalac
     */
    public static Object[] deepCopy(Object obj1, Object obj2) {
        Object obj[] = { null, null };
        try {
            // must be the same byte stream (link and element painter)
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bos);
            out.writeObject(obj2);
            out.writeObject(obj1);
            out.flush();
            out.close();

            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
            obj[0] = in.readObject();
            obj[1] = in.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
        return obj;
    }
}

Related

  1. deepCopy(final Class c, final Serializable s)
  2. deepCopy(final Object oldObj)
  3. deepCopy(List src)
  4. deepCopy(List src)
  5. deepCopy(Object o)
  6. deepCopy(Object oldObj)
  7. deepCopy(Object orig)
  8. deepCopy(Object original)
  9. deepCopy(Object toCopy)