public static byte[] readBytes(InputStream stream, int length) throws IOException {
    byte[] bytes = new byte[length];

    int n = 0, off = 0;
    while (n < length) {
      int count = stream.read(bytes, off + n, length - n);
      if (count < 0) {
        throw new IOException("premature end of stream");
      }
      n += count;
    }

    return bytes;
  }
public static byte[] readBytes(InputStream stream, int length)