Skip to content

Instantly share code, notes, and snippets.

@ddmills
Last active December 8, 2015 18:54
Show Gist options
  • Save ddmills/5a892c358e511b924c63 to your computer and use it in GitHub Desktop.
Save ddmills/5a892c358e511b924c63 to your computer and use it in GitHub Desktop.
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class DeepCopy {
private DeepCopy() { }
public static Object copy(Object orig) {
Object obj = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(obj);
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(bos.toByteArray()));
obj = in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment