code: public static String formatHostnameForHttp(InetSocketAddress addr) {
          String hostString = NetUtil.getHostname(addr);
          if (NetUtil.isValidIpV6Address(hostString)) {
              if (!addr.isUnresolved()) {
                  hostString = NetUtil.toAddressString(addr.getAddress());
              }
              return '[' + hostString + ']';
          }
          return hostString;
      }
docs: Formats the host string of an address so it can be used for computing an HTTP component
  such as an URL or a Host header
  
  @param addr the address
  @return the formatted String