Solved

Problem with ObjectOutputStream

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I remove an object from a 3 40
jsp insert to database example 2 47
java example issue 5 26
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

821 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