Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BufferedReader, readLine() method, and the issue

 
Zaeed McColin
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm really sorry for this post, but I still couldn't solve my problem.
well as API says, BufferedReader readLine() method returns null if end of the stream (EOS) reached. but it waits, here is my code.

the above code listens on port x and reads the Incoming stream from the client(Browser e.g. FF, IE) and parse it (very simple http server).
generally I'm not wondered using BufferedReader, any solution that help me out to solve this problem, I've tried to use InputStreamReader and InputStream classes but I could not.

my config: winxp sp2(sorry),tested in both JDK1.6.0.20 and Apache harmony 6M2 (same result).
 
Andrey Kozhanov
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you are reading from Socket, and Socket input stream could be considered as "endless" stream. So readLine will never read null, so to reach line 14 of your code you should use some sort of flag indicating end of the stream.
 
Zaeed McColin
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrey Kozhanov.
well because it's a http server I don't have any hand to handle requests from clients, generally clients are browsers, so as you mentioned, how can I determine that browser(client) stream is completed and getting into the line 14? any solution? is there any standard belong browsers to notify servers about complete incoming stream?
 
Joe Ess
Bartender
Posts: 9298
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arash M. Dehghani wrote: is there any standard belong browsers to notify servers about complete incoming stream?


Yes, there is. It is the Hypertext Transfer Protocol (HTTP). If you are writing a server, you will want to become very familiar with it.
To answer your question directly, I believe an HTTP request ends with two carriage returns, but I wouldn't bet my life on it.
 
Zaeed McColin
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Joe, thanks for helping, well I read the HTTP rfc and it says
The Request-Line begins with a method token, followed by the Request-URI and the protocol version, and ending with CRLF. The elements are separated by SP characters. No CR or LF is allowed except in the final CRLF sequence.

well the CRLF is known as "\r\n" and when I tried to determine the end of the stream with this (Just in GET method)

it has the same result, waiting....
but I tried it like this

it seems work fine (but I cannot bet on it) with get method, when for methods which has body it requires some condition of course.
so the new question is, according to http rfc and spec, how to determine it? I mean CRLF.
 
Joe Ess
Bartender
Posts: 9298
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be interested in the JavaDoc for BufferedReader:


public String readLine() throws IOException

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached


 
Zaeed McColin
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have to swap my eyes
many thanks Joe, I've just got a big help, as you said and according to API the following code may help. does it? I think.

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic