关于inputstream的read方法返回值的理解

官方文档的解释是:

Returns:the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

返回值:返回的是缓冲流的总字节数,到达文件末尾时返回-1。

当时很难理解,为什么既能返回总字节数,又可以返回-1,怎么能返回两个值…

正确的理解是:
当调用read方法,读取第一个字节时,如果为null(说明已到文件末尾),则返回为-1,
否则,继续读取下一个字节,直到为null,此时就返回读取的字节总数。