• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does FileInputStream's read() method return integer?  RSS feed

 
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody, I was reading about I/O streams and came across this particular phrase.

Notice that read() returns an int value. If the input is a stream of bytes, why doesn't read() return a byte value? Using a int as a return type allows read() to use -1 to indicate that it has reached the end of the stream.

What does this mean actually? Does it mean byte variables cant store '-1'? But they can store '-1'. What does the author want to tell the readers from this?

Thank you all in advance. Have a good day.
 
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you think it's possible for an input stream to return any byte it reads as data, and also return some specific byte to indicate there is no more data, then you should tell us what byte that would be.

So think about it. What byte would you choose to indicate there is no more data available? Bear in mind that you can't choose any byte which might appear in the data.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, I din't understood what you were saying. Can you please tell me in somewhat detail?
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay Paul, I think I understood it. Let me please explain you what I understood. Tell me whether its right or not.

When reading bytes of data the least 8 bits in the int is occupied by the data, where as the upper 8 bits are free. To indicate that the file has been ended, while transferring the last byte of data, streams put the last data in lower 8 bits and -1 in the upper 8 bits indicating file end. So when ever the it encounters that a the upper 8 bits are full it means that the file has been ended.

Is this what you were saying? Please correct me if I am wrong.

Thank you in advance.
 
Paul Clapham
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you re-read what I said, you should notice that I didn't say anything about bits. And I didn't say that an int value has 16 bits (it doesn't). And I didn't say that any 16-bit value with all 1's in the first 8 bits represents -1 (it doesn't).

So, no. I wasn't saying any of that. I can't imagine how you thought I was. It's all wrong. And it's way too complicated. When the read() method returns a byte, it returns a value between 0 and 255. If there is no data left in the input stream, then it returns -1 to indicate that. That's all. Don't look for complicated explanations when simple explanations are available.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My point is -1 can fit in a byte. Why the read() method is returning int? If it always return 0 to 255 when data is available it can return -1 using a byte. But why is it returning int?

Hope you understood my doubt. Thank you.
 
Paul Clapham
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, I understood. When I originally replied, I in effect asked you how you expected that 257 different values (all possible bytes between 0 and 255 and the special value -1) could be represented by a byte. So, how do you expect that to be done?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that might help is that the bit pattern for "-1" in an int is different than the bit pattern for "-1" in a byte.

if it successfully reads a character, it can return any of:

00000000 00000000
00000000 00000001
00000000 00000010
00000000 00000011
...
00000000 11111110
00000000 11111111

all of which mean success. There are 256 possible values there. We need one more to indicate failure, which is why we have all those leading 0's.

When it fails, it will return (I think)

11111111 11111111
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it returns 11111111 11111111 11111111 11111111 - 32 bits that are all set to 1.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).

The javadoc for read() says it returns values between 0-255 inclusive...How do you propose the read() method, if it should return a byte, should return any value > +127 when +127 is byte's max value?

Get it now? Did you mean to ask why it doesn't return a short instead?
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for taking pain in explaining me in detail. Thanks a lot.

Have a good day.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!