/** * Returns the {@link InetAddress} having the given string representation. * * <p>This deliberately avoids all nameservice lookups (e.g. no DNS). * * @param ipString {@code String} containing an IPv4 or IPv6 string literal, e.g. * {@code "192.168.0.1"} or {@code "2001:db8::1"} * @return {@link InetAddress} representing the argument * @throws IllegalArgumentException if the argument is not a valid IP string literal */ public static InetAddress forString(String ipString) { byte[] addr = ipStringToBytes(ipString); // The argument was malformed, i.e. not an IP string literal. if (addr == null) { throw formatIllegalArgumentException("'%s' is not an IP string literal.", ipString); } return bytesToInetAddress(addr); }
hextets[i] = intFromBytes((byte) 0, (byte) 0, bytes[2 * i], bytes[2 * i + 1]); compressLongestRunOfZeroes(hextets); return hextetsToIPv6String(hextets);
ipString = convertDottedQuadToHex(ipString); if (ipString == null) { return null; return textToNumericFormatV6(ipString); } else if (hasDot) { return textToNumericFormatV4(ipString);
/** * Returns the IPv4 address embedded in an IPv4 compatible address. * * @param ip {@link Inet6Address} to be examined for an embedded IPv4 address * @return {@link Inet4Address} of the embedded IPv4 address * @throws IllegalArgumentException if the argument is not a valid IPv4 compatible address */ public static Inet4Address getCompatIPv4Address(Inet6Address ip) { Preconditions.checkArgument(isCompatIPv4Address(ip), "Address '%s' is not IPv4-compatible.", toAddrString(ip)); return getInet4Address(Arrays.copyOfRange(ip.getAddress(), 12, 16)); }
/** * Returns {@code true} if the supplied string is a valid IP string * literal, {@code false} otherwise. * * @param ipString {@code String} to evaluated as an IP string literal * @return {@code true} if the argument is a valid IP string literal */ public static boolean isInetAddress(String ipString) { return ipStringToBytes(ipString) != null; }
/** * Returns an {@link Inet4Address}, given a byte array representation of the IPv4 address. * * @param bytes byte array representing an IPv4 address (should be of length 4) * @return {@link Inet4Address} corresponding to the supplied byte array * @throws IllegalArgumentException if a valid {@link Inet4Address} can not be created */ private static Inet4Address getInet4Address(byte[] bytes) { Preconditions.checkArgument(bytes.length == 4, "Byte array has invalid length for an IPv4 address: %s != 4.", bytes.length); // Given a 4-byte array, this cast should always succeed. return (Inet4Address) bytesToInetAddress(bytes); }
/** * Returns {@code true} if the supplied string is a valid URI IP string * literal, {@code false} otherwise. * * @param ipString {@code String} to evaluated as an IP URI host string literal * @return {@code true} if the argument is a valid IP URI host */ public static boolean isUriInetAddress(String ipString) { try { forUriString(ipString); return true; } catch (IllegalArgumentException e) { return false; } }
/** * It will enclose an IPV6 host with [], that we need before building URIs */ public static String encloseHost(final String host) { // if the host contains a ':' then we know it's not IPv4 if (host != null && host.contains(":")) { String hostToCheck = host; /* strip off zone index since com.google.common.net.InetAddresses.isInetAddress() doesn't support it * see https://en.wikipedia.org/wiki/IPv6_address#Link-local_addresses_and_zone_indices for more info */ if (host.contains("%")) { hostToCheck = host.substring(0, host.indexOf("%")); } if (InetAddresses.isInetAddress(hostToCheck)) { return "[" + host + "]"; } } return host; }
/** * Returns the IPv4 address embedded in an IPv4 compatible address. * * @param ip {@link Inet6Address} to be examined for an embedded IPv4 address * @return {@link Inet4Address} of the embedded IPv4 address * @throws IllegalArgumentException if the argument is not a valid IPv4 compatible address */ public static Inet4Address getCompatIPv4Address(Inet6Address ip) { Preconditions.checkArgument(isCompatIPv4Address(ip), "Address '%s' is not IPv4-compatible.", toAddrString(ip)); return getInet4Address(Arrays.copyOfRange(ip.getAddress(), 12, 16)); }
/** * Returns {@code true} if the supplied string is a valid IP string * literal, {@code false} otherwise. * * @param ipString {@code String} to evaluated as an IP string literal * @return {@code true} if the argument is a valid IP string literal */ public static boolean isInetAddress(String ipString) { return ipStringToBytes(ipString) != null; }
/** * Returns an {@link Inet4Address}, given a byte array representation of the IPv4 address. * * @param bytes byte array representing an IPv4 address (should be of length 4) * @return {@link Inet4Address} corresponding to the supplied byte array * @throws IllegalArgumentException if a valid {@link Inet4Address} can not be created */ private static Inet4Address getInet4Address(byte[] bytes) { Preconditions.checkArgument(bytes.length == 4, "Byte array has invalid length for an IPv4 address: %s != 4.", bytes.length); // Given a 4-byte array, this cast should always succeed. return (Inet4Address) bytesToInetAddress(bytes); }
/** * Returns {@code true} if the supplied string is a valid URI IP string * literal, {@code false} otherwise. * * @param ipString {@code String} to evaluated as an IP URI host string literal * @return {@code true} if the argument is a valid IP URI host */ public static boolean isUriInetAddress(String ipString) { try { forUriString(ipString); return true; } catch (IllegalArgumentException e) { return false; } }
/** * It will enclose an IPV6 host with [], that we need before building URIs */ public static String encloseHost(final String host) { // if the host contains a ':' then we know it's not IPv4 if (host != null && host.contains(":")) { String hostToCheck = host; /* strip off zone index since org.apache.activemq.artemis.shaded.com.google.common.net.InetAddresses.isInetAddress() doesn't support it * see https://en.wikipedia.org/wiki/IPv6_address#Link-local_addresses_and_zone_indices for more info */ if (host.contains("%")) { hostToCheck = host.substring(0, host.indexOf("%")); } if (InetAddresses.isInetAddress(hostToCheck)) { return "[" + host + "]"; } } return host; }
byte[] addr = ipStringToBytes(ipString); if (addr == null || addr.length != expectBytes) { throw formatIllegalArgumentException("Not a valid URI IP literal: '%s'", hostAddr); return bytesToInetAddress(addr);
/** * Returns the IPv4 address embedded in an IPv4 compatible address. * * @param ip {@link Inet6Address} to be examined for an embedded IPv4 address * @return {@link Inet4Address} of the embedded IPv4 address * @throws IllegalArgumentException if the argument is not a valid IPv4 compatible address */ public static Inet4Address getCompatIPv4Address(Inet6Address ip) { Preconditions.checkArgument(isCompatIPv4Address(ip), "Address '%s' is not IPv4-compatible.", toAddrString(ip)); return getInet4Address(Arrays.copyOfRange(ip.getAddress(), 12, 16)); }
ipString = convertDottedQuadToHex(ipString); if (ipString == null) { return null; return textToNumericFormatV6(ipString); } else if (hasDot) { return textToNumericFormatV4(ipString);
hextets[i] = intFromBytes((byte) 0, (byte) 0, bytes[2 * i], bytes[2 * i + 1]); compressLongestRunOfZeroes(hextets); return hextetsToIPv6String(hextets);
/** * Returns {@code true} if the supplied string is a valid IP string * literal, {@code false} otherwise. * * @param ipString {@code String} to evaluated as an IP string literal * @return {@code true} if the argument is a valid IP string literal */ public static boolean isInetAddress(String ipString) { return ipStringToBytes(ipString) != null; }
/** * Returns an {@link Inet4Address}, given a byte array representation of the IPv4 address. * * @param bytes byte array representing an IPv4 address (should be of length 4) * @return {@link Inet4Address} corresponding to the supplied byte array * @throws IllegalArgumentException if a valid {@link Inet4Address} can not be created */ private static Inet4Address getInet4Address(byte[] bytes) { Preconditions.checkArgument(bytes.length == 4, "Byte array has invalid length for an IPv4 address: %s != 4.", bytes.length); // Given a 4-byte array, this cast should always succeed. return (Inet4Address) bytesToInetAddress(bytes); }
/** * Returns {@code true} if the supplied string is a valid URI IP string * literal, {@code false} otherwise. * * @param ipString {@code String} to evaluated as an IP URI host string literal * @return {@code true} if the argument is a valid IP URI host */ public static boolean isUriInetAddress(String ipString) { try { forUriString(ipString); return true; } catch (IllegalArgumentException e) { return false; } }