GZIPOutputStream GZIPInputStream

From: Steffen Heinzl (steffen.heinzl_at_informatik.fh-fulda.de)
Date: 11/18/03


Date: Tue, 18 Nov 2003 18:04:21 +0100

Hi!
I got a a problem sending compressed objects via sockets.

At the client site a Message object is given to the static method
sendMessage()
There are a few other MessageTypes which extend Message, so I can send
different types of messages over the socket.
My OutputStream os was obtained by socket.getOutputStream(), that means the
message objects are given to "os" and thus sent over the socket.
The variable compressed indicates whether GZIP is to be used or not.

If compressed is always set to false, there are no problems.
If compressed is set to true, I get an IOException (at server side) after an
undefined number of sent messages. Sometimes only 1 or 2 messages are
received correctly, sometimes 20.
The IOException looks like this:
java.io.IOException: Not in GZIP format
        at
java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:131)
        at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:58)
        at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:68)
        at
ScreenGrabber.messaging.MessageLogic.readMessage(MessageLogic.java:68)
        at
ScreenGrabber.communication.ConnectionThread.run(ConnectionThread.java:80)

At the client site a thread is responsible for sending messages; at the
server site a thread is used to receive messages.

The code for sending and receiving messages:

client site:
public static synchronized void sendMessage(Message msg, OutputStream os,
boolean compressed) throws IOException
    {
        if(compressed)
        {
            GZIPOutputStream gout = new GZIPOutputStream(os);
            ObjectOutputStream oos = new ObjectOutputStream(gout);
            oos.writeObject(msg);
            gout.finish();
        }
        else
        {
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(msg);
        }
    }

server site (InputStream is socket.getInputStream()):
 public static synchronized Message readMessage(InputStream in, boolean
compressed) throws ClassNotFoundException, IOException
    {
        if(compressed)
        {
            GZIPInputStream gin = new GZIPInputStream(in);
            ObjectInputStream ois = new ObjectInputStream(gin);
             return ((Message) ois.readObject());
        }
        else
        {
            ObjectInputStream ois = new ObjectInputStream(in);
             return ((Message) ois.readObject());
        }
    }

Can anybody tell me why the compression doesn't work properly?
(I'm using jdk 1.4.2)

Thanks in advance,
Steffen



Relevant Pages

  • Re: GZIPOutputStream GZIPInputStream
    ... > I got a a problem sending compressed objects via sockets. ... > The IOException looks like this: ... > At the client site a thread is responsible for sending messages; ... DeflaterOutputStream where you can set the compression level. ...
    (comp.lang.java.programmer)
  • Re: Compress::Zlib inflateInit help
    ... from the socket? ... including java sample code ... compression library to do this. ... RFC 1951 data streams that are each prefixed with a bespoke header. ...
    (comp.lang.perl.misc)
  • gzip chunk by chunk from a buffer using zlib library- only part of file is retrieved
    ... I am using zlib functions to implement http compression. ... successfully perform compression of a whole buffer and I could ... header to socket ...
    (comp.compression)
  • Re: estimate zlib compression ratio?
    ... For example, I need to send files over a socket, and some files are ... documents with good compression ratios. ... compress the files based on an estimated compression ratio. ... compression ratio by some constant to estimate that of zlib. ...
    (comp.compression)
  • Compressed Data transfer over Socket (Network Stream)
    ... provide any out of the box solution for compression of the data over socket ... or a network stream. ... what are the best alternatives for a high speed text compression ...
    (microsoft.public.dotnet.languages.csharp)