java read性能,Java:BufferedReader的readLine方法的效率和可能的替代方案

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!

解决方案

Will the inputReader.readLine() method return as soon as it hits the \n character or will it wait till the buffer is full?

It will return as soon as it gets a newline.

Is there a faster of picking up data from the socket than using a BufferedReader?

BufferedReader entails some copying of the data. You could try the NIO apis, which can avoid copying, but you might want to profile before spending any time on this to see if it really is the I/O that is the bottleneck. A simpler quick fix is to add a BufferedInputStream around the socket, so that each read is not hitting the socket (It's not clear if InputStreamReader does any buffering itself.) e.g.

new BufferedReader(new InputStreamReader(new BufferedInputStream(in)))

What happens when the size of the input string is smaller than the size of the Socket's receive buffer?

The BufferedReader will fetch all the data availalbe. It will then scan this data to look for the newline. The result is that subsequent reads may already have the data in the BufferedReader.

What happens when the size of the input string is bigger than the size of the Socket's receive buffer?

The bufferedReader will read what is in the recieve buffer, and as there is no newline or the end of stream is reached, it will continue to read data from the socket until it finds EOF or a newline. Subsequent reads may block until more data becomes available.

To sum up, BufferedReader blocks only when absolutely necessary.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值