DataInputStream(new GZIPInputStream).readFully throws EOFException

A

A. Farber

Hello,

I'm trying to extend a web-chat applet by adding gzip-compression.

The data sent from the web-server is really ok: I'm looking at it
using tcpdump (full output listed at the bottom) and see that the
content-length (32) matches the length of the body and that the
compressed bytes match the string I'm sending by my Apache
module ("player=Vasja").

However I can't read the data in my applet - I get an EOFException:

Mon Mar 20 18:13:07 CET 2006 dis=java.io.DataInputStream@6f9b8e
Mon Mar 20 18:13:07 CET 2006 getData, status=HTTP/1.1 200 OK
Mon Mar 20 18:13:07 CET 2006 getData, clength=32
Mon Mar 20 18:13:07 CET 2006 getData,
dis=java.io.DataInputStream@6f9b8e
java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:178)
at java.io.DataInputStream.readFully(DataInputStream.java:152)
at Net.getData(Net.java:133)
at Pref.run(Pref.java:165)
at java.lang.Thread.run(Thread.java:595)
Mon Mar 20 18:13:07 CET 2006 getData end

Here is my code. Does anybody please know what is wrong there?
Aren't I allowed to contstruct DataInputStream(new GZIPInputStream(
URLConnection.getInputStream())) and call readFully on it?

try {
DataInputStream dis;

URLConnection conn = url.openConnection();
if (modified != 0)
conn.setIfModifiedSince(modified);
conn.setRequestProperty("Accept-Encoding", "gzip");

int clength = conn.getContentLength();
String encoding = conn.getContentEncoding();
if (encoding != null &&
encoding.equalsIgnoreCase("gzip")) {
dis = new DataInputStream(new
GZIPInputStream(conn.getInputStream()));
trace("dis=" + dis);
} else
dis =
new DataInputStream(conn.getInputStream());

trace("getData, status=" + conn.getHeaderField(0));
trace("getData, clength=" + clength);
trace("getData, dis=" + dis);

if (clength > 0) {
byte[] body = new byte[clength];
dis.readFully(body); // line 133
trace("getData, body=" + new String(body));

modified = conn.getLastModified();
hol = parseResponse(new String(body));
}

dis.close();
url = null;
} catch (Exception ex) {
ex.printStackTrace();
}

When I disable gzip both in the applet and my server ( preferans.de ),
then everything works as I want, even the HTTP Keep-Alive.

Thank you
Alex

------
16:31:50.804896 127.0.0.1.80 > 127.0.0.1.35892: P 33174:33526(352) ack
36097 win 16384 <nop,nop,timestamp 146348926 0> (DF)
0000: 4500 0194 4f04 4000 4006 ec5d 7f00 0001 E...O.@.@.ì]....
0010: 7f00 0001 0050 8c34 3750 0ec8 7452 e306 .....P.47P.ÈtRã.
0020: 8018 4000 fe8d 0000 0101 080a 08b9 1b7e ..@.þ........¹.~
0030: 0000 0000 4854 5450 2f31 2e31 2032 3030 ....HTTP/1.1 200
0040: 204f 4b0d 0a44 6174 653a 204d 6f6e 2c20 OK..Date: Mon,
0050: 3230 204d 6172 2032 3030 3620 3136 3a33 20 Mar 2006 16:3
0060: 313a 3530 2047 4d54 0d0a 5365 7276 6572 1:50 GMT..Server
0070: 3a20 4170 6163 6865 2f31 2e33 2e32 3920 : Apache/1.3.29
0080: 2855 6e69 7829 2050 4850 2f35 2e30 2e35 (Unix) PHP/5.0.5
0090: 206d 6f64 5f73 736c 2f32 2e38 2e31 3620 mod_ssl/2.8.16
00a0: 4f70 656e 5353 4c2f 302e 392e 3767 0d0a OpenSSL/0.9.7g..
00b0: 4c61 7374 2d4d 6f64 6966 6965 643a 204d Last-Modified: M
00c0: 6f6e 2c20 3230 204d 6172 2032 3030 3620 on, 20 Mar 2006
00d0: 3136 3a33 313a 3439 2047 4d54 0d0a 436f 16:31:49 GMT..Co
00e0: 6e74 656e 742d 4c65 6e67 7468 3a20 3332 ntent-Length: 32
00f0: 0d0a 436f 6e74 656e 742d 456e 636f 6469 ..Content-Encodi
0100: 6e67 3a20 677a 6970 0d0a 4b65 6570 2d41 ng: gzip..Keep-A
0110: 6c69 7665 3a20 7469 6d65 6f75 743d 3930 live: timeout=90
0120: 2c20 6d61 783d 310d 0a43 6f6e 6e65 6374 , max=1..Connect
0130: 696f 6e3a 204b 6565 702d 416c 6976 650d ion: Keep-Alive.
0140: 0a43 6f6e 7465 6e74 2d54 7970 653a 2061 .Content-Type: a
0150: 7070 6c69 6361 7469 6f6e 2f78 2d77 7777 pplication/x-www
0160: 2d66 6f72 6d2d 7572 6c65 6e63 6f64 6564 -form-urlencoded
0170: 0d0a 0d0a 1f8b 0800 0000 0000 0003 2bc8 ..............+È
0180: 49ac 4c2d b20d 4b2c ce4a 0400 ed8c b5fb I¬L-².K,ÎJ..í.µû
0190: 0c00 0000 ....

(as you see there are even 2 magic gzip bytes above: 1f and 8b).

laptop:afarber {533} echo -n "player=Vasja" | gzip -c | xxd
0000000: 1f8b 0800 0000 0000 0003 2bc8 49ac 4c2d ..........+.I.L-
0000010: b20d 4b2c ce4a 0400 ed8c b5fb 0c00 0000 ..K,.J..........
 
D

Dave Mandelin

A. Farber said:
Hello,

I'm trying to extend a web-chat applet by adding gzip-compression.

The data sent from the web-server is really ok: I'm looking at it
using tcpdump (full output listed at the bottom) and see that the
content-length (32) matches the length of the body and that the
compressed bytes match the string I'm sending by my Apache
module ("player=Vasja").

However I can't read the data in my applet - I get an EOFException: ....
byte[] body = new byte[clength];
dis.readFully(body); // line 133 ....
(as you see there are even 2 magic gzip bytes above: 1f and 8b).

laptop:afarber {533} echo -n "player=Vasja" | gzip -c | xxd
0000000: 1f8b 0800 0000 0000 0003 2bc8 49ac 4c2d ..........+.I.L-
0000010: b20d 4b2c ce4a 0400 ed8c b5fb 0c00 0000 ..K,.J..........

Hello,

I believe the problem is that the content-length is the length of the
data sent by the server, i.e., the length of the compressed data.
readFully copies the uncompressed data into an array. In this case, the
uncompressed data is shorter, so when you try to read 32 bytes of
uncompressed data, you get EOF before 32 bytes. That's fine for this
example, but in general, the uncompressed data is of course longer, so
you will have to call dis.read() inside a loop.

Cheers, Dave
 
A

A. Farber

Thank you Dave, your explanation really makes sense.
I've got it working now (readFully the Content-Length bytes first
and after that uncompress the buffer)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top