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 / November 2005

Tip: Looking for answers? Try searching our database.

BufferedReader.readline() behavior with Sockets

Thread view: 
oziris - 29 Nov 2005 08:31 GMT
hi !

Here the piece of code that creates my BufferedReader object

--- code ---
Socket socket = new Socket(....);
BufferedReader inSocket =
   new BufferedReader(
       new InputStreamReader(
           m_socketService.getInputStream()));
--- /code ---

My request concerns the method readline() of this object. According to
what I understood of my readings, I expect from this method it blocks
itself until data comes to the socket.

Is that correct? Because in my implementation, this method returns
always null if there is no data to read. So I have to write this

--- code ---
String requete;
if ((requete = inSocket.readLine()) != null)
{
   // do something
}
--- /code ---

And that sounds bad to me. It's like an infinite loop...

Thanks a lot for your precious advices.

-o--
NullBock - 29 Nov 2005 09:07 GMT
Calling InputStream#read() (which the BufferedReader ultimately does)
blocks until there is data to be had, *or* the stream closes.  So your
code is proper, and isn't an infinite loop.  I'd write it so:

BufferedReader r;
//...
String s;
while ((s = r.readLine()) != null) {
 //do something
}

This is not only acceptable, but necessary.  Once the stream closes,
BufferedReader returns null. That is, unless the stream doesn't close
cleanly, either because it becomes corrupted or interrupted.  In that
case, readLine() simply throws an exception.

Walter Gildersleeve
Freiburg, Germany

______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
Roedy Green - 29 Nov 2005 11:02 GMT
>My request concerns the method readline() of this object. According to
>what I understood of my readings, I expect from this method it blocks
>itself until data comes to the socket.

see http://mindprod.com/jgloss/readblocking.html
http://mindprod.com/jgloss/readeverything.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



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



©2009 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.