Generic clone for deep copying - is this any good?

Discussion in 'Java' started by castillo.bryan@gmail.com, Dec 24, 2005.

  1. Guest

    I just came across a class that uses serialization to clone itself.
    Its a class that is a base class for about 30 different classes that
    represent items stored in a relational database. I understand that the
    programmer did this so they could clone any object inherting from the
    base class, without writing anything else. I'm not sure if this is
    really a good idea or not though. Any opinions?


    public Object clone() {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {
    baos = new ByteArrayOutputStream();
    oos = new ObjectOutputStream(baos);
    oos.writeObject(this);
    bais = new ByteArrayInputStream(baos.toByteArray());
    ois = new ObjectInputStream(bais);
    return ois.readObject();
    }
    catch (ClassNotFoundException cnfe) {
    throw new RuntimeException(cnfe);
    }
    catch (IOException ioe) {
    throw new RuntimeException(ioe);
    }
    finally {
    if (oos != null) try { oos.close(); } catch (IOException ioe) {}
    if (ois != null) try { ois.close(); } catch (IOException ioe) {}
    }
    }
     
    , Dec 24, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 23 Dec 2005 20:44:03 -0800, wrote, quoted
    or indirectly quoted someone who said :

    > I'm not sure if this is
    >really a good idea or not though. Any opinions?


    It is a very roundabout way to clone. The ordinary clone implemented
    in a base method will do the same job.

    see http://mindprod.com/jgloss/clone.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 24, 2005
    #2
    1. Advertising

  3. Roedy Green wrote:
    > On 23 Dec 2005 20:44:03 -0800, wrote, quoted
    > or indirectly quoted someone who said :
    >
    >
    >>I'm not sure if this is
    >>really a good idea or not though. Any opinions?

    >
    >
    > It is a very roundabout way to clone. The ordinary clone implemented
    > in a base method will do the same job.


    Object.clone does a very shallow copy. Serialisation does a very deep
    clone. Different things.

    Usually what you want is some kind of intermediate clone. For instance,
    ArrayList.clone doesn't clone its stored elements, but more than a
    bitwise clone as it must copy the array.

    BTW: Serialisation will have poor performance (particularly with Sun's
    implementation, allegedly). That may or may not be a problem.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Dec 24, 2005
    #3
  4. wrote:
    > public Object clone() {


    Using a different method name (or 1.5), you could return a more specific
    type.

    > ByteArrayOutputStream baos = null;
    > ObjectOutputStream oos = null;
    > ByteArrayInputStream bais = null;
    > ObjectInputStream ois = null;


    What's the point in all of that? And I hate those initials.

    > try {
    > baos = new ByteArrayOutputStream();
    > oos = new ObjectOutputStream(baos);
    > oos.writeObject(this);


    I don't know if it actually makes any difference, but you should flush here.

    > bais = new ByteArrayInputStream(baos.toByteArray());
    > ois = new ObjectInputStream(bais);
    > return ois.readObject();
    > }
    > catch (ClassNotFoundException cnfe) {
    > throw new RuntimeException(cnfe);
    > }
    > catch (IOException ioe) {
    > throw new RuntimeException(ioe);
    > }
    > finally {
    > if (oos != null) try { oos.close(); } catch (IOException ioe) {}
    > if (ois != null) try { ois.close(); } catch (IOException ioe) {}
    > }


    As you aren't dealing with any actual resources, I don't think there's
    any need for the finally. Unless anyone know different?

    > }


    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Dec 24, 2005
    #4
  5. Roedy Green Guest

    On Sat, 24 Dec 2005 09:38:37 +0000, Thomas Hawtin
    <> wrote, quoted or indirectly quoted someone
    who said :

    >> ObjectOutputStream oos = null;
    >> ByteArrayInputStream bais = null;
    >> ObjectInputStream ois = null;

    >
    >What's the point in all of that? And I hate those initials.


    that's my fault . I wanted short names for the file i/o amanuensis
    that I could use to tie together disparate code generated in many
    different places.

    bais = Byte Array Input Stream.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 24, 2005
    #5
    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. Kohn J. Wrahn
    Replies:
    0
    Views:
    2,892
    Kohn J. Wrahn
    May 25, 2005
  2. Tim Tyler
    Replies:
    28
    Views:
    6,237
    Words13100
    Aug 8, 2010
  3. lordy

    To clone or not to clone..

    lordy, Jul 7, 2006, in forum: Java
    Replies:
    3
    Views:
    725
    lordy
    Jul 7, 2006
  4. David Garamond

    deep clone

    David Garamond, Jan 20, 2004, in forum: Ruby
    Replies:
    1
    Views:
    109
    Joel VanderWerf
    Jan 20, 2004
  5. Ronald Fischer

    Deep Clone of array?

    Ronald Fischer, Aug 9, 2007, in forum: Ruby
    Replies:
    4
    Views:
    255
    Chris Hayes
    Apr 15, 2010
Loading...

Share This Page