简体   繁体   English

JAVA中的流结束

[英]end of stream in JAVA

I am confused by the following statement that appears here 我对此处出现的以下声明感到困惑

The basic read() method of the InputStream class reads a single unsigned byte of data and returns the int value of the unsigned byte. InputStream类的基本read()方法读取单个无符号数据字节,并返回无符号字节的int值。 This is a number between 0 and 255. If the end of stream is encountered, it returns -1 instead; 这是0到255之间的数字。如果遇到流的末尾,则返回-1; and you can use this as a flag to watch for the end of stream. 并且您可以将其用作标志来监视流的结尾。

Since one byte can represent up to 256 integers, I fail to see how it can represent 0 to 256 and -1. 由于一个字节最多可以表示256个整数,因此我无法看到它如何表示0到256和-1。 Can someone please comment on what I am missing here? 有人可以评论我在这里缺少的东西吗?

Although the read() operation just reads a byte it actually returns an int so there is no problem. 虽然read()操作只是读取一个字节,但它实际上返回一个int所以没有问题。

Just values in range 0-255 are returned though, aside from the special -1 end of stream value. 除了流值的特殊-1结束之外,仅返回范围0-255中的值。

它返回一个int ,而不是一个byte ,所以虽然它通常只包含0-255,但它可以包含其他值。

InputStream#read()的返回类型是一个int ,如果该值在0-255范围内,则可以将其读取为一个byte

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

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