简体   繁体   中英

What does it mean when a DataInputStream keeps returning 0?

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

根据http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataInputStream.html#read(byte []) ,DataInputStream返回0是可能且有效的。这应该没问题,因为您应该在流的末尾测试-1。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM