Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Cloning complex objects.

Reply
Thread Tools

Cloning complex objects.

 
 
hitesh
Guest
Posts: n/a
 
      04-11-2004
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());
}
}
 
Reply With Quote
 
 
 
 
Tim Tyler
Guest
Posts: n/a
 
      04-12-2004
hitesh <(E-Mail Removed)> wrote or quoted:

> 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/groups?selm...&output=gplain
--
__________
|im |yler http://timtyler.org/ http://www.velocityreviews.com/forums/(E-Mail Removed) Remove lock to reply.
 
Reply With Quote
 
 
 
 
Dmytro Sheyko
Guest
Posts: n/a
 
      04-15-2004
I use serialization in order to make deep copy and still have no problem.

"hitesh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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());
> }
> }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How complex is complex? Kottiyath Python 22 03-28-2009 10:11 PM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM
hard drive cloning tami Microsoft Certification 1 12-15-2003 06:12 PM



Advertisments