java 为什么 InputStream#read() 返回一个 int 而不是一个字节?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4659659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:23:51  来源:igfitidea点击:

Why does InputStream#read() return an int and not a byte?

javainputstream

提问by user489041

Why does InputStream#read()return an intand not a byte?

为什么InputStream#read()返回 anint而不是 a byte

回答by BalusC

Because a bytecan only hold -128 until 127, while it should return 0 until 255 (and -1 when there's no byte left (i.e. EOF)). Even if it returned byte, there would be no room to represent EOF.

因为 abyte只能保存 -128 直到 127,而它应该返回 0 直到 255(并且 -1 当没有字节剩余时(即 EOF))。即使它返回byte,也没有空间来表示 EOF。

A more interesting question is why it doesn't return short.

一个更有趣的问题是为什么它不返回short

回答by Edwin Buck

It returns an int because when the stream can no longer be read, it returns -1.

它返回一个 int 值,因为当无法再读取流时,它返回 -1。

If it returned a byte, then -1 could not be returned to indicate a lack of input because -1 is a valid byte. In addition, you could not return value above 127 or below -128 because Java only handles signed bytes.

如果它返回一个字节,则无法返回 -1 以指示缺少输入,因为 -1 是有效字节。此外,您不能返回大于 127 或小于 -128 的值,因为 Java 只处理有符号字节。

Many times when one is reading a file, you want the unsigned bytes for your processing code. To get values between 128 and 255 you could use a short, but by using an int you will align the memory registers with your data bus more efficiently. As a result, you don't really lose any information by using an int, and you probably gain a bit of performance. The only downside is the cost of the memory, but odds are you won't be hanging on to that int for long (as you will process it and turn it into a char or byte[]).

很多时候,在读取文件时,您需要处理代码的无符号字节。要获得 128 到 255 之间的值,您可以使用 short,但通过使用 int,您将更有效地将内存寄存器与数据总线对齐。因此,使用 int 并不会真正丢失任何信息,而且您可能会获得一些性能。唯一的缺点是内存成本,但很可能您不会长时间使用该 int(因为您将处理它并将其转换为 char 或 byte[])。

回答by Nanne

So it can return "-1" . It must do that when there is no more bytes to read.

所以它可以返回 "-1" 。当没有更多字节要读取时,它必须这样做。

You can't have it return a byte sometimes AND -1 for EOF/nobyte/whatever, so it returns an int ;)

你不能让它有时返回一个字节和 -1 表示 EOF/nobyte/什么的,所以它返回一个 int ;)

回答by wesoly

Because EOF (end of file or generally end of data) can't be represented using char.

因为 EOF(文件结尾或通常的数据结尾)不能使用 char 表示。

回答by andy

as the Java doc says in InputStream#read, The value byte is returned as an int in the range 0 to 255. That is to say the byte value[-128~127] has been changed to int value[0~255], so the return value can be used to represent the end of the stream.

正如 Java 文档在InputStream#read 中所说的那样,值字节作为 0 到 255 范围内的 int 返回。也就是说字节值[-128~127]变成了int值[0~255],所以可以用返回值来表示流的结束。

回答by radistao

Appending to BalusC answer:

附加到BalusC 答案

  • not a byteto allow [0; 255]as main capacity and additionaly -1as EOF result
  • intis used to adjust result to machine word (one of the main requirements to I/O operations - velocity, so they should work as fast as possible!)
  • not abyte允许[0; 255]作为主要容量和附加-1作为 EOF 结果
  • int用于将结果调整为机器字(I/O 操作的主要要求之一 -速度,因此它们应该尽可能快地工作!)

Exception is not used because they are significantly slow!

不使用异常,因为它们非常慢!