Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter / ResponseWriter throwing an excpetion.  RSS feed

 
Ameet Parande
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a part of my product's code:
Object oData = oPartData.getPartContent();
if( oData instanceof String )
{
out.write( (String)oData );
}
else
{
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.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define what you mean by only 1 client throws this exception. If all others work, I would say that the problem is with that client, not with your code. Perhaps they are trying to send a String that is larger than the rest of the clients, and hence not fitting within your array (just wildly guessing here).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, it's hard to say really, but I'm a bit suspicious of this line:
out.write( new String(buf), 0, bytesRead);
I see two problems:
  • You're making a String of the [i]entire[/] buffer, not just the first [bytesRead] bytes of it. The later data should not be part of the String.
  • You're making a String from bytes without declaring the encoding to be used. This means that you're using the default character encoding on whatever machine you're running on. My bet is that one of the client machines has a different default encoding from the others, and it's a multi-byte encoding. This means that n bytes may convert to a String that has less than n chars in it, which means that when you try to write n chars, you get StringIndexOutOfBoundsException.


  • The latter problem should definitely fixed, like so:

    And the former problem may be fixed if all machines need to use the same encoding. I.e. if you're passing bytes from one machine to another, they need to agree on encoding; if a machine is just reading and writing bytes to its own files, it can just use its own default encoding (as long as no one else tries to read/write those bytes.) You can declare a particular encoding easily:

    where "UTF-8" can be replaced with whatever encoding you decide is appropriate.
     
    Ameet Parande
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jim,
    You hit the hammer right on the head of the nail- it was an enoding problem.
    That instillation was done on RedHad 8.0 that has a different default encoding scheme. Other installations of the s/w were on RedHat 7.1, 7.2 and 7.3 and hence were not creating a problem.
    Thanks for the support
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!