Results 1 to 4 of 4

Thread: Deep copy?!

  1. #1
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default Deep copy?!

    Hello,
    I need to store objects that i read from an ObjectInputStream into an array, so i can use the same array later on to further send the objects to the next server.

    This code, however does not do it. Please help.

    [I]InputStream is = socket.getInputStream();
    ObjectInputStream ois = new ObjectInputStream(is);




    Packets a= new Packets();
    a = (Packets) ois.readObject();
    packet[0]=(Packets)a.clone();
    String disp="Encoded:"+a.en+"\nPacketNo:"+a.pktno+"\nSour ce:"+a.src+"\nDestination:"+a.dest+"\nData:"+a.dat a+"\nDistance:"+a.dist+"\n";

    jTextArea1.append(disp);





    count++;

    for(int tot=0;tot<a.tot_pkt-1;tot++)
    {

    int i=1;
    Packets pktrecvd= new Packets();
    pktrecvd = (Packets) ois.readObject();
    packet[i]=(Packets)pktrecvd.clone();
    System.out.println(packet.en);
    i++;
    String dispp="Encoded:"+pktrecvd.en+"\nPacketNo:"+pktrecv d.pktno+"\nSource:"+pktrecvd.src+"\nDestination:"+ pktrecvd.dest+"\nData:"+pktrecvd.data+"\nDistance: "+pktrecvd.dist+"\n";
    jTextArea1.append(dispp);





    count++;


    }
    ois.close();
    is.close();


    At the receiver end, I am able to see on the first and last packet.

    This is my Packets class which implements Cloneable.

    public class Packets extends UnicastRemoteObject implements Serializable,DLL,Cloneable {
    public String en;
    public int pktno;
    public String src;
    public String dest;
    public String data;
    public int dist;

    public int tot_pkt;

    public Packets() throws RemoteException
    {

    }
    /* public Object clone() {
    try {
    return super.clone();
    }
    catch(CloneNotSupportedException e) {
    // we implement Cloneable, this exception will never be thrown
    return null;
    }
    } */
    Packets copy = new Packets();

    public Object clone() {
    // Packets copy = null;
    try {
    copy = (Packets)super.clone();
    // make the copy a little deeper
    copy.en = (String)this.en.clone(); //Error!
    copy.pktno=(int)this.pktno;
    copy.src=(String)this.src.clone();
    copy.dest=(String)this.dest.clone();
    copy.data=(String)this.data.clone();
    copy.dist=(int)this.dist;
    }
    catch(CloneNotSupportedException e) {
    e.printStackTrace();
    }
    return copy;
    }


    The error is "The method clone() from the type Object is not visible"

    Please Please help me here.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Please use code tags when posting the whole code/snippets

    I couldn't broadly understand why you implement it that way, (i'm not an expert though) if you have time to explain further about the goal of your code...

    Why cloning if you would just add them in an array?

    after adding them in that array, would you still modify the captured object? before receiving another object from the ObjectInputStream?

    Please correct me if im wrong,
    freedom exists in the world of ideas

  3. #3
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    Oh man, i cannot get any stupider!
    It works!

    Waste of bandwidth and server space,however small it maybe. My apologies.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Strings can't be cloned. They are immutable so this doesn't matter.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Help with Deep Cloning
    By jralexander137 in forum New To Java
    Replies: 6
    Last Post: 10-30-2008, 05:34 PM
  2. deep copying arraylist to add to a list
    By alvations in forum New To Java
    Replies: 13
    Last Post: 10-08-2008, 03:13 PM
  3. USB Device Copy
    By Mir in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 11:44 AM
  4. Deep Copy Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:05 PM
  5. Replies: 0
    Last Post: 12-28-2007, 12:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •