What 0 returned by InputStream.read(byte[]) means? How to handle this?
Asked Answered
T

4

13

What 0 (number of bytes read) returned by InputStream.read(byte[]) and InputStream.read(byte[], int, int) means? How to handle this situation?

To be clear, I mean read(byte[] b) or read(byte[] b, int off, int len) methods which return number of bytes read.

Tamqrah answered 23/2, 2010 at 15:40 Comment(0)
A
18

The only situation in which a InputStream may return 0 from a call to read(byte[]) is when the byte[] passed in has a length of 0:

 byte[] buf = new byte[0];
 int read = in.read(buf); // read will contain 0

As specified by this part of the JavaDoc:

If the length of b is zero, then no bytes are read and 0 is returned

My guess: you used available() to see how big the buffer should be and it returned 0. Note that this is a misuse of available(). The JavaDoc explicitly states that:

It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

Arman answered 23/2, 2010 at 15:49 Comment(1)
I agree: >This method blocks until input data is available, end of file is detected, or an exception is thrown. >If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. >If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b. Thanks!Tamqrah
R
10

Take a look at the implementation of javax.sound.AudioInputStream#read(byte[] b, int off, int len) ... yuck. They completely violated the standard java.io.InputStream semantics and return a read size of 0 if you request fewer than a whole frame of data.

So unfortunately; the common advice (and api spec) should preclude having to deal with return of zero when len > 0 but even for JDK provided classes you can't universally rely on this to be true for InputStreams of arbitrary types.

Again, yuck.

Ragucci answered 16/9, 2011 at 7:8 Comment(2)
InputStream from UrlConnection.getInputStream() seems also violated the standard java.io.InputStream semantics and return a read size of 0 when handling large file. Can anyone confirm this?Woolson
^ Yes, thats confirmed, I came to this question when searching for the same exact problem of UrlConnection.getInputStream read returning a zero 0Coparcener
Q
3

According to Java API Doc:

http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read(byte[])

It only can happen if the byte[] you passed has zero items (new byte[0]).

In other situations it must return at least one byte. Or -1 if EOF reached. Or an exception.

Of course: it depends of the actual implementation of the InputStream you are using!!! (it could be a wrong one)

Quinque answered 23/2, 2010 at 15:50 Comment(0)
P
0

I observed the same behavior (reading 0 bytes) when I build a swing console output window and made a reader-thread for stdout and stderr via the following code:

                this.pi = new PipedInputStream();
                po = new PipedOutputStream((PipedInputStream)pi);
                System.setOut(new PrintStream(po, true));
When the 'main' swing application exits, and my console window is still open I read 0 from this.pi.read(). The read data was put on the console window resulting in a race condition some how, just ignoring the result and not updating the console window solved the issue.
Petry answered 4/7, 2012 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.