public Object copy ( final Object obj , PersistenceBroker broker ) throws ObjectCopyException { ObjectOutputStream oos = null ; ObjectInputStream ois = null ; try { final ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ; oos = new ObjectOutputStream ( bos ) ; // serialize and pass the object oos . writeObject ( obj ) ; oos . flush ( ) ; final ByteArrayInputStream bin = new ByteArrayInputStream ( bos . toByteArray ( ) ) ; ois = new ObjectInputStream ( bin ) ; // return the new object return ois . readObject ( ) ; } catch ( Exception e ) { throw new ObjectCopyException ( e ) ; } finally { try { if ( oos != null ) { oos . close ( ) ; } if ( ois != null ) { ois . close ( ) ; } } catch ( IOException ioe ) { // ignore } } }
This implementation will probably be slower than the metadata object copy but this was easier to implement .