Solved

Problem with ObjectOutputStream

Posted on 1998-02-12
2
352 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

912 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now