28 September 2005

Deep cloning.

Wish all good things in life were so easily cloneable...

public Object clone() throws CloneNotSupportedException {
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
Object deepCopy = ois.readObject();
}
catch(Exception e){
throw CloneNotSupportedException(e);
}
return deepCopy;
}

No comments: