问题描述
我使用BufferedReader的 readLine()
方法从套接字读取文本行。
I use BufferedReader's readLine()
method to read lines of text from a socket.
没有明显的方法来限制读取行的长度。
There is no obvious way to limit the length of the line read.
我担心数据源可能(恶意地或错误地)写入大量数据而没有任何换行符,这会导致BufferedReader分配一个无限量的记忆。
I am worried that the source of the data can (maliciously or by mistake) write a lot of data without any line feed character, and this will cause BufferedReader to allocate an unbounded amount of memory.
有没有办法避免这种情况?或者我是否必须自己实现 readLine()
的有界版本?
Is there a way to avoid that? Or do I have to implement a bounded version of readLine()
myself?
推荐答案
最简单的方法是实现自己的有界线读取器。
The simplest way to do this will be to implement your own bounded line reader.
甚至更简单,重用。
Or even simpler, reuse the code from this BoundedBufferedReader
class.
实际上,编码 readLine()
与标准相同方法并非无足轻重。处理3种线路终结器正确地需要一些非常仔细的编码。有趣的是将上述链接的不同方法与和 Apache Harmony版本 BufferedReader。
Actually, coding a readLine()
that works the same as the standard method is not trivial. Dealing with the 3 kinds of line terminator CORRECTLY requires some pretty careful coding. It is interesting to compare the different approaches of the above link with the Sun version and Apache Harmony version of BufferedReader.
注意:我并不完全相信有界版本或Apache版本是100%正确的。有界版本假设底层流支持标记和重置,这当然不总是正确的。如果将Apache视为缓冲区中的最后一个字符,则Apache版本似乎预读一个字符。当读取用户键入的输入时,这将在MacOS上中断。 Sun版本通过在下一个 read ...
操作中跳过CR后设置一个标志来引发可能的LF来处理这个问题。即没有虚假的预读。
Note: I'm not entirely convinced that either the bounded version or the Apache version is 100% correct. The bounded version assumes that the underlying stream supports mark and reset, which is certainly not always true. The Apache version appears to read-ahead one character if it sees a CR as the last character in the buffer. This would break on MacOS when reading input typed by the user. The Sun version handles this by setting a flag to cause the possible LF after the CR to be skipped on the next read...
operation; i.e. no spurious read-ahead.
这篇关于Java中BufferedReader.readLine()的最大行长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!