JDK-4107821 : BufferedReader.readLine(int maxlen) to limit maximum number of read characters
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1.5
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 1998-01-29
  • Updated: 2006-02-01
  • Resolved: 2006-02-01
Name: tb29552			Date: 01/29/98

Requested by:
(company - Mobile Telephone Networks , email - ###@###.###)

This would allow a programmer to read a line of text
not more than a certain number of characters.

Eg. reading a socket that does text IO (like SMTP)
one wants to read a line but not more than a certain
number of characters.

To use readLine() as is would be convinient but opens
such a socket reader to denial of service attacks
when a client spams volumes of text to the socket
with no new line characters.  The BufferedReader
will read all of the text and eventually run out
of memory.

(Review ID: 24018)

EVALUATION This bug was submitted over 8 years ago. Without any compelling use case or interest, there is no strong motivation to add an api which can be implemented in a few lines of code by the small set of users who require this funtionality. Closing this request as "will not fix".

WORK AROUND Name: tb29552 Date: 01/29/98 public String readLine(int len) { int currentPos=0; char[] data=new char[len] char newLineChar=System.getproperty("line.separator"); char currentChar=read(); while((currentChar != newLineChar) && (currentPos<len)) { data[currentPos++]=currentChar; currentChar=read(); } if(currentChar==newLineChar) return(new String(data,0,currentPos-1)); else return(new String(data,0,currentPos)); } ======================================================================