Class InetAddresses
- java.lang.Object
-
- org.elasticsearch.common.network.InetAddresses
-
public class InetAddresses extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description InetAddresses()
-
Method Summary
Modifier and Type Method Description static java.net.InetAddress
forString(java.lang.String ipString)
Returns theInetAddress
having the given string representation.static boolean
isInetAddress(java.lang.String ipString)
static Tuple<java.net.InetAddress,java.lang.Integer>
parseCidr(java.lang.String maskedAddress)
Parse an IP address and its prefix length using the CIDR notation.static java.lang.String
toAddrString(java.net.InetAddress ip)
Returns the string representation of anInetAddress
.static java.lang.String
toUriString(java.net.InetAddress ip)
Returns the string representation of anInetAddress
suitable for inclusion in a URI.
-
-
-
Method Detail
-
isInetAddress
public static boolean isInetAddress(java.lang.String ipString)
-
toUriString
public static java.lang.String toUriString(java.net.InetAddress ip)
Returns the string representation of anInetAddress
suitable for inclusion in a URI.For IPv4 addresses, this is identical to
InetAddress.getHostAddress()
, but for IPv6 addresses it compresses zeroes and surrounds the text with square brackets; for example"[2001:db8::1]"
.Per section 3.2.2 of http://tools.ietf.org/html/rfc3986, a URI containing an IPv6 string literal is of the form
"http://[2001:db8::1]:8888/index.html"
.Use of either
toAddrString(java.net.InetAddress)
,InetAddress.getHostAddress()
, or this method is recommended overInetAddress.toString()
when an IP address string literal is desired. This is becauseInetAddress.toString()
prints the hostname and the IP address string joined by a "/".- Parameters:
ip
-InetAddress
to be converted to URI string literal- Returns:
String
containing URI-safe string literal
-
toAddrString
public static java.lang.String toAddrString(java.net.InetAddress ip)
Returns the string representation of anInetAddress
.For IPv4 addresses, this is identical to
InetAddress.getHostAddress()
, but for IPv6 addresses, the output follows RFC 5952 section 4. The main difference is that this method uses "::" for zero compression, while Java's version uses the uncompressed form.This method uses hexadecimal for all IPv6 addresses, including IPv4-mapped IPv6 addresses such as "::c000:201". The output does not include a Scope ID.
- Parameters:
ip
-InetAddress
to be converted to an address string- Returns:
String
containing the text-formatted IP address- Since:
- 10.0
-
forString
public static java.net.InetAddress forString(java.lang.String ipString)
Returns theInetAddress
having the given string representation.This deliberately avoids all nameservice lookups (e.g. no DNS).
- Parameters:
ipString
-String
containing an IPv4 or IPv6 string literal, e.g."192.168.0.1"
or"2001:db8::1"
- Returns:
InetAddress
representing the argument- Throws:
java.lang.IllegalArgumentException
- if the argument is not a valid IP string literal
-
parseCidr
public static Tuple<java.net.InetAddress,java.lang.Integer> parseCidr(java.lang.String maskedAddress)
Parse an IP address and its prefix length using the CIDR notation.- Throws:
java.lang.IllegalArgumentException
- if the string is not formatted asip_address/prefix_length
java.lang.IllegalArgumentException
- if the IP address is an IPv6-mapped ipv4 addressjava.lang.IllegalArgumentException
- if the prefix length is not in 0-32 for IPv4 addresses and 0-128 for IPv6 addressesjava.lang.NumberFormatException
- if the prefix length is not an integer
-
-