Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Using BufferedReader with a URL

Thread view: 
Eric Capps - 16 Aug 2006 19:32 GMT
I'm using BufferedReader with a URL to try to read information from that
URL and do something with it. The BufferedReader is basically declared
as follows:

  InputStreamReader stream = new InputStreamReader((new
URL(url)).openStream());
  BufferedReader fileReader = new BufferedReader(stream);

There are a certain number of lines "length" I'm expecting in the file.
I then read from the BufferedReader as follows:

  while(fileReader.ready() && i < length){
    String thisLine = fileReader.readLine();
    // code i've snipped out involving thisLine
    i++;
  }

I have simplified this, but you get the idea: I am using readLine() and
the doing something with that line.

The problem: sometimes, this loop seems to terminate earlier than I am
expecting, that is, before "length" number of lines have been read. When
I debug the code, and step through, this NEVER happens, and it only
sometimes happens (and not always on the same invocations) when I
actually run it.

I suspect that what is happening is that somehow the ready() method of
the BufferedReader is returning false prematurely. My questions:

1) Is it possible that the BufferedReader is somehow not able to read
the entire contents of the URL, thus terminating before I expect it to?
2) If so, why is this happening, and how can I work around this?
Gordon Beaton - 16 Aug 2006 20:11 GMT
> I suspect that what is happening is that somehow the ready() method of
> the BufferedReader is returning false prematurely. My questions:
[quoted text clipped - 3 lines]
>    expect it to?
> 2) If so, why is this happening, and how can I work around this?

Your suspicion is right: ready() returns false before you reach the
end of the file, because sometimes you read faster than the data
arrives, so there are times when no data is "ready" to be read. That
also explains why it never happens when you single step through the
code.

The solution is to avoid ready() altogether. Simply use readLine() and
it will return each line as it arrives, blocking when no data is
(temporarily) available.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Eric Sosman - 16 Aug 2006 20:13 GMT
Eric Capps wrote On 08/16/06 14:32,:
> I'm using BufferedReader with a URL to try to read information from that
> URL and do something with it. The BufferedReader is basically declared
[quoted text clipped - 24 lines]
> I suspect that what is happening is that somehow the ready() method of
> the BufferedReader is returning false prematurely.

   What do you mean by "prematurely?"  The ready() method
returns true if some input data has been received and is
sitting in a buffer somewhere just waiting to be read.  Once
you've drained that buffer, ready() will return false until
some more data arrives from the other end of the network.
Once some more data trickles in, ready() will start returning
true again.

   (This description is somewhat simplified.  The behavior
of ready() depends on the underlying Reader, and Readers of
different kinds of data sources have different ideas of "data
is waiting; come and get it.")

> My questions:
>
> 1) Is it possible that the BufferedReader is somehow not able to read
> the entire contents of the URL, thus terminating before I expect it to?

   Yes, of course.  The machine at the other end could crash
before sending all the data you expect, or the network could
get confused and route all the data to Timbuktu instead of
to you.

> 2) If so, why is this happening, and how can I work around this?

   The likely cause of your problem is the ready() call.
I'm having a hard time understanding why you wrote it; perhaps
you misunderstood what it tests.

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.