Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum line length for BufferedReader.readLine() in Java?

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.

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.

Is there a way to avoid that? Or do I have to implement a bounded version of readLine() myself?

like image 575
daphshez Avatar asked May 11 '11 07:05

daphshez


People also ask

What is the default buffer size for BufferedReader in java?

As per this java documentation, default buffer size is 8192 characters capacity. Line size is considered as 80 chars capacity. 8192 buffer size is sufficient for smaller file sizes.

How do I find BufferedReader size?

BufferedReader Buffer SizeYou provide the size as a constructor parameter, like this: int bufferSize = 8 * 1024; BufferedReader bufferedReader = new BufferedReader( new FileReader("c:\\data\\input-file. txt"), bufferSize ); This example sets the internal buffer to 8 KB.

Does BufferedReader read line by line?

Java Read File line by line using BufferedReaderWe can use java. io. BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached.

What is readLine in BufferedReader?

The readLine() method of BufferedReader class in Java is used to read one line text at a time. The end of a line is to be understood by '\n' or '\r' or EOF. Syntax: public String readLine() throws IOException. Parameters: This method does not accept any parameter.


2 Answers

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.

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.

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.

like image 82
Stephen C Avatar answered Oct 10 '22 02:10

Stephen C


Another option is Apache Commons' BoundedInputStream:

InputStream bounded = new BoundedInputStream(is, MAX_BYTE_COUNT);
BufferedReader reader = new BufferedReader(new InputStreamReader(bounded));
String line = reader.readLine();
like image 41
Kevin Litwack Avatar answered Oct 10 '22 03:10

Kevin Litwack