Cloning complex objects.

Discussion in 'Java' started by hitesh, Apr 11, 2004.

  1. hitesh

    hitesh Guest

    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());
    }
    }
    hitesh, Apr 11, 2004
    #1
    1. Advertising

  2. hitesh

    Tim Tyler Guest

    hitesh <> 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/ Remove lock to reply.
    Tim Tyler, Apr 12, 2004
    #2
    1. Advertising

  3. I use serialization in order to make deep copy and still have no problem.

    "hitesh" <> wrote in message
    news:...
    > 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());
    > }
    > }
    Dmytro Sheyko, Apr 15, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    560
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    734
    Stanimir Stamenkov
    Oct 25, 2005
  3. Robert Mark Bram
    Replies:
    0
    Views:
    673
    Robert Mark Bram
    Feb 4, 2007
  4. Cloning C++ objects

    , Dec 4, 2006, in forum: C++
    Replies:
    10
    Views:
    754
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Dec 5, 2006
  5. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    742
Loading...

Share This Page