20131022
간단한 메소드를 만들어 보았습니다.
메모리상에서 객체 상태를 복사하는 기능을 수행합니다.
단, 직렬화를 이용하는 것이기 때문에 복사하려는 Object는 Serializable 인터페이스를 구현한 클래스 객체여야 합니다.
public Object deepCopy( Object item ) throws IOException, ClassNotFoundException{
Object copied = null;
ByteArrayOutputStream baos = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try{
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream( baos );
oos.writeObject( item );
oos.flush();
ois = new ObjectInputStream( new ByteArrayInputStream( baos.toByteArray() ) );
copied = ( Object ) ois.readObject();
}finally{
try{
if( oos != null ){ oos.close(); }
if( ois != null ){ ois.close(); }
}catch( Exception ex ){
ex.printStackTrace();
}
}
return copied;
}