Sending the same but modifed object over ObjectOutputStream

家住魔仙堡 提交于 2019-11-26 04:28:38

问题


I have the following code that shows either a bug or a misunderstanding on my part.

I sent the same list, but modified over an ObjectOutputStream. Once as [0] and other as [1]. But when I read it, I get [0] twice. I think this is caused by the fact that I am sending over the same object and ObjectOutputStream must be caching them somehow.

Is this work as it should, or should I file a bug?

import java.io.*;
import java.net.*;
import java.util.*;

public class OOS {

    public static void main(String[] args) throws Exception {
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    ServerSocket ss = new ServerSocket(12344);
                    Socket s= ss.accept();

                    ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                    List same = new ArrayList();
                    same.add(0);
                    oos.writeObject(same);
                    same.clear();
                    same.add(1);
                    oos.writeObject(same);

                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();

        Socket s = new Socket(\"localhost\", 12344);
        ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

        // outputs [0] as expected
        System.out.println(ois.readObject());

        // outputs [0], but expected [1]
        System.out.println(ois.readObject());
        System.exit(0);
    }
}

回答1:


The stream has a reference graph, so an object which is sent twice will not give two objects on the other end, you will only get one. And sending the same object twice separately will give you the same instance twice (each with the same data - which is what you're seeing).

See the reset() method if you want to reset the graph.




回答2:


Max is correct, but you can also use:

public void writeUnshared(Object obj);

See comment below for caveat




回答3:


What you probably want is:

ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
List same = new ArrayList();
same.add(0);
oos.writeObject(same);
oos.flush();  // flush the stream here
same.clear();
same.add(1);
oos.writeObject(same);

Otherwise the same object will be flushed twice when the stream is closed or its buffer runs out.

Just FYI, when you deserialize the objects into, let's say o1 and o2, o1 != o2.



来源:https://stackoverflow.com/questions/142317/sending-the-same-but-modifed-object-over-objectoutputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!