Why does the read() in FileInputStream return an integer?
Solution 1
The reason for it returning the value as an int
is that it needs to return a value between 0-255, as well as being able to indicate when there is no more bytes to read from the file. By using an int
, you can return the full range of positive unsigned values 0-255, as well as indicate when the file is complete. It wouldn't be able to provide this with only the 256 distinct values of a byte
value, half of which are negative by Java default.
Solution 2
Sure, but the JavaDocs go on to say..
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
Hopefully more than 127 bytes can be read from a stream at a time.
Solution 3
A byte of data is an unsigned value with a range from 0
to 255
, while a byte
in java is defined to range from -128
to 127
, which doesn't make sense when reading binary data. read()
returns an integer to allow it to use all of the non-negative values to represent valid data, and a negative value to signal end of data.
In general, a function should indicate an error condition or exception using a different mechanism from the one it uses to return data. In the simplest case, it can return a value that cannot be used to represent valid data, to ensure its meaning is unambiguous.
Solution 4
Q: wouldn't it make more sense for the return type of read() to be byte?
A: No, because "byte" can't return the whole range 0..255 (unsigned), and "short" is just a PITA.
GrowinMan
Updated on July 20, 2022Comments
-
GrowinMan almost 2 years
This page shows says that it is so that the method can return -1 when it wants to indicate that there are no more bytes to be read.
But a byte ranges from -128 to 127, right? And wouldn't it make more sense for the return type of read() to be byte since it returns a byte?
Thank you for your time.
-
wattostudios about 12 yearsI made this mistake at first too - the read() method actually returns a single byte of data, not the number of bytes that can be read. docs.oracle.com/javase/7/docs/api/java/io/…
-
GrowinMan about 12 years@Andrew Thompson I think you read the wrong method's documentation. You read the one for public int read(byte[] b) where as I'm referring to public int read()
-
Adam Liss about 12 yearsThere are several different
read()
methods. One returns a single byte; others fill an array and return the number of bytes that were read. -
GrowinMan about 12 yearsI was referring to the method that doesn't need any arguments. Since that's the method that's also used in the example given in the link that I pointed to.
-
GrowinMan about 12 yearsOk. I was thinking maybe by using internal bit-level manipulations the data could be stored in the signed format itself. That was dumb but, I see now. Anyway, I'm curious to know why short is a PITA?
-
paulsm4 about 12 yearsPITA == "Pain In The gluteus mAximus" ;)
-
wattostudios about 12 yearsJust because numbers in Java default to being
int
values, so if it were ashort
you would need to do conversions fromshort
toint
, even if the conversions are handled automatically by the Java engine -
Boris Šuška almost 11 years-1 because of "That is, -1 as int value, not -1 as byte value. There is a difference here!". What is evaluation of
( (byte) -1 ) == ( (int) -1 )
? It istrue
of course because byte is from -128 to 127 which means -1 is also legal byte value.