code: public static byte[] readAllToBytes(final InputStream in) throws IOException {
      try {
        final ByteArrayOutputStream result = new ByteArrayOutputStream();
        final byte[] buffer = new byte[1024];
        int length;
        while ((length = in.read(buffer)) != -1) {
          result.write(buffer, 0, length);
        }
        return result.toByteArray();
      } finally {
        in.close();
      }
    }
docs: Reads the entire {@link InputStream} to EOF and returns the data as bytes.
  
  @param in the stream to read.
  @return the read data as bytes.
  @throws IOException in the event the data cannot be read.