java - Sending the same but modifed object over ObjectOutputStream -
i have following code shows either bug or misunderstanding on part.
i sent same list, modified on objectoutputstream. once [0] , other [1]. when read it, [0] twice. think caused fact sending on same object , objectoutputstream must caching them somehow.
is work should, or should file 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] expected system.out.println(ois.readobject()); // outputs [0], expected [1] system.out.println(ois.readobject()); system.exit(0); } }
the stream has reference graph, object sent twice not give 2 objects on other end, one. , sending same object twice separately give same instance twice (each same data - you're seeing).
see reset() method if want reset graph.
Comments
Post a Comment