aspose file tools*
The moose likes I/O and Streams and the fly likes BufferedReader, readLine() method, and the issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "BufferedReader, readLine() method, and the issue" Watch "BufferedReader, readLine() method, and the issue" New topic
Author

BufferedReader, readLine() method, and the issue

Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
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).


Open source
Andrey Kozhanov
Ranch Hand

Joined: Mar 12, 2010
Posts: 79
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

Joined: Jan 13, 2009
Posts: 90
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

Joined: Oct 29, 2001
Posts: 8911
    
    8

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
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

Joined: Oct 29, 2001
Posts: 8911
    
    8

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

Joined: Jan 13, 2009
Posts: 90
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.
 
jQuery in Action, 2nd edition
 
subject: BufferedReader, readLine() method, and the issue