wood burning stoves 2.0*
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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 = oPartToDisplayStream.read(buf)) != -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(String.java:484)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:145)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:204)
at java.io.PrintWriter.write(PrintWriter.java:230)
at org.apache.catalina.connector.ResponseWriter.write(ResponseWriter.java:441)
at DetailMail.doShowAttachment(DetailMail.java:593)
at DetailMail.doGet(DetailMail.java:244)
at javax.servlet.http.HttpServlet.service(HttpServlet.java)
at javax.servlet.http.HttpServlet.service(HttpServlet.java)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

Here "DetailMail.doShowAttachment(DetailMail.java:593" 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.
Don't get me started about those stupid light bulbs.
subject: PrintWriter / ResponseWriter throwing an excpetion.
Similar Threads
URL download
Urgent !
Need Help With: java.io StreamCorruptedException invalid stream header
Problem with returning images from a servlet
PrintWriter / ResponseWriter throwing an excpetion.