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
According to http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataInputStream.html#read(byte%5B%5D) 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.