InputStream.read()返回的是什么意思?怎么办呢? [英] What 0 returned by InputStream.read() means? How to handle this?

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

问题描述

InputStream.read 返回的0(读取的字节数)是什么意思?如何处理这种情况?

What 0 (number of bytes read) returned by InputStream.read means? How to handle this situation?

更新:我的意思是读取(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 返回的情况致电 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()返回的是什么意思?怎么办呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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