Reputation: 419
We have recently started running our application using Jetty 9.1.0 in development. The same application is run using Tomcat 7 in production. Whenever a POST request happens we read the request body using:
private static String readRequestData(HttpServletRequest request) throws IOException
{
BufferedReader r = request.getReader();
if (request.getContentLength() > 0)
{
int len = request.getContentLength();
char[] buf = new char[len];
int read = 0;
int readTo = 0;
while (read > -1)
{
read = r.read(buf, readTo, 1000);
readTo += read;
}
return new String(buf);
}
return "";
}
Under Tomcat the BufferedReader object is a org.apache.catalina.connector.CoyoteReader and the data is read correctly. Under Jetty the reader is a org.eclipse.jetty.server.Request and this results in an java.lang.IndexOutOfBoundsException the second time through the loop. Just calling read the second time throws the exception. I can't even step into read the second time through the loop, which is confusing.
Can anyone provide guidance on what we are doing wrong?
Thank you!
Upvotes: 1
Views: 1402
Reputation: 4923
char[] buf = new char[len];
int read = 0;
int readTo = 0;
while (read > -1)
{
read = r.read(buf, readTo, 1000);
readTo += read;
}
In above code you are reading the content from 'request.getReader
'and method read
1000 characters into a specifed array buf
, started from readTo
.As the loop increases the minimum requirement of the char size increase as 1000+readTo+........
1st Iteration -> char elements are added from index 0 - 1000
2nd Iteration -> char elements are added from index 1000 - 2000
So if issue arise in 2nd iteration can check that the size of the array must be greater than 2000
Upvotes: 3