docs: private static String readFromConnection(URLConnection connection) throws IOException {
      InputStream stream;
      if (connection instanceof HttpURLConnection) {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        try {
          stream = connection.getInputStream();
        } catch (Exception e) {
          stream = httpConnection.getErrorStream();
        }
      } else {
        stream = connection.getInputStream();
      }
      try {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int read = 0;
        while ((read = stream.read(buffer)) != -1) {
          output.write(buffer, 0, read);
        }
        String charset = connection.getContentEncoding();
        if (charset == null) {
          String mimeType = connection.getContentType();
          String[] parts = mimeType.split(";");
          for (String part : parts) {
            part = part.trim();
            if (part.startsWith("charset=")) {
              charset = part.substring("charset=".length());
              break;
            }
          }
          if (charset == null) {
            charset = "UTF-8";
          }
        }
        return new String(output.toByteArray(), charset);
      } finally {
        stream.close();
      }
    }