title: Most efficient way to convert InputStream into byte[]?
body: <pre><code>ChannelBufferInputStream responseStream = (ChannelBufferInputStream) response.getBodyAsStream();
  ArrayList&lt;Byte&gt; arrayList = new ArrayList&lt;Byte&gt;();
  try {
      while (responseStream.available() &gt; 0) {
          arrayList.add(responseStream.readByte());
      }
  } catch (IOException e) {
      e.printStackTrace();
      return internalServerError();
  }
  Iterator&lt;Byte&gt; iterator = arrayList.iterator();
  byte[] bytes = new byte[arrayList.size()];
  int i = 0;
  while (iterator.hasNext()) {
      bytes[i++] = iterator.next();
  }
  </code></pre>
  
  <p>This code is called on every page load of my web app. It seems to be running pretty fast, but is there anything that could make this run faster?</p>
  
  <p>Edit - Updated using byte array output stream</p>
  
  <pre><code>ChannelBufferInputStream responseStream = (ChannelBufferInputStream) response.getBodyAsStream();
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  try {
      int read = responseStream.read();
      while (read != -1) {
          byteArrayOutputStream.write(read);
          read = responseStream.read();
      }
  } catch (IOException e) {
      e.printStackTrace();
      return internalServerError();
  }
  byte[] bytes = byteArrayOutputStream.toByteArray();
  return ok(bytes).as(response.getHeader("Content-type"));
  </code></pre>
  
  <p>Edit - Benchmark test code</p>
  
  <pre><code>ChannelBufferInputStream responseStream = (ChannelBufferInputStream) response.getBodyAsStream();
  long t1 = System.nanoTime();
  
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  try {
      int read = responseStream.read();
      while (read != -1) {
          byteArrayOutputStream.write(read);
          read = responseStream.read();
      }
  } catch (IOException e) {
      e.printStackTrace();
      return internalServerError();
  }
  byte[] bytes = byteArrayOutputStream.toByteArray();
  
  long t2 = System.nanoTime();
  System.out.println(t2-t1);
  return ok(bytes).as(response.getHeader("Content-type"));
  </code></pre>
  
  <p>Average Time after 100+ request - 46873</p>
  
  <pre><code>ChannelBufferInputStream responseStream = (ChannelBufferInputStream) response.getBodyAsStream();
  long t1 = System.nanoTime();
  
  ArrayList&lt;Byte&gt; arrayList = new ArrayList&lt;Byte&gt;();
  try {
      while (responseStream.available() &gt; 0) {
          arrayList.add(responseStream.readByte());
      }
  } catch (IOException e) {
      e.printStackTrace();
      return internalServerError();
  }
  Iterator&lt;Byte&gt; iterator = arrayList.iterator();
  byte[] bytes = new byte[arrayList.size()];
  int i = 0;
  while (iterator.hasNext()) {
      bytes[i++] = iterator.next();
  }
  
  long t2 = System.nanoTime();
  System.out.println(t2-t1);
  return ok(bytes).as(response.getHeader("Content-type"));
  </code></pre>
  
  <p>Average Time after 100+ request - 522848</p>
  
  <pre><code>long t1 = System.nanoTime();
  byte[] bytes;
  try {
      bytes = org.apache.commons.io.IOUtils.toByteArray(responseStream);
  } catch (Exception e) {
      return internalServerError();
  }
  
  long t2 = System.nanoTime();
  System.out.println(t2-t1);
  </code></pre>
  
  <p>Average Time after 100+ request - 45088</p>
  
  <pre><code>long t1 = System.nanoTime();
  byte[] bytes;
  try {
      bytes = sun.misc.IOUtils.readFully(responseStream, -1, true);
  } catch (Exception e) {
      return internalServerError();
  }
  
  long t2 = System.nanoTime();
  System.out.println(t2 - t1);
  </code></pre>
  
  <p>Average Time after 100+ request - 20180</p>