title: Java servlet not writing response bytes
body: <p>I have a few Java servlets (3.x - Tomcat 8) running that generate and return PDF documents. I've never had any problems with any of them. I recently wrote a new servlet to also create and return a PDF document, and this new servlet is using the <strong>exact same</strong> piece of response code as the others are using:</p>
  
  <pre><code>response.reset();
  response.setContentType("application/pdf");
  response.setHeader("Content-Transfer-Encoding", "binary");
  response.setHeader("Content-Disposition","attachment; filename=\""+filename+".pdf\""); 
  response.setContentLength(pdfBytes.length);
  
  System.out.println("# Bytes =&gt; " + pdfBytes.length);
  
  ServletOutputStream sos = response.getOutputStream();
  BufferedOutputStream bos = new BufferedOutputStream(sos);
  bos.write(pdfBytes);
  sos.flush();
  sos.close();
  </code></pre>
  
  <p>As I said, this has been working fine with the others, but when I call the new servlet, it returns 0 bytes, even though the print statement above has a non-zero value.</p>
  
  <p>However, if I change the response writing code above to:</p>
  
  <pre><code>OutputStream os = response.getOutputStream();
  os.write(pdfBytes);
  os.flush();
  os.close();
  </code></pre>
  
  <p>...it works fine, returning a well-formed PDF document. Why might this be happening?</p>