InputStream.read() 返回的 0 是什么意思?如何处理? [英] What 0 returned by InputStream.read() means? How to handle this?

查看:18
本文介绍了InputStream.read() 返回的 0 是什么意思?如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 可能从对 <​​a href=" 的调用返回 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

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

我的猜测:你使用了 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:

使用此方法的返回值来分配用于保存此流中所有数据的缓冲区永远是不正确的.

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

这篇关于InputStream.read() 返回的 0 是什么意思?如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆