public static Object copy ( final Serializable obj ) throws IOException , ClassNotFoundException { ObjectOutputStream out = null ; ObjectInputStream in = null ; Object copy = null ; try { // write the object ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; out = new ObjectOutputStream ( baos ) ; out . writeObject ( obj ) ; out . flush ( ) ; // read in the copy byte data [ ] = baos . toByteArray ( ) ; ByteArrayInputStream bais = new ByteArrayInputStream ( data ) ; in = new ObjectInputStream ( bais ) ; copy = in . readObject ( ) ; } finally { Streams . close ( out ) ; Streams . close ( in ) ; } return copy ; }
Copy an serializable object deeply .