Socket / writeObject / NotSerializableException question

J

JMC

Suppose a stream socket connection exists between P[roducer] and
C[onsumer], where P has an ObjectOutputStream on the socket and C has
an ObjectInputStream.

Then if P makes a sequence of OOS.writeXxxx calls, and these are not
matched exactly in sequence by OIS.readXxxx calls by C, then C will of
course get out of step and throw an exception, for example
OptionalDataException.

However, if P only makes OOS.writeObject calls, and C only makes
OIS.readObject calls, then they will stay in step as long as
OOS.writeObject does not throw a NotSerializableException.

But what if one of P's calls to OOS.writeObject does throw a
NotSerializableException, e.g. because some sub-sub-object deep in the
argument's graph is not serialisable? Then C's corresponding
OIS.readObject will get some sort of "malformed" object (or perhaps
null).

But then say P's next call to OOS.writeObject does not raise the
exception. Is it guaranteed that C will stay in step, and successfully
read the object? Or is it the case that sometimes P and C will need to
reset their connection to get back in step when P gets a
NotSerializableException?
 
R

Roedy Green

But then say P's next call to OOS.writeObject does not raise the
exception. Is it guaranteed that C will stay in step, and successfully
read the object? Or is it the case that sometimes P and C will need to
reset their connection to get back in step when P gets a
NotSerializableException?

When you write an object, it is a blob consisting of the object and
its dependents. If something goes wrong with the read, the stream
will be sitting at the end of the blob ready to try the next object.

I would think that if one of the subobjects was faulty, you would have
a half read tree of objects in RAM, but without a master object to
point to them, they would soon be garbage collected.

It should not be hard to try an experiment to ensure it can stay in
sync. Just leave out one of the classes from the receiver.

http://mindprod.com/jgloss/fileio.html for sample code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top