Solved

Problem with ObjectOutputStream

Posted on 1998-02-12
2
364 Views
Last Modified: 2012-06-22
Hi,

  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");
  Object thisTestObject=oin.readObject();
  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.

Sun Jian

0
Comment
Question by:sunjian061197
2 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1233033
I haven't used GZIP streams so I'll post as a comment since it falls under educated guesswork.

First thing you might want to check is that an ObjectInputStream is derrived directly from InputStream while GZIPInputStream is derived from InflaterInputStream which is derrived from FilterInputStream and finaly from InputStream.

In the line where you call:

ObjectInputStream oin=new ObjectInputStream(gzis);

You are asking an ObjectInputStream to extend a class for which it has no interface.

Try creating your own stream class, derrived from ObjectInputStream and extending FilterInputStream.

Using such a class might solve your problem.
0
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 1233034
Use the GZIPOutputStream finish() method instead close.  This will complete the output of the object but will leave the stream open for further communication.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question