Here you can find the source of deepCopy(Object toCopy)
public static Object deepCopy(Object toCopy)
//package com.java2s; //License from project: Apache License import java.io.BufferedInputStream; import java.io.BufferedOutputStream; 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.jav a 2 s .c o m * Creates a deep copy of an object using serialization. */ public static Object deepCopy(Object toCopy) { 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); } storedObjectArray = ostream.toByteArray(); } Object toReturn = null; try (ByteArrayInputStream istream = new ByteArrayInputStream( storedObjectArray)) { ObjectInputStream p; p = new ObjectInputStream(new BufferedInputStream(istream)); toReturn = p.readObject(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } return toReturn; } }