tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stewart, Daniel J" <daniel.j.stew...@boeing.com>
Subject RE: Tomcat sucks at receiving large messages
Date Thu, 02 Oct 2003 16:34:20 GMT
Since I will be occasionally receiving messages in the 10Mbyte range, I
can't read in a line at a time - it takes too long.

The bug in the code below is because BufferedReader.read() will not
necessarily return the whole buffer.  So I replace the line
  reader.read(charArr);
With this:
  int length = req.getContentLength();
  char [] charArr = new char[length];
  int readResult = 0;
  int sum = 0;
  do {
    sum += readResult;
    length -= readResult;
    readResult = reader.read(charArr, sum, length);
  } while (readResult < length);
  

Thanks for your help.  Any other critiques on the use of the standard
library are welcome.

Dan
-----Original Message-----
From: Shapira, Yoav [mailto:Yoav.Shapira@mpi.com] 
Sent: Tuesday, September 30, 2003 11:30 AM
To: Tomcat Users List
Subject: RE: Tomcat sucks at receiving large messages



Howdy,

>public void service(HttpServletRequest req, HttpServletResponse res) {
>  BufferedReader reader = req.getReader();
>  try {
>    char [] charArr = new char[req.getContentLength()];
>    reader.read(charArr);
>    String str = new String(charArr);
>
>    try {
>      File f = new File("servlet.out");
>      PrintWriter out = new PrintWriter(new FileWriter(f));
>      out.print(str);
>      out.flush();
>      out.close();
>    } catch(IOException err { System.err.println(err.toString()); }
>
>  } catch(IOException err) { System.err.println(err.toString()); } }

What happens if you ditch the req.getContentLength() approach (there are
times when it will be -1 anyways), and do something like: BufferedReader
reader = req.getReader(); StringBuffer contents = new StringBuffer();
String line = null; while((line = reader.readLine()) != null) {
  contents.append(line);
}

System.out.println(contents);

(Later we'll worry about the writing -- first make sure you're reading
the entire contents).

Yoav Shapira



This e-mail, including any attachments, is a confidential business
communication, and may contain information that is confidential,
proprietary and/or privileged.  This e-mail is intended only for the
individual(s) to whom it is addressed, and may not be saved, copied,
printed, disclosed or used by anyone else.  If you are not the(an)
intended recipient, please immediately delete this e-mail from your
computer system and notify the sender.  Thank you.


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message