code: private static byte[] streamToBytes(InputStream in, int length) throws IOException {
          byte[] bytes = new byte[length];
          int count;
          int pos = 0;
          while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) {
              pos += count;
          }
          if (pos != length) {
              throw new IOException("Expected " + length + " bytes, read " + pos + " bytes");
          }
          return bytes;
      }
docs: Reads the contents of an InputStream into a byte[].