Solved

Problem with ObjectOutputStream

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

747 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

13 Experts available now in Live!

Get 1:1 Help Now