Reading _multiple_ Externalizable objects from ObjectInputStream

O

OtisUsenet

Hi,

I'm passing a list of Externalized objects over HTTP. On the other end
of that HTTP connection I get the ObjectInputStream, but I am having
trouble reading objects from it.

That is, on the sending side I send N objects, but on the receiving end
I can read only 1.

On the sending side, I do this:

HttpURLConnection conn = null;
ObjectOutputStream oos = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setAllowUserInteraction(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-type", "....my stuff here
....");
conn.setRequestMethod("POST");
// send POST output
oos = new ObjectOutputStream(conn.getOutputStream());
for (Iterator it = goodies.iterator(); it.hasNext(); ) {
Goodies g = (Goodie) it.next();
g.writeExternal(oos);
}
}
... catch block + finally block that calls oos.close()


On the receiving end I have:

InputStream is = null;
ObjectInputStream ois = null;
try {
is = request.getInputStream();
ois = new ObjectInputStream(is);
Goodie g = null;
do {
g = (Goodie) ois.readObject();
} while (g != null);
... catch + finally block

The problem is, this do-while loop reads the first Goodie off of OIS,
but the second loop iteration causes this:

java.io_OptionalDataException
at
java.io_ObjectInputStream.readObject0(ObjectInputStream.java:1285)
at
java.io_ObjectInputStream.readObject(ObjectInputStream.java:324)

It looks as if there is only 1 Goodie in the stream!

I have been searching and searching for some examples that show how to
read MULTIPLE objects from ObjectInputStream, but all examples I was
able to find show cases with a single Foo foo = (Foo) ois.readObject();
call.
That's the easy case!
How does one read _multiple_ objects?

Any help would be very appreciated.

Thanks!
 
M

Maru

Any reason why ur object needs to be Externalizable? Cant it be just
serializable instead? I have tried passing multiple serializable
objects over a socket connection (Not http) and it works well. Should
work over http aswell.
-Maru
 
O

OtisUsenet

Hi Maru,
Any reason why ur object needs to be Externalizable? Cant it be just
serializable instead? I have tried passing multiple serializable
objects over a socket connection (Not http) and it works well. Should
work over http aswell.

So what happened in your case? Did you, on the receiving end of the
connection, simpy have a loop like in my example? Or, put more simply,
did you just call readObject() on an instance of ObjectInputStream
multiple times?

As in:

Foo foo1 = (Foo) ois.readObject();
foo foo2 = (Foo) ois.readObject();
....
?

I could try going with just Serializable instead of Externalizable, but
the object that I'm trying to send over the wire has some transient
List objects. If I understand Serializable/Externalizable correctly,
if I don't implement Externalizable and _explicitly_ write my transient
instances on the stream, they will not be written. Thus, I think I
need to implement Externalizable, not Serializable, I believe.

For now, I think, my main problem is simply reading multiple objects
from OIS - I can read only one, so I'm wondering what your code for
reading multiple things from OIS looked like. I don't have to do any
read(), or reset() or mark(int) calls,right?

Thanks.
 
N

Nigel Wade

OtisUsenet said:
Hi,

I'm passing a list of Externalized objects over HTTP. On the other end
of that HTTP connection I get the ObjectInputStream, but I am having
trouble reading objects from it.

That is, on the sending side I send N objects, but on the receiving end
I can read only 1.

On the sending side, I do this:
oos = new ObjectOutputStream(conn.getOutputStream());
for (Iterator it = goodies.iterator(); it.hasNext(); ) {
Goodies g = (Goodie) it.next();
g.writeExternal(oos);

You write the object with writeExternal()...
}
}
... catch block + finally block that calls oos.close()


On the receiving end I have:

InputStream is = null;
ObjectInputStream ois = null;
try {
is = request.getInputStream();
ois = new ObjectInputStream(is);
Goodie g = null;
do {
g = (Goodie) ois.readObject();


.... and read it back with readObject().

I've never used Externalizable, but don't you need to use
Externalizable.readExternal() to read it back?
 
E

EJP

Nigel said:
You write the object with writeExternal()...

... and read it back with readObject().

I've never used Externalizable, but don't you need to use
Externalizable.readExternal() to read it back?

This will work but what the OP should really be doing is
oos.writeObject(g) and g = (Goodie)ois.readObject().
 
O

OtisUsenet

Hi,
This will work but what the OP should really be doing is
oos.writeObject(g) and g = (Goodie)ois.readObject().

I'm currently doing this to write the object:
g.writeExternal(oos);

Are you saying I should swap 'g' and 'oos', and call writeExternal on
oos, as in oos.writeExternal(g)? I'm not sure that's true, but I will
try it anyway and report back.
 
O

OtisUsenet

Wow, thanks, I think this was actually the solution! Calling
writeObject instead of writeExternal solved it. Thanks for pointing
this out. I suppose OOS figures out if my Goodie object implements
Externalizable, and if so, it calls writeExternal. If not, then it
calls writeObject() from Serializable interface.

Is this correct?

Thanks again!
 
E

EJP

OtisUsenet said:
Wow, thanks, I think this was actually the solution! Calling
writeObject instead of writeExternal solved it. Thanks for pointing
this out. I suppose OOS figures out if my Goodie object implements
Externalizable, and if so, it calls writeExternal. If not, then it
calls writeObject() from Serializable interface.

Is this correct?

This is correct, and that way your writing code is also insulated from
whether Goodie is Serializable or Externalizable.

In your previous post you got my recommendation back to front but the
above is correct.
 

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

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top