title: ServletOutputStream.write throws index out of bounds sometimes
body: <p>I am having a problem which I cannot reproduce consistently. I read a file and write the contents of that file to ServletOutputStream.</p>
  
  <pre><code>                        byte[] buff = new byte[65536];
  
                          int bytesRead = dataStream.read(buff);
                          response.getOutputStream().write(buff, 0, bytesRead);
  </code></pre>
  
  <p>I noticed the some files (smaller than 20KBs) throw IndexOutofBoundsException when writing to the outputstream sometimes but at other times, everything works just find. By that I mean, no IndexOutOfBoundsException is reported.</p>
  
  <p>Here is the stack trace that I get:</p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException
          at java.lang.System.arraycopy(Native Method)
          at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:331)
          at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:392)
          at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:381)
          at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:88)
          at com.abc.web.DeliverData.serviceRequest(DeliverData.java:101)
  </code></pre>
  
  <p>Line 101 in my code is:
             response.getOutputStream().write(buff, 0, bytesRead);</p>
  
  <p>Any ideas why the problem is not reported consistently even if testing with the same files?</p>