views:

72

answers:

2

I have a DataInputStream, created from a Socket in Java. The connection is to a simple web server that uses chunked transfer encoding. The web server does in fact work in normal browser. But in my program, I am attempting to read, I read the first first bytes (some 5kb of data). But each read after that returns 0 bytes read. Isn't it supposed to block until it can read?

Note: This usually doesn't occur. The problem is with the server I am connecting to.

Also, this code here all returns false even after the bytesread == 0.:

        System.out.println(socket.isClosed());
        System.out.println(socket.isInputShutdown());
        System.out.println(socket.isOutputShutdown());

And here are the resp headers:

HTTP/1.1 200 OK

Date: Tue, 08 Jun 2010 14:01:01 GMT

Server: Apache/2.2.11 (Unix) PHP/5.2.10

X-Powered-By: PHP/5.2.10

Expires: Thu, 19 Nov 1981 08:52:00 GMT

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Pragma: no-cache

Keep-Alive: timeout=5, max=100

Connection: Keep-Alive

Transfer-Encoding: chunked

Content-Type: text/html
A: 

You are right, an InputStream should never return 0 on a read. It should either block until a byte is available, or return -1 which indicates EOF.

Any chance you could provide a test case? I've seen a bug like this before.

Keith Randall
I'll look into it. It is kind of hard because it depends on the server too. Most of the time this does not occur.
Zombies
That's not correct, see above. If the buffer is length zero or the specified length is zero, it will return zero.
EJP
Indeed, I wasn't expecting a read request for 0 bytes.
Keith Randall
+1  A: 

According to http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataInputStream.html#read(byte[]) it is possible and valid for a DataInputStream to return 0. This should not be a problem since you should be testing for -1 for the end of stream.

Romain Hippeau
COrrect. I didn't notice that in read(byte[], off, len) if len = 0 it could return 0. and len was set to 0 accidently.
Zombies