Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedReader and ready state

 
Zaeed McColin
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys. I'm wonder about BufferedReader and it's stream ready state. I mean does BufferedReader (readline() method) take care of ready state of incoming stream or not? because when I try to read stream with InputStream or InputStreamReader I have to check the ready state of the stream (ready() method) before begin reading the stream (read() method), I've faced with kinda this issue when the connection between two node is provided with a low-bandwidth connection.
according to API and my tests it seems take care of ready state (just readline() method) but API says it will returns null when end of the stream (EOF) is reached but sometimes it cannot work well, it seems data is blocked. I don't know.
I parse the Socket incoming stream.
My JVM version is 1.6.0.16 (OS: windxp sp2, x86 and win2003).
Thnaks
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arash M. Dehghani wrote:when I try to read stream with InputStream or InputStreamReader I have to check the ready state of the stream (ready() method) before begin reading the stream (read() method)

Hm, that will be difficult considering there is no ready() method on InputStream or InputStreamReader. Do you mean available()? That method is probably best ignored as it's unreliable. You don't need to check available() before you call read() - and I would argue that in most cases, you shouldn't, simply because it can give you misleading results. What does it mean when available() returns 0? Well, it might mean the stream is closed. Or it might mean the disc reader is still moving to the correct position on the disc. Or a human typing at the other end of your network connection is still typing, very slowly by machine standards. Ultimately, you need to read() again, and find out if it returns new bytes, eventually, or returns -1, indicating end of file.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arash probably wrapped the InputStream into an InputStreamReader. The same holds though, ready() returns true if read() will not block so there is at least one character available. It's just as useless as available() in regards to checking if there is more data.
 
Zaeed McColin
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike and Rob. you mentioned very important note. well as you said the ready() method just is useful when only next byte of the stream available. and available() method says how many byte you can buffer that it won't help me anyway. but as you said I started to read the stream with read() method directly. but in low-bandwidth connection it returns -1 (EOS) when I'm sure that the stream is not ready. but readline() method from buffered reader, reads a line well, but as I said sometimes it works crazy when EOS is reached.
generally I cannot trust read() method and according to API if I can combine two ready() and read() method together to ensure that next byte is ready for read, may my problem will be solved. I don't know. except my solution, what's you solution about this problem? thanks
Sorry Mike I've got mistake while I was typing, yes InputStream doesn't have any ready() method, it's for Reader interface.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!