• Post Reply Bookmark Topic Watch Topic
  • New Topic

why InputStreamReaders read method returns int  RSS feed

 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.

Oracle doc says


Why the heck , any read() method returns integer? From description it should return characters , not digits that represent character?
How can i get those digits converted to characters then?
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API:
"
Returns:
The character read, or -1 if the end of the stream has been reached.
"

char cannot go to -1.
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you explain " a bridge from byte streams to character streams"?
Where is character stream, if i get int and need to do cast to char each time i read next portion of data ?
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the only other option would have been to throw an Exception on EOS.
Some might argue that would have been a better option.
Me? I think they worked of C's stdio. fgetc returns an int character, which can be -1, representing EOF.
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave. This starts to make sence with your assumption.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Gurianov wrote:How do you explain " a bridge from byte streams to character streams"?
Where is character stream, if i get int and need to do cast to char each time i read next portion of data ?

I believe it's already been explained to you: a char CANNOT hold a value of -1, which is what is used to indicate an end-of file - and '\uFFFF' (which would be the binary equivalent) is an actual character in the Unicode set.

Yes, it's cumbersome, which is why, when you want to read characters, you generally use a Reader.

Winston
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Yes, it's cumbersome, which is why, when you want to read characters, you generally use a Reader.

Winston


Ah, but the interface for Reader has the aforementioned read() method...

They just do these things to mess with our heads.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!