简体   繁体   English

为什么FileInputStream在java中读取方法返回一个int,而不是short?

[英]Why does FileInputStream read method in java return a int, not a short?

I aware that byte is not a proper type enough to contain result of read method. 我知道byte不是一个足以包含read方法结果的正确类型。
So, read method returns int type value. 因此,read方法返回int类型值。
But i think a short type is more efficient type than int. 但我认为短类型比int更有效。
It could contain the value of range -256~ 255. 它可以包含范围-256~255的值。
Why does read method return int, not short? 为什么read方法返回int,而不是short?

Java documentation on primitive types suggests that short s should be used instead of int s to "save memory in large arrays": 关于原始类型的Java文档建议应该使用short s而不是int来“节省大型数组中的内存”:

short : The short data type is a 16-bit signed two's complement integer. short short数据类型是16位带符号的二进制补码整数。 It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). 它的最小值为-32,768,最大值为32,767(含)。 As with byte , the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters. byte一样,适用相同的指导原则:在内存节省实际上很重要的情况下,您可以使用short来节省大型阵列中的内存。

Since in this situation memory savings do not actually matter, using int is a more consistent choice. 因为在这种情况下,内存节省实际上并不重要,使用int是一个更一致的选择。

There are several reasons: 有几个原因:

  • You can easily read more than 32 KB worth of data and with a short type this would cause an overflow 您可以轻松读取超过32 KB的数据,并且使用short类型会导致溢出
  • Performance of short equal to the performance of int due to the "bitness" of processors. 由于处理器的“位数”, short的性能等于int的性能。 Modern CPUs take numbers of 32 or 64-bits, which fits both int and short . 现代CPU采用32位或64位数,适合intshort There was a performance benefit when running code on old 16-bit processors. 旧的16位处理器上运行代码时一个性能优势。 Long time ago... 很久以前...

That's because read returns the number of bytes read, there is the following method defined in InputStream: 那是因为read返回读取的字节数,在InputStream中定义了以下方法:

public int read(byte[] b) throws IOException

Reads some number of bytes from the input stream and stores them into the buffer array b. 从输入流中读取一些字节数并将它们存储到缓冲区数组b中。 The number of bytes actually read is returned as an integer . 实际读取的字节数以整数形式返回 This method blocks until input data is available, end of file is detected, or an exception is thrown. 此方法将阻塞,直到输入数据可用,检测到文件结尾或引发异常。

The maximum length of an array is about Integer.MAX - 5, since you can do operations with arrays then the return type is an int. 数组的最大长度约为Integer.MAX - 5,因为您可以对数组进行操作,然后返回类型为int。

Read method returns int which signifies number of bytes read from the file, its a design decision taken while writing the API, now lets ask same question other way around: Read方法返回int表示从文件读取的字节数,它是在编写API时做出的设计决定,现在让我们以相反的方式提出相同的问题:

How many maximum bytes we can read ? 我们可以读取多少个字节?

•short: The short data type is a 16-bit signed two's complement integer. •short:短数据类型是16位带符号的二进制补码整数。 It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). 它的最小值为-32,768,最大值为32,767(含)。 You can use a short to save memory in large arrays, in situations where the memory savings actually matters. 在内存节省确实很重要的情况下,您可以使用short来节省大型阵列中的内存。

•int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. •int:默认情况下,int数据类型是32位带符号的二进制补码整数,其最小值为-231,最大值为231-1。

Assuming we will have enough memory to hold bytes, int serves a better candidate over short and hence the API design. 假设我们将有足够的内存来保存字节,int在短期内提供了更好的候选者,因此API设计。 However API leaves the decision to user to decide how many bytes to read based on resources available. 然而,API决定用户根据可用资源决定要读取多少字节。

Also, read method calls native code and on the native language side, these primitive data types are mapped to the closest matching native language data type. 此外, read方法调用本机代码,在本机语言方面,这些原始数据类型映射到最接近的匹配本机语言数据类型。

Cheers !! 干杯!!

The real reason is that the abstract class that FileInputStream inherits from is also the base for InputStreams that are supposed to read characters in any encoding, and it has a method 真正的原因是FileInputStream继承的抽象类也是应该在任何编码中读取字符的 InputStreams的基础,它有一个方法

public int read();

that is used in character reading streams to read a single character. 用于字符读取流以读取单个字符。

As we all know, a char is an unsigned integer type of 16-bit. 众所周知, char是无符号整数类型的16位。

Hence, a short could not be used, because it only has 32k (or 15 bits worth) positive values, but we need 64k (or 16 bits worth) for characters. 因此,不能使用short ,因为它只有32k(或15位)正值,但我们需要64k(或16位值)的字符。

And we need (-1) to signal end of file. 我们需要(-1)来表示文件结束。

It is the same story with Reader s of all kinds and sorts. Reader的各种各样的故事也是如此。

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

相关问题 为什么 FileInputStream.read() 返回 int? - Why does FileInputStream.read() return int? 为什么InputStream read()返回一个int而不是short? - Why does InputStream read() return an int and not a short? 为什么在一个应该返回简短工作的方法中返回一个int? - Why does returning an int in a method that should return a short work? 为什么Inpustream读取方法返回一个int? - Why does an Inpustream read method return an int? 为什么带byte参数的方法调用带short的方法,为什么不是int? - Why does the method with byte parameter calls the method with short, why not int? 如果FileInputStream的read()方法返回1个字节,而java中的char占据2个字节,则下面的程序如何工作 - If read() method of FileInputStream return 1 byte and char in java occupy 2 bytes, how below program works 为什么两个短值的按位与会导致 Java 中的 int 值? - Why does bitwise AND of two short values result in an int value in Java? 为什么 Java API 使用 int 而不是 short 或 byte? - Why does the Java API use int instead of short or byte? 在 Java 中,FileInputStream 中的 read() 方法为什么不会抛出“不兼容的类型:可能的有损转换”? - In java, how come method read() from FileInputStream works does not throw " incompatible types: possible lossy conversion"? FileInputStream读取方法的样式 - Style of FileInputStream read method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM