This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

? return value of read() when end of buffer reached, but more input pending  RSS feed

 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am hoping someone can explain what happens in the following scenario:
say there is a BufferedReader that is open on an input source, such as a file or stream. another application is writing to the source as the BufferedReader is reading from it, but there may be pauses between write operations. (imagine a client sporadically sending messages to a server, or reading log messages as they are written to the logfile.) if the BufferedReader has read all available information, but the stream has not been closed, or -1 is not returned, does the .read() method block, or return null, or something else? the API says it should block, but i ran in to a problem late last night where the read() command was in a loop that would process input, then read() again - but for some reason was skipping over the read() and re-use the last input value. has anyone seen this, or know what may be causing it? i apologize as i don't have the source handy to post - will put it up later when i have access to it.
thanks,
Jon
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using read() (which reads one char, as an int) or read(char[]) (which reads multiple chars into a buffer). The former should definitely block if no char is available but the stream has not ended. If it's doing something else, that's a problem.
In the case of read(char[]), you need to look at the return value to see how many bytes have been successfully read. This could be less than the buffer size, even 0, if the stream would block past a certain point. If the read size was less than the buffer size, the buffer may still contain values from the last read. So if you're not making proper use of the read size, you may be reading old values. Try something like this:

You can do something else in the loop if writing to another stream isn't waht you want, but make sure that whatever you do, you don't use any index value >= bytesRead, since it's either old data, or out of bounds.
If this didn't answer your questions, please post some code when you get a chance.
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim. I think it was the "bytes read" problem you described - it seemed like it was using bytes still in the buffer. the fact that i wrote the code at 2am didn't help either. i re-wrote it using a BufferedReader and the readLine() method, and now it is working.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!