问题描述
InputStream.read
返回的 0(读取的字节数)是什么意思?如何处理这种情况?
What 0 (number of bytes read) returned by InputStream.read
means? How to handle this situation?
更新:我的意思是 read(byte[] b)
或 read(byte[] b, int off, int len)
方法返回读取的字节数.
Update: I mean read(byte[] b)
or read(byte[] b, int off, int len)
methods which return number of bytes read.
推荐答案
InputStream
可能从对 0 的唯一情况http://java.sun.com/javase/6/docs/api/java/io/InputStream.html#read(byte[])" rel="noreferrer">read(byte[])
是当传入的 byte[]
长度为 0 时:
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
按照 JavaDoc 的这一部分的规定:
As specified by this part of the JavaDoc:
如果 b 的长度为零,则不读取任何字节并返回 0
我的猜测:你使用了 available()
以查看缓冲区应该有多大,并返回 0
.请注意,这是对 available()
的误用.JavaDoc 明确指出:
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:
使用此方法的返回值来分配用于保存此流中所有数据的缓冲区永远是不正确的.
这篇关于InputStream.read() 返回的 0 是什么意思?如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!