United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4107821 : BufferedReader.readLine(int maxlen) to limit maximum number of read characters

Details
Type:
Enhancement
Submit Date:
1998-01-29
Status:
Closed
Updated Date:
2006-02-01
Project Name:
JDK
Resolved Date:
2006-02-01
Component:
core-libs
OS:
generic
Sub-Component:
java.io
CPU:
generic
Priority:
P5
Resolution:
Won't Fix
Affected Versions:
1.1.5
Fixed Versions:

Related Reports

Sub Tasks

Description
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)
======================================================================

                                    

Comments
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));
}
======================================================================
                                     
2004-09-08
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".
                                     
2006-02-01



Hardware and Software, Engineered to Work Together