Problem with ObjectOutputStream
Posted on 1998-02-12
I created an ObjectOutputStream to send serialized object through network. The steps are like this:
1. I create an OutputStream using Socket.getOutputStream()
2. I create an GZIPOutputStream from this OutputStream
3. I create an ObjectOutputStream from this GZIPOutputStream
4. I send the object using ObjectOutputStream.writeObject()
My problem is: after I send the object, it seems that I have to call
ObjectOutputStream.close() in order to let my receiver know that's the end of object.
If I don't close(), my receiver simply blocks. I have tried ObjectOutputStream.flush(), it didn't work either.
The unfortunate thing is that after I close the ObjectOutputStream, my OutputStream is closed
automatically, which is defintely NOT what I want.
I inserted several debug statements to the receiver and find out that the receiver actually blocks when it attemps to create the ObjectInputStream.
The code looks like this:
System.out.println("About to create the input streams.");
GZIPInputStream gzis=new GZIPInputStream(in);
//in is a DataInputStream created from socket.getInputStream()
System.out.println("GZIPInputStream is ready.");
ObjectInputStream oin=new ObjectInputStream(gzis); //blocked here!!!!
System.out.println("ObjectInputStream is ready.");
System.out.println("Reading the object");
System.out.println("Successfully reading the object.");
After the output side sent the object and flush()ed, at the input side, i can only see message like this:
About to create the input streams.
GZIPInputStream is ready.
and nothing appears after that!
It's so strange as I really don't see the essential difference between
a GZIPInputStream and a ObjectInputStream. But why the former doesn't
block when created, and the later does?
Please help if you can... as I am really frustrated on this..
Thanks in advance.