Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Generic clone for deep copying - is this any good?

Thread view: 
castillo.bryan@gmail.com - 24 Dec 2005 04:44 GMT
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) {}
        }
    }
Roedy Green - 24 Dec 2005 08:43 GMT
> 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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 24 Dec 2005 09:24 GMT
>>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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Thomas Hawtin - 24 Dec 2005 09:30 GMT
>     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);
[quoted text clipped - 10 lines]
>             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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 24 Dec 2005 11:47 GMT
On Sat, 24 Dec 2005 09:38:37 +0000, Thomas Hawtin
<usenet@tackline.plus.com> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.