Java: Efficiency of the readLine method of the BufferedReader and possible alternatives

Posted by Luhar on Stack Overflow See other posts from Stack Overflow or by Luhar
Published on 2010-05-07T11:44:04Z Indexed on 2010/05/07 11:48 UTC
Read the original article Hit count: 267

Filed under:
|
|

We are working to reduce the latency and increase the performance of a process written in Java that consumes data (xml strings) from a socket via the readLine() method of the BufferedReader class. The data is delimited by the end of line separater (\n), and each line can be of a variable length (6KBits - 32KBits). Our code looks like:

Socket sock = connection;
InputStream in = sock.getInputStream();
BufferedReader inputReader = new BufferedReader(new InputStreamReader(in));
...
do 
{
   String input = inputReader.readLine();
   // Executor call to parse the input thread in a seperate thread
}while(true)

So I have a couple of questions:

  • Will the inputReader.readLine() method return as soon as it hits the \n character or will it wait till the buffer is full?
  • Is there a faster of picking up data from the socket than using a BufferedReader?
  • What happens when the size of the input string is smaller than the size of the Socket's receive buffer?
  • What happens when the size of the input string is bigger than the size of the Socket's receive buffer?

I am getting to grips (slowly) with Java's IO libraries, so any pointers are much appreciated.

Thank you!

© Stack Overflow or respective owner

Related posts about java

Related posts about socket