Java Utililty Methods Object Deep Copy

List of utility methods to do Object Deep Copy

Description

The list of methods to do Object Deep Copy are organized into topic(s).

Method

ObjectdeepCopy(Object toCopy)
Creates a deep copy of an object using serialization.
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
byte[] storedObjectArray;
    try (ObjectOutputStream p = new ObjectOutputStream(new BufferedOutputStream(ostream))) {
        p.writeObject(toCopy);
        p.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
...
SerializabledeepCopy(Serializable source)
Utility for making deep copies (vs.
Object copy = null;
try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bos);
    out.writeObject(source);
    out.flush();
    out.close();
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
...
TdeepCopy(T item)
deep Copy
if (item == null)
    return null;
try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(item);
    oos.flush();
    byte[] rawData = baos.toByteArray();
...
TdeepCopy(T originalObject)
Copies a file into another one
Object clone = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    oos = new ObjectOutputStream(baos);
    oos.writeObject(originalObject); 
    oos.flush();
...
TdeepCopy(T src)
deep Copy
if (!(src instanceof Serializable)) {
    throw new CloneNotSupportedException("src is not Serializable class");
ByteArrayOutputStream bout = null;
ObjectOutputStream oout = null;
ByteArrayInputStream bin = null;
ObjectInputStream oin = null;
try {
...
TdeepCopy(T t)
deep Copy
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(t);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return (T) ois.readObject();
SerializabledeeplyCopy(Serializable serializable)
deeply Copy
try {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(serializable);
    objectOutputStream.close();
    byteArrayOutputStream.close();
    byte[] bytes = byteArrayOutputStream.toByteArray();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
...