GZipInput/Output stream problems

Hi,

Im trying to compress the data sent over a socket with Gzip.

try

{

// Get the list of processes.

Socket socket =new Socket(_server.getHost(), _server.getPort());

// Need to connect to server and send request type,

// ie what we are requesting to be sent to us

GZIPOutputStream gZipOut =new GZIPOutputStream(socket.getOutputStream());

ObjectOutputStream out =new ObjectOutputStream(gZipOut);

CSSO.debug("Opened output socket for sending request "+socket);

try

{

Thread.sleep(longSleep);

}

catch(InterruptedException x)

{

// just ignore.

}

out.writeInt(requestType);

CSSO.debug("Wrote request type "+requestType);

out.flush();

gZipOut.finish();

try

{

Thread.sleep(mediumSleep);

}

catch(InterruptedException x)

{

FLOW.debug("Thread.sleep x was interrupted");

}

// we now need to read the responses off

// the socket.

CSSO.debug("Opening response input stream for processes from socket "+socket );

GZIPInputStream gZipIn =new GZIPInputStream(socket.getInputStream());

ObjectInputStream in =new ObjectInputStream(gZipIn);//error line

CSSO.debug("Opened input socket for receiving data "+socket);

try

{

Thread.sleep(shortSleep);

}

catch(InterruptedException x)

{

FLOW.debug("Thread.sleep x was interrupted");

}

The output part seems to work fine, but when it gets to this line :

ObjectInputStream in =new ObjectInputStream(gZipIn);

I get the following exception:

java.io.EOFException: Unexpected end of ZLIB input stream

at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:216)

at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:134)

at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:87)

at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2217)

at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2230)

at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2698)

at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:750)

at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)

Can anyone help :(

Thanks,

James

[3557 byte] By [James00a] at [2007-11-27 1:20:45]
# 1
That's only half the code.Does the server read the request correctly?Does the code that writes the response do the same things as the code that writes the request?
ejpa at 2007-7-11 23:58:02 > top of Java-index,Core,Core APIs...
# 2

Ah yes, I see I have forgotten the server code :D

Here it is :

FLOW.info("bbRad AdminServer listening on port "+_portNumber+"....");

while ( !_exitAdminServer )// I've termed this one the Main While Loop

{

Socket client = server.accept();

CSSO.info("Admin client connection received from "+client.getRemoteSocketAddress());

try

{

Thread.sleep(100);

}

catch(InterruptedException x)

{

// just ignore.

}

CSSO.debug("opening input stream from socket "+client);

GZIPInputStream gZipIn = new GZIPInputStream(client.getInputStream());

ObjectInputStream in = new ObjectInputStream(gZipIn);

// We need to read the first int off the socket

// to identify what type of request we have.

int type = in.readInt();

CSSO.debug("Read request type "+type+" from port "+client.getPort() );

switch (type)

{

//massive switch here

I still have problems, i now get this exception :

java.io.IOException: write beyond end of stream

at java.util.zip.DeflaterOutputStream.write(DeflaterOutputStream.java:104)

at java.util.zip.GZIPOutputStream.write(GZIPOutputStream.java:72)

at java.io.ObjectOutputStream$BlockDataOutputStream.writeBlockHeader(ObjectOutputStream.java:1689)

at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1674)

at java.io.ObjectOutputStream$BlockDataOutputStream.flush(ObjectOutputStream.java:1621)

at java.io.ObjectOutputStream.flush(ObjectOutputStream.java:655)

Am I doing gZipOut.finish() in the first piece of code in the wrong place or something?

Thanks

James00a at 2007-7-11 23:58:02 > top of Java-index,Core,Core APIs...
# 3

Still can't see all the code, but in your sending code you are creating an ObjectOutputStream and a GZIPOutputStream for the request type and another pair of these for the request. If you're not creating an ObjectInputStream and a GZIPInputStream for the request type and another pair of these for the request, it won't work.

ejpa at 2007-7-11 23:58:02 > top of Java-index,Core,Core APIs...