Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the read() in FileInputStream return an integer? [duplicate]

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.

like image 793
GrowinMan Avatar asked Apr 08 '12 05:04

GrowinMan


People also ask

Why does InputStream read return an int?

It returns an int because when the stream can no longer be read, it returns -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.

What is the return type of read () method in Java stream?

read() method reads the next byte of the data from the the input stream and returns int in the range of 0 to 255. If no byte is available because the end of the stream has been reached, the returned value is -1.

What does InputStream read return?

read() methods are used to read a byte or character, respectively, from a stream. The InputStream. read() method reads a single byte from an input source and returns its value as an int in the range 0 to 255 ( 0x00 - 0xff ).

How does FileInputStream work in Java?

A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader .


3 Answers

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.

like image 191
wattostudios Avatar answered Nov 15 '22 16:11

wattostudios


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.

like image 22
Andrew Thompson Avatar answered Nov 15 '22 16:11

Andrew Thompson


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.

like image 32
Adam Liss Avatar answered Nov 15 '22 18:11

Adam Liss