[英]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
: Theshort
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 withbyte
, the same guidelines apply: you can use ashort
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: 有几个原因:
short
type this would cause an overflow 您可以轻松读取超过32 KB的数据,并且使用short
类型会导致溢出 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位数,适合int
和short
。 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.