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
Zombies
  • 25,039
  • 43
  • 140
  • 225
  • Should we assume you mean `ByteArrayInputStream` rather than `ByteInputStream`? – Powerlord Jun 08 '10 at 15:42
  • Sorry completely. Actually it is DataInputStream! – Zombies Jun 08 '10 at 15:43
  • That is strange... the read methods for `DataInputStream` *should* block. The reason I asked is because `ByteArrayInputStream` appears to be the exception to the rule on blocking reads (it never blocks). – Powerlord Jun 08 '10 at 15:47

2 Answers2

1

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
  • 24,113
  • 5
  • 60
  • 79
  • 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 Jun 08 '10 at 16:56
-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.

Community
  • 1
  • 1
Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • 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 Jun 08 '10 at 15:39
  • That's not correct, see above. If the buffer is length zero or the specified length is zero, it will return zero. – user207421 Jun 08 '10 at 23:58