Flushing a GZIPOutputStream

Discussion in 'Java' started by Dave Rudolf, Jan 8, 2004.

  1. Dave Rudolf

    Dave Rudolf Guest

    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.
     
    Dave Rudolf, Jan 8, 2004
    #1
    1. Advertising

  2. Dave Rudolf

    Chris Gokey Guest

    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 Rudolf wrote:

    > 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.


    --
     
    Chris Gokey, Jan 8, 2004
    #2
    1. Advertising

  3. Dave Rudolf

    Dave Rudolf Guest

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


    "Chris Gokey" <> wrote in message
    news:8t5Lb.310901$_M.1808859@attbi_s54...
    > 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 Rudolf wrote:
    >
    > > 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.

    >
    > --
    >
     
    Dave Rudolf, Jan 8, 2004
    #3
  4. Dave Rudolf

    Anton Spaans Guest

    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.

    "Dave Rudolf" <> wrote in message
    news:...
    > Hmm... interesting, but because I am streaming serialized objects, I

    really
    > have no control over the block size.
    >
    >
    > "Chris Gokey" <> wrote in message
    > news:8t5Lb.310901$_M.1808859@attbi_s54...
    > > 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 Rudolf wrote:
    > >
    > > > 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.

    > >
    > > --
    > >

    >
    >
     
    Anton Spaans, Jan 8, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric

    Flushing the Buffer in .Net

    Eric, Apr 25, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    667
  2. z. f.

    flushing inside a repeaterItem

    z. f., Jan 30, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    333
    z. f.
    Jan 30, 2005
  3. Steffen Heinzl

    GZIPOutputStream GZIPInputStream

    Steffen Heinzl, Nov 18, 2003, in forum: Java
    Replies:
    4
    Views:
    5,356
    Steffen Heinzl
    Nov 19, 2003
  4. Ken DeLong
    Replies:
    2
    Views:
    655
    Roedy Green
    Jul 24, 2004
  5. Replies:
    4
    Views:
    637
    Roedy Green
    Sep 28, 2005
Loading...

Share This Page