简体   繁体   English

Java IO中的“ while(-1!=(len = in.read(b)))”和“ while((len = in.read(b))> 0)”之间有什么区别?

[英]What is difference between “while (-1 != (len = in.read(b)))” and “while ((len = in.read(b)) > 0)” in Java IO?

I have this (working) code: 我有此(工作)代码:

BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
byte[] b = new byte[1024];
int len;
while (-1 != (len = in.read(b))) {
    fos.write(b, 0, len);
}
fos.flush();

But if I change the while (-1 != (len = in.read(b))) to while ((len = in.read(b)) > 0) , the stream cannot finish. 但是,如果我将while (-1 != (len = in.read(b)))更改为while ((len = in.read(b)) > 0) ,则流无法完成。 Why is this? 为什么是这样?

On first sight, these two conditions may seem very different. 乍一看,这两个条件可能看起来非常不同。 Let's rearrange them so that (len = in.read(b)) is always on the left: 让我们重新排列它们,使(len = in.read(b))始终在左侧:

(len = in.read(b)) != -1

(len = in.read(b)) > 0

The expression (len = in.read(b)) evaluates to just in.read(b) . 表达式(len = in.read(b))计算结果仅为in.read(b) Therefore, the only difference between the two conditions is that the first checks if read does not return -1, while the second checks if read returns a value greater than 0. 因此,这两个条件之间的唯一区别是,第一个条件检查是否read不返回-1,而第二个条件检查read返回大于0的值。

Let's look at what read can return : 让我们看一下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。

This means that read does not return anything less than -1, which in turn means that the two conditions in question will only evaluate to different values if read returns 0. But look, read only returns 0 when no byte is read, and the only time that no byte is read is when you pass in an array of length 0: 这意味着, read不返回任何小于-1,这反过来又意味着,如果有问题的两个条件只计算为不同的值, read返回0。但是你看, read只返回0时没有字节读取,唯一的当您传入长度为0的数组时,没有字节被读取的时间是:

If the length of b is zero, then no bytes are read and 0 is returned; 如果b的长度为零,则不读取任何字节,并返回0;否则,返回0。 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; 如果由于流位于文件末尾而没有字节可用,则返回值-1;否则返回值-1。 otherwise, at least one byte is read and stored into b. 否则,读取至少一个字节并将其存储到b。

Your array has a constant length of 1024 , so in this particular case, the two conditions will produce identical results. 您的数组的恒定长度为1024 ,因此在这种特殊情况下,两个条件将产生相同的结果。

According to the documentations for the method InputSteam.read(byte[] b) : 根据方法InputSteam.read(byte[] b)文档

If the length of b is zero, then no bytes are read and 0 is returned ; 如果b的长度为零,则不读取任何字节,并返回0 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 ; 如果由于流位于文件末尾而没有字节可用, 则返回-1 ;否则, 返回-1 otherwise, at least one byte is read and stored into b. 否则,读取至少一个字节并将其存储到b。

So, basically, when you use while ((len = in.read(b)) > 0) you are exiting the loop if read() return 0 (meaning that the length of the last read byte is 0), even though the end of stream has not been reached and, thus, there is still data to be read (the stream is not finished). 因此,基本上,当您使用while ((len = in.read(b)) > 0) ,如果read()返回0(意味着最后一个读取字节的长度为while ((len = in.read(b)) > 0)您将退出循环,即使尚未到达流的末尾,因此,仍有数据要读取(流未结束)。

You are entirely changing condition rule. 您正在完全更改条件规则。 The first statement check value exactly for not matching -1, so your new condition should met the requirements that value is in range of (-infinity,-1) and (-1, infinity) 第一条语句检查的值正好与-1不匹配,因此您的新条件应满足该值在(-infinity,-1)和(-1,infinity)范围内的要求

(-1 != (len = in.read(b))) // read until reach end of stream
(len = in.read(b)) > 0) // read until data are present but this is wrong

Change it to: 更改为:

(len = in.read(b)) >= 0)

I am not sure why to change this condition anyway. 我不确定为什么还是要更改此条件。 As docs say: 正如文档所说:

returns the number of bytes read, or -1 if the end of the stream has been reached. 返回读取的字节数;如果已到达流的末尾,则返回-1。

Situation described here 这里描述的情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM