简体   繁体   English

DataInputStream持续返回0是什么意思?

[英]What does it mean when a DataInputStream keeps returning 0?

I have a DataInputStream , created from a Socket in Java. 我有一个DataInputStream ,它是用Java中的一个Socket创建的。 The connection is to a simple web server that uses chunked transfer encoding. 该连接是使用分块传输编码的简单Web服务器。 The web server does in fact work in normal browser. Web服务器实际上可以在普通浏览器中工作。 But in my program, I am attempting to read, I read the first first bytes (some 5kb of data). 但是在我的程序中,我试图读取,我读取了第一个头字节(大约5kb的数据)。 But each read after that returns 0 bytes read. 但是此后的每次读取都返回读取的0字节。 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.: 同样,即使在bytesread == 0之后,此代码也都返回false:

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

And here are the resp headers: 这是resp标头:

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. 没错,InputStream永远不应在读取时返回0。 It should either block until a byte is available, or return -1 which indicates EOF. 它应该阻塞直到一个字节可用为止,或者返回-1表示EOF。

Any chance you could provide a test case? 您有机会提供测试用例吗? I've seen a bug like this before. 我以前见过这样的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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