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


i use bufferedreader's readline() method read lines of text socket.

there no obvious way limit length of line read.

i worried source of data can (maliciously or mistake) write lot of data without line feed character, , cause bufferedreader allocate unbounded amount of memory.

is there way avoid that? or have implement bounded version of readline() myself?

the simplest way implement own bounded line reader.

or simpler, reuse code this boundedbufferedreader class.

actually, coding readline() works same standard method not trivial. dealing 3 kinds of line terminator correctly requires pretty careful coding. interesting compare different approaches of above link sun version , apache harmony version of bufferedreader.

note: i'm not entirely convinced either bounded version or apache version 100% correct. bounded version assumes underlying stream supports mark , reset, not true. apache version appears read-ahead 1 character if sees cr last character in buffer. break on macos when reading input typed user. sun version handles setting flag cause possible lf after cr skipped on next read... operation; i.e. no spurious read-ahead.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -