code: public static String canonicalizeHost(String host) {
      // If the input contains a :, it’s an IPv6 address.
      if (host.contains(":")) {
        // If the input is encased in square braces "[...]", drop 'em.
        InetAddress inetAddress = host.startsWith("[") && host.endsWith("]")
            ? decodeIpv6(host, 1, host.length() - 1)
            : decodeIpv6(host, 0, host.length());
        if (inetAddress == null) return null;
        byte[] address = inetAddress.getAddress();
        if (address.length == 16) return inet6AddressToAscii(address);
        if (address.length == 4) return inetAddress.getHostAddress(); // An IPv4-mapped IPv6 address.
        throw new AssertionError("Invalid IPv6 address: '" + host + "'");
      }
  
      try {
        String result = IDN.toASCII(host).toLowerCase(Locale.US);
        if (result.isEmpty()) return null;
  
        // Confirm that the IDN ToASCII result doesn't contain any illegal characters.
        if (containsInvalidHostnameAsciiCodes(result)) {
          return null;
        }
        // TODO: implement all label limits.
        return result;
      } catch (IllegalArgumentException e) {
        return null;
      }
    }
docs: If {@code host} is an IP address, this returns the IP address in canonical form.
  
  <p>Otherwise this performs IDN ToASCII encoding and canonicalize the result to lowercase. For
  example this converts {@code ☃.net} to {@code xn--n3h.net}, and {@code WwW.GoOgLe.cOm} to
  {@code www.google.com}. {@code null} will be returned if the host cannot be ToASCII encoded or
  if the result contains unsupported ASCII characters.