GZIPOutputStream GZIPInputStream

S

Steffen Heinzl

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
 
C

Christopher Dean

Steffen said:
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
Hmmm...sure sounds like you are sending something uncompressed...I don't
use the GZIP streams has they just add additional information that you
don't need (not reading/writing zip files after all) and there may even
be some weirdness because of that. I use the InflaterInputStream and
DeflaterOutputStream where you can set the compression level. If you
don't want to send compressed set the level to NO_COMPRESSION. This way
you code works not matter what the client is sending (as long as they
are using your send routine) and clients can change the compression
level as needed (in fact, I look at what I am sending and if it is less
than 8K I don't compress at all). The only other time I have seen this
happen is when I was reading header information for HTTP (I compress
HTTP messages) and since I was using a buffered reader it read past the
HTTP header and into the compression and screwed everything up (which I
don't see you doing).
 
S

Steffen Heinzl

Hmmm...sure sounds like you are sending something uncompressed...I don't
use the GZIP streams has they just add additional information that you
don't need (not reading/writing zip files after all) and there may even
be some weirdness because of that. I use the InflaterInputStream and
DeflaterOutputStream where you can set the compression level. If you
don't want to send compressed set the level to NO_COMPRESSION. This way
you code works not matter what the client is sending (as long as they
are using your send routine) and clients can change the compression
level as needed (in fact, I look at what I am sending and if it is less
than 8K I don't compress at all). The only other time I have seen this
happen is when I was reading header information for HTTP (I compress
HTTP messages) and since I was using a buffered reader it read past the
HTTP header and into the compression and screwed everything up (which I
don't see you doing).

How do I choose an Inflater and Deflater? Could you perhaps give me an
example?

Thanks,
Steffen
 
C

Christopher Dean

Steffen said:
How do I choose an Inflater and Deflater? Could you perhaps give me an
example?

Thanks,
Steffen

new OutputStreamWriter(
new DeflaterOutputStream(_socket.getOutputStream(),
new Deflater(Deflater.BEST_COMPRESSION))); // change to
NO_COMPRESSION if you want

new InputStreamReader (
new InflaterInputStream(_socket.getInputStream());

After looking at my code the only other thing I did was I made my own
reader and writer classes extended from BufferedReader and
BufferedWriter respectively where I use a special character '/f' as a
sentinel to signify an end of input. If I remember correctly the
deflater, inflaters had problems knowing when they were done but it has
been a while. Basically, the reader would read until the sentinel was
reached and the writer would write the sentinel on a close. You may not
need to do this for your application I don't know.
 
S

Steffen Heinzl

Christopher Dean said:
new OutputStreamWriter(
new DeflaterOutputStream(_socket.getOutputStream(),
new Deflater(Deflater.BEST_COMPRESSION))); // change to
NO_COMPRESSION if you want

new InputStreamReader (
new InflaterInputStream(_socket.getInputStream());

That doesn't work either. If I use Deflater.NO_COMPRESSION everything works
fine, but if I use another compression type I have the same problem again.
Which JDK did you use for your program?

Regards,
Steffen
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top