Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Servlets and the fly likes PrintWriter / ResponseWriter throwing an excpetion. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "PrintWriter / ResponseWriter throwing an excpetion." Watch "PrintWriter / ResponseWriter throwing an excpetion." New topic

PrintWriter / ResponseWriter throwing an excpetion.

Ameet Parande

Joined: Sep 10, 2003
Posts: 7
This is a part of my product's code:
Object oData = oPartData.getPartContent();
if( oData instanceof String )
out.write( (String)oData );
InputStream oPartToDisplayStream = (InputStream)oData;
try {
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = != -1)
out.write( new String(buf), 0, bytesRead);
finally {
if (oPartToDisplayStream != null) oPartToDisplayStream.close();
For some mysterious reason - this code generates an exception at only 1 client's location - it works fine in all other installations.
Here is the stack trace:
java.lang.StringIndexOutOfBoundsException: String index out of range: 4096
at java.lang.String.getChars(
at sun.nio.cs.StreamEncoder.write(
at org.apache.catalina.connector.ResponseWriter.write(
at DetailMail.doShowAttachment(
at DetailMail.doGet(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardPipeline.invokeNext(
at org.apache.catalina.core.StandardPipeline.invoke(
at org.apache.catalina.core.ContainerBase.invoke(

Here "DetailMail.doShowAttachment(" is the only line that is there in the while loop.
I would be grateful if someone could tell me what is happening / point me to some links.
Thanks in advance.
Ameet Parande

Joined: Sep 10, 2003
Posts: 7
The problem is solved.
I posted a note on the same date in the I/O forums and "Jim Yingst" correctly diagnosed it as an encoding problem.
If you are interested in the details, have a look at that forum.
I agree. Here's the link:
subject: PrintWriter / ResponseWriter throwing an excpetion.
It's not a secret anymore!