title: Having ClientAbortException Not able to understand the fix
body: <p>I am having this exception as follows :</p>
  
  <pre><code>Exception in writing file. null ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error
   at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:369)
   at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:448)
   at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:363)
   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:89)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at com.gtnexus.page.servlet.HttpCachePolicyFilter.doFilter(HttpCachePolicyFilter.java:173)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:107)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
   at java.lang.Thread.run(Thread.java:745) Caused by: java.net.SocketException: Connection reset by peer: socket write error
   at java.net.SocketOutputStream.socketWrite0(Native Method)
   at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
   at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
   at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:761)
   at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:448)
   at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:363)
   at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:785)
   at org.apache.coyote.http11.filters.IdentityOutputFilter.doWrite(IdentityOutputFilter.java:127)
   at org.apache.coyote.http11.InternalOutputBuffer.doWrite(InternalOutputBuffer.java:598)
   at org.apache.coyote.Response.doWrite(Response.java:533)
   at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:364) ... 75 more
  </code></pre>
  
  <p>it happens on following code :</p>
  
  <pre><code>private void returnFile(File file, OutputStream out) {
          FileInputStream fis = null;
          try {
              fis = new FileInputStream(file);
              byte[] buf = new byte[4 * 1024]; 
              int bytesRead;
              while ((bytesRead = fis.read(buf)) != -1) {
                  out.write(buf, 0, bytesRead);
              }
              out.flush();
          } catch (Exception e) {
              log.error("Exception in writing file. " + e.getMessage(), e);
          } finally {
              CloseUtils.close(fis, out);
          }
      }
  </code></pre>
  
  <p>OutputStream refers to ServletOutputStream where an excel is rendered, Strangely it's fix is very weird which i am not able to understand, here's the fix. </p>
  
  <pre><code>private void returnFile(File file, OutputStream out) {
          FileInputStream fis = null;
          try {
              fis = new FileInputStream(file);
              int bytesRead;
              byte[] buf = new byte[(int) file.length()];
              while ((bytesRead = fis.read(buf)) != -1) {
                  out.write(buf, 0, bytesRead);
              }
              out.flush();
          } catch (Exception e) {
              log.error("Exception in writing file. " + e.getMessage(), e);
          } finally {
              CloseUtils.close(fis, out);
          }
      }
  </code></pre>
  
  <p>You can see that the only difference is the use of 4kb buffer in case of the exception, fix having buffer size with the size of total file length itself. </p>
  
  <p>Now in the case of big files using a buffer is valid, My question is, what is going on? I don't understand the cause as well as the fix of this issue which is given by me only but I don't get it how it works and why. </p>
  
  <p>Can anyone please explain this so i have better justification such fix?</p>