Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with ObjectOutputStream

Posted on 1998-02-12
2
Medium Priority
?
394 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

618 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