title: Strange byte[] behavior reading from a URL
body: <p><strong>In the end, my <em>ultimate</em> goals are:</strong></p>
  
  <ul>
  <li><strong>Read from a URL (what this question is about)</strong></li>
  <li>Save the retrieved [PDF] content to a BLOB field in a DB (already have that nailed down)</li>
  <li>Read from the BLOB field and attach that content to an email</li>
  <li>All without going to a filesystem</li>
  </ul>
  
  <p>The goal with the following method is to get a <code>byte[]</code> that can be used downstream as an email attachment (to avoid writing to disk):</p>
  
  <pre class="lang-java prettyprint-override"><code>public byte[] retrievePDF() {
  
           HttpClient httpClient = new HttpClient();
  
           GetMethod httpGet = new GetMethod("http://website/document.pdf");
           httpClient.executeMethod(httpGet);
           InputStream is = httpGet.getResponseBodyAsStream();
  
           byte[] byteArray = new byte[(int) httpGet.getResponseContentLength()];
  
           is.read(byteArray, 0, byteArray.length);
  
          return byteArray;
  }
  </code></pre>
  
  <p>For a particular PDF, the <code>getResponseContentLength()</code> method returns 101,689 as the length.  The <em>strange</em> part is that if I set a break-point and interrogate the <code>byteArray</code> variable, it has 101,689 byte elements, however, after byte #3744 the remaining bytes of the array are all zeroes (<code>0</code>).  <em>The resulting PDF is then not readable by a PDF-reader client, like Adobe Reader.</em></p>
  
  <p><strong>Why would that happen?</strong></p>
  
  <p>Retrieving this same PDF via browser and saving to disk, or using a method like the following (which I patterned after an <a href="https://stackoverflow.com/a/921400/425507">answer to this StackOverflow post</a>), results in a readable PDF:</p>
  
  <pre class="lang-java prettyprint-override"><code>public void retrievePDF() {
      FileOutputStream fos = null;
      URL url;
      ReadableByteChannel rbc = null;
  
      url = new URL("http://website/document.pdf");
  
      DataSource urlDataSource = new URLDataSource(url);
  
      /* Open a connection, then set appropriate time-out values */
      URLConnection conn = url.openConnection();
      conn.setConnectTimeout(120000);
      conn.setReadTimeout(120000);
  
      rbc = Channels.newChannel(conn.getInputStream());
  
      String filePath = "C:\\temp\\";
      String fileName = "testing1234.pdf";
      String tempFileName = filePath + fileName;
  
      fos = new FileOutputStream(tempFileName);
      fos.getChannel().transferFrom(rbc, 0, 1 &lt;&lt; 24);
      fos.flush();
  
      /* Clean-up everything */
      fos.close();
      rbc.close();
  }
  </code></pre>
  
  <p>For both approaches, the size of the resulting PDF is 101,689-bytes when doing a <em>Right-click > Properties...</em> in Windows.</p>
  
  <p><strong>Why would the byte array essentially "stop" part-way through?</strong></p>