android/guava/src/com/google/common/net/InetAddresses.java
public static String toUriString(InetAddress ip) {
    if (ip instanceof Inet6Address) {
      return "[" + toAddrString(ip) + "]";
    }
    return toAddrString(ip);
  }
public static String toUriString(InetAddress ip) {
    if (ip instanceof Inet6Address) {
      return "[" + toAddrString(ip) + "]";
    }
    return toAddrString(ip);
  }
Returns the string representation of an {@link InetAddress} suitable for inclusion in a URI.

<p>For IPv4 addresses, this is identical to {@link InetAddress#getHostAddress()}, but for IPv6
addresses it compresses zeroes and surrounds the text with square brackets; for example {@code
"[2001:db8::1]"}.

<p>Per section 3.2.2 of <a target="_parent"
href="http://tools.ietf.org/html/rfc3986#section-3.2.2">RFC 3986</a>, a URI containing an IPv6
string literal is of the form {@code "http://[2001:db8::1]:8888/index.html"}.

<p>Use of either {@link InetAddresses#toAddrString}, {@link InetAddress#getHostAddress()}, or
this method is recommended over {@link InetAddress#toString()} when an IP address string
literal is desired. This is because {@link InetAddress#toString()} prints the hostname and the
IP address string joined by a "/".

@param ip {@link InetAddress} to be converted to URI string literal
@return {@code String} containing URI-safe string literal