10

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.

GrowinMan
  • 4,891
  • 12
  • 41
  • 58

5 Answers5

3

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.

GrowinMan
  • 4,891
  • 12
  • 41
  • 58
wattostudios
  • 8,666
  • 13
  • 43
  • 57
3

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    I 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. http://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#read%28%29 – wattostudios Apr 08 '12 at 05:28
  • @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()** – GrowinMan Apr 08 '12 at 05:32
  • There are several different `read()` methods. One returns a single byte; others fill an array and return the _number_ of bytes that were read. – Adam Liss Apr 08 '12 at 05:32
  • I 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 Apr 08 '12 at 05:33
2

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.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
1

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.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Ok. 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? – GrowinMan Apr 08 '12 at 05:35
  • 1
    PITA == "Pain In The gluteus mAximus" ;) – paulsm4 Apr 08 '12 at 05:41
  • Just because numbers in Java default to being `int` values, so if it were a `short` you would need to do conversions from `short` to `int`, even if the conversions are handled automatically by the Java engine – wattostudios Apr 08 '12 at 05:41
0

The FileInputStream class makes it possible to read the contents of a file as a stream of bytes. Here is a simple example:

InputStream input = new FileInputStream("c:\\data\\input-text.txt");

int data = input.read();
while(data != -1) {
  //do something with data...
  doSomethingWithData(data);

  data = input.read();
}
input.close();

Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.

The read() method of a FileInputStream returns an int which contains the byte value of the byte read. If the read() method returns -1, there is no more data to read in the stream, and it can be closed. That is, -1 as int value, not -1 as byte value. There is a difference here!

Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
  • -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 is `true` of course because byte is from -128 to 127 which means -1 is also legal byte value. – Boris Šuška Jul 23 '13 at 14:40