Cloning complex objects.

H

hitesh

Is this a right way to clone a complex Object

public Object clone(){
try {
//Write the Complex object to the OutputStream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
oos.flush();

//Read the Complex Object from the InputStream
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);

return ois.readObject();
}
catch (Exception e) {
// This should never happen
throw new MyException("Cannot Clone Complex Object. " + e.toString());
}
}
 
T

Tim Tyler

hitesh said:
Is this a right way to clone a complex Object

public Object clone(){
try {
//Write the Complex object to the OutputStream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
oos.flush();

//Read the Complex Object from the InputStream
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);

return ois.readObject();
}
catch (Exception e) {
// This should never happen
throw new MyException("Cannot Clone Complex Object. " + e.toString());
}
}

Consider using the clone() method in the ObjectUtilities class:

http://groups.google.com/[email protected]&output=gplain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top