code: public static byte[] toByteArray(InputStream input) throws IOException {
          ByteArrayOutputStream os = new ByteArrayOutputStream();
          byte[] buf = new byte[1024];
          for (int n = input.read(buf); n != -1; n = input.read(buf)) {
              os.write(buf, 0, n);
          }
          return os.toByteArray();
      }
docs: Get the contents of an <code>InputStream</code> as a ByteArray
  <p>
  This method buffers the input internally, so there is no need to use a
  <code>BufferedInputStream</code>.
  
  @param input
  the <code>InputStream</code> to read from
  @return the byte array
  @throws NullPointerException
  if the input is null
  @throws IOException
  if an I/O error occurs