/** * Returns the {@link String} representation of an {@link InetAddress}. * <ul> * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li> * <li>Inet6Address results adhere to * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li> * </ul> * <p> * The output does not include Scope ID. * @param ip {@link InetAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toAddressString(InetAddress ip) { return toAddressString(ip, false); }
/** * Returns the {@link String} representation of an {@link InetAddress}. * <ul> * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li> * <li>Inet6Address results adhere to * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li> * </ul> * <p> * The output does not include Scope ID. * @param ip {@link InetAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toAddressString(InetAddress ip) { return toAddressString(ip, false); }
/** * Returns the {@link String} representation of an {@link InetAddress}. * <ul> * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li> * <li>Inet6Address results adhere to * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li> * </ul> * <p> * The output does not include Scope ID. * @param ip {@link InetAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toAddressString(InetAddress ip) { return toAddressString(ip, false); }
return toAddressString(ip.getAddress(), 0, ipv4Mapped);
/** * Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively. * * @throws IllegalArgumentException * if {@code length} is not {@code 4} nor {@code 16} */ public static String bytesToIpAddress(byte[] bytes, int offset, int length) { switch (length) { case 4: { return new StringBuilder(15) .append(bytes[offset] & 0xff) .append('.') .append(bytes[offset + 1] & 0xff) .append('.') .append(bytes[offset + 2] & 0xff) .append('.') .append(bytes[offset + 3] & 0xff).toString(); } case 16: return toAddressString(bytes, offset, false); default: throw new IllegalArgumentException("length: " + length + " (expected: 4 or 16)"); } }
return toAddressString(ip.getAddress(), 0, ipv4Mapped);
/** * Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively. * * @throws IllegalArgumentException * if {@code length} is not {@code 4} nor {@code 16} */ public static String bytesToIpAddress(byte[] bytes, int offset, int length) { switch (length) { case 4: { return new StringBuilder(15) .append(bytes[offset] & 0xff) .append('.') .append(bytes[offset + 1] & 0xff) .append('.') .append(bytes[offset + 2] & 0xff) .append('.') .append(bytes[offset + 3] & 0xff).toString(); } case 16: return toAddressString(bytes, offset, false); default: throw new IllegalArgumentException("length: " + length + " (expected: 4 or 16)"); } }
/** * Returns the {@link String} representation of an {@link InetSocketAddress}. * <p> * The output does not include Scope ID. * @param addr {@link InetSocketAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toSocketAddressString(InetSocketAddress addr) { String port = String.valueOf(addr.getPort()); final StringBuilder sb; if (addr.isUnresolved()) { String hostname = getHostname(addr); sb = newSocketAddressStringBuilder(hostname, port, !isValidIpV6Address(hostname)); } else { InetAddress address = addr.getAddress(); String hostString = toAddressString(address); sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address); } return sb.append(':').append(port).toString(); }
return toAddressString(ip.getAddress(), 0, ipv4Mapped);
/** * Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively. * * @throws IllegalArgumentException * if {@code length} is not {@code 4} nor {@code 16} */ public static String bytesToIpAddress(byte[] bytes, int offset, int length) { switch (length) { case 4: { return new StringBuilder(15) .append(bytes[offset] & 0xff) .append('.') .append(bytes[offset + 1] & 0xff) .append('.') .append(bytes[offset + 2] & 0xff) .append('.') .append(bytes[offset + 3] & 0xff).toString(); } case 16: return toAddressString(bytes, offset, false); default: throw new IllegalArgumentException("length: " + length + " (expected: 4 or 16)"); } }
/** * Returns the {@link String} representation of an {@link InetSocketAddress}. * <p> * The output does not include Scope ID. * @param addr {@link InetSocketAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toSocketAddressString(InetSocketAddress addr) { String port = String.valueOf(addr.getPort()); final StringBuilder sb; if (addr.isUnresolved()) { String hostname = getHostname(addr); sb = newSocketAddressStringBuilder(hostname, port, !isValidIpV6Address(hostname)); } else { InetAddress address = addr.getAddress(); String hostString = toAddressString(address); sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address); } return sb.append(':').append(port).toString(); }
/** * 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 */ 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; } }
/** * Returns the {@link String} representation of an {@link InetSocketAddress}. * <p> * The output does not include Scope ID. * @param addr {@link InetSocketAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toSocketAddressString(InetSocketAddress addr) { String port = String.valueOf(addr.getPort()); final StringBuilder sb; if (addr.isUnresolved()) { String hostname = getHostname(addr); sb = newSocketAddressStringBuilder(hostname, port, !isValidIpV6Address(hostname)); } else { InetAddress address = addr.getAddress(); String hostString = toAddressString(address); sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address); } return sb.append(':').append(port).toString(); }
/** * Returns the {@link String} representation of an {@link InetAddress}. * <ul> * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li> * <li>Inet6Address results adhere to * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li> * </ul> * <p> * The output does not include Scope ID. * @param ip {@link InetAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toAddressString(InetAddress ip) { return toAddressString(ip, false); }
/** * Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively. * * @throws IllegalArgumentException * if {@code length} is not {@code 4} nor {@code 16} */ public static String bytesToIpAddress(byte[] bytes, int offset, int length) { switch (length) { case 4: { return new StringBuilder(15) .append(bytes[offset] & 0xff) .append('.') .append(bytes[offset + 1] & 0xff) .append('.') .append(bytes[offset + 2] & 0xff) .append('.') .append(bytes[offset + 3] & 0xff).toString(); } case 16: return toAddressString(bytes, offset, false); default: throw new IllegalArgumentException("length: " + length + " (expected: 4 or 16)"); } }
return toAddressString(ip.getAddress(), 0, ipv4Mapped);
/** * Returns the {@link String} representation of an {@link InetSocketAddress}. * <p> * The output does not include Scope ID. * @param addr {@link InetSocketAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toSocketAddressString(InetSocketAddress addr) { String port = String.valueOf(addr.getPort()); final StringBuilder sb; if (addr.isUnresolved()) { String hostname = getHostname(addr); sb = newSocketAddressStringBuilder(hostname, port, !isValidIpV6Address(hostname)); } else { InetAddress address = addr.getAddress(); String hostString = toAddressString(address); sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address); } return sb.append(':').append(port).toString(); }
/** * Returns the {@link String} representation of an {@link InetAddress}. * <ul> * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li> * <li>Inet6Address results adhere to * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li> * </ul> * <p> * The output does not include Scope ID. * @param ip {@link InetAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toAddressString(InetAddress ip) { return toAddressString(ip, false); }
/** * Returns the {@link String} representation of an {@link InetAddress}. * <ul> * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li> * <li>Inet6Address results adhere to * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li> * </ul> * <p> * The output does not include Scope ID. * @param ip {@link InetAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toAddressString(InetAddress ip) { return toAddressString(ip, false); }
/** * Returns the {@link String} representation of an {@link InetAddress}. * <ul> * <li>Inet4Address results are identical to {@link InetAddress#getHostAddress()}</li> * <li>Inet6Address results adhere to * <a href="http://tools.ietf.org/html/rfc5952#section-4">rfc 5952 section 4</a></li> * </ul> * <p> * The output does not include Scope ID. * @param ip {@link InetAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address */ public static String toAddressString(InetAddress ip) { return toAddressString(ip, false); }