Flushing a GZIPOutputStream

D

Dave Rudolf

Hi all.

I was just playing around with output streams, and I noticed something
interesting. Suppose that I have a socket between two processes. On each end
of the socket, I put an ObjectInputStream and an ObjectOutputStream. I then
serialize objects over the socket via the object streams and everything
works just fine. But, if I put a GZIPOutputStream between the
ObjectOutputStream and the sockets out stream (and similar for the input
streams) then the app hangs on me when I create the ObjectInputStream.
Perhaps some code will illustrate what I'm talking about.

// Client code

Socket sock = new Socket( "www.disco.com", 1234 );
ObjectOutputStream outStream = new ObjectOutputStream(
new GZIPOutputStream( sock.getOutputStream() );
ObjectInputStream inStream = new ObjectInputStream(
new GZIPInputStream( sock.getInputStream() );



// Server code

ServerSocket svrSock = new ServerSocket( 1234 );
Socket sock = svrSock.accept();
ObjectOutputStream outStream = new ObjectOutputStream(
new GZIPOutputStream( sock.getOutputStream() );
ObjectInputStream inStream = new ObjectInputStream(
new GZIPInputStream( sock.getInputStream() );


Both apps hang when they try to create the ObjectInputStream. The reason
seems to be that the ObjevtInputStream waits for the other end to send
certain initialization data along, but the GZIP stream is holding back the
raw bytes -- it wants more data before it can deflate and transmit a block.
Is this correct?

Even if I put an outStream.flush() before creating the input stream, it
still hangs. If I call finish() on the GZIPOutputStream, then I gen an
exception the next time I do a write. Any suggestions on how to force the
data across without finishing the stream?


Dave.
 
C

Chris Gokey

Some information regarding this can be found here:
http://home.comcast.net/~cgokey/java/zlib/index.html

I ran into some similiar problems with compression over sockets, it may
apply to GZIPOutputStream as well. In any case you maybe able to apply
some of this to what you are trying to to.

Chris

Dave said:
Hi all.

I was just playing around with output streams, and I noticed something
interesting. Suppose that I have a socket between two processes. On each
end of the socket, I put an ObjectInputStream and an ObjectOutputStream. I
then serialize objects over the socket via the object streams and
everything works just fine. But, if I put a GZIPOutputStream between the
ObjectOutputStream and the sockets out stream (and similar for the input
streams) then the app hangs on me when I create the ObjectInputStream.
Perhaps some code will illustrate what I'm talking about.

// Client code

Socket sock = new Socket( "www.disco.com", 1234 );
ObjectOutputStream outStream = new ObjectOutputStream(
new GZIPOutputStream( sock.getOutputStream() );
ObjectInputStream inStream = new ObjectInputStream(
new GZIPInputStream( sock.getInputStream() );



// Server code

ServerSocket svrSock = new ServerSocket( 1234 );
Socket sock = svrSock.accept();
ObjectOutputStream outStream = new ObjectOutputStream(
new GZIPOutputStream( sock.getOutputStream() );
ObjectInputStream inStream = new ObjectInputStream(
new GZIPInputStream( sock.getInputStream() );


Both apps hang when they try to create the ObjectInputStream. The reason
seems to be that the ObjevtInputStream waits for the other end to send
certain initialization data along, but the GZIP stream is holding back the
raw bytes -- it wants more data before it can deflate and transmit a
block. Is this correct?

Even if I put an outStream.flush() before creating the input stream, it
still hangs. If I call finish() on the GZIPOutputStream, then I gen an
exception the next time I do a write. Any suggestions on how to force the
data across without finishing the stream?


Dave.

--
 
D

Dave Rudolf

Hmm... interesting, but because I am streaming serialized objects, I really
have no control over the block size.
 
A

Anton Spaans

If a solution to your problem can not be found ( if you do find one, mail me
the solution... thanks! :) ), then see if you could use an intermediate
byte-buffer.

- Stream the contents of the GZIPOutput into a byte-buffer and write that to
the socket.
- If that does not work, do the opposite on the server: Put the contents of
the socket-stream into a byte-buffer and move that into a GZIPInput.
- If that does not work either, do both (use intermediate byte-buffers on
both client and server).

-- Anton Spaans.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,625
Members
45,320
Latest member
icelord

Latest Threads

Top