Package org.wildfly.common.net
Class Inet
java.lang.Object
org.wildfly.common.net.Inet
Utilities relating to Internet protocol (a.k.a. "INET" or "IP") address manipulation.
- Author:
- David M. Lloyd
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Inet4AddressThe "any" address for IPv4.static final Inet4AddressThe broadcast-all address for IPv4.static final Inet4AddressThe traditional loopback address for IPv4.static final Inet6AddressThe "any" address for IPv6.static final Inet6AddressThe loopback address for IPv6. -
Method Summary
Modifier and TypeMethodDescriptionstatic NetworkInterfacefindInterfaceWithScopeId(String scopeName) static <T extends InetAddress>
TgetAddressByNameAndType(String hostName, Class<T> addressType) Resolve the given host name, returning the first answer with the given address type.static <T extends InetAddress>
T[]getAllAddressesByNameAndType(String hostName, Class<T> addressType) Resolve the given host name, returning all answers with the given address type.static StringgetHostNameIfResolved(InetAddress inetAddress) Get the host name of the given address, if it is resolved.static StringgetHostNameIfResolved(InetSocketAddress socketAddress) Get the host name of the given address, if it is resolved.static Inet4AddressgetInet4Address(int s1, int s2, int s3, int s4) Get an IPv4 address from four integer segments.static Inet6AddressgetInet6Address(int s1, int s2, int s3, int s4, int s5, int s6, int s7, int s8) Get an IPv6 address from eight integer segments.static ProtocolFamilygetProtocolFamily(InetAddress inetAddress) Get the protocol family of the given Internet address.static InetSocketAddressgetResolved(InetSocketAddress address) Get a copy of the given socket address, but with a resolved address component.static InetSocketAddressgetResolved(InetSocketAddress address, Class<? extends InetAddress> addressType) Get a copy of the given socket address, but with a resolved address component of the given type.static InetSocketAddressgetResolved(URI uri, int defaultPort) Get the resolved socket address from the given URI.static InetSocketAddressgetResolved(URI uri, int defaultPort, Class<? extends InetAddress> addressType) Get a resolved socket address from the given URI.static InetAddressGet an Internet address for a URI destination, resolving the host name if necessary.static <T extends InetAddress>
TgetResolvedInetAddress(URI uri, Class<T> addressType) Get an Internet address for a URI destination, resolving the host name if necessary.static intgetScopeId(String scopeName) Attempt to get the scope ID of the given string.static intgetScopeId(String scopeName, InetAddress compareWith) Attempt to get the scope ID of the given string.static intgetScopeId(InetAddress address) Get the scope ID of the given address (if it is an IPv6 address).static intgetScopeId(NetworkInterface networkInterface) static intgetScopeId(NetworkInterface networkInterface, InetAddress compareWith) static URIgetURIFromAddress(String scheme, InetSocketAddress socketAddress, int defaultPort) Extract a base URI from the given scheme and socket address.static booleanisInet4Address(String address) Checks whether given String is a valid IPv4 address.static booleanisInet6Address(String address) Checks whether given String is a valid IPv6 address.static CidrAddressparseCidrAddress(String address) Parse a CIDR address into aCidrAddressobject.static Inet4AddressparseInet4Address(String address) Parse an IPv4 address into anInet4Addressobject.static Inet4AddressparseInet4Address(String address, String hostName) Parse an IPv4 address into anInet4Addressobject.static Inet4AddressparseInet4AddressOrFail(String address) Parse an IPv4 address into anInet4Addressobject, throwing an exception on failure.static Inet4AddressparseInet4AddressOrFail(String address, String hostName) Parse an IPv4 address into anInet4Addressobject.static byte[]parseInet4AddressToBytes(String address) Converts IPv4 address from textual representation to bytes.static Inet6AddressparseInet6Address(String address) Parse an IPv6 address into anInet6Addressobject.static Inet6AddressparseInet6Address(String address, String hostName) Parse an IPv6 address into anInet6Addressobject.static Inet6AddressparseInet6AddressOrFail(String address) Parse an IPv6 address into anInet6Addressobject, throwing an exception on failure.static Inet6AddressparseInet6AddressOrFail(String address, String hostName) Parse an IPv6 address into anInet6Addressobject.static byte[]parseInet6AddressToBytes(String address) Converts IPv6 address from textual representation to bytes.static InetAddressparseInetAddress(String address) Parse an IP address into anInetAddressobject.static InetAddressparseInetAddress(String address, String hostName) Parse an IP address into anInetAddressobject.static InetAddressparseInetAddressOrFail(String address) Parse an IP address into anInetAddressobject, throwing an exception on failure.static InetAddressparseInetAddressOrFail(String address, String hostName) Parse an IP address into anInetAddressobject.static byte[]parseInetAddressToBytes(String address) Converts an IP address from textual representation to bytes.static Inet6AddresstoInet6Address(InetAddress inetAddress) Get the IPv6 equivalent of the given address.static StringtoOptimalString(byte[] addressBytes) Get the optimal string representation of the bytes of an IP address.static StringtoOptimalString(InetAddress inetAddress) Get the optimal string representation of an IP address.static StringtoURLString(byte[] addressBytes) Get a string representation of the given address bytes which is suitable for use as the host component of a URL.static StringtoURLString(InetAddress inetAddress, boolean useHostNameIfPresent) Get a string representation of the given address which is suitable for use as the host component of a URL.
-
Field Details
-
INET4_ANY
The "any" address for IPv4. -
INET4_LOOPBACK
The traditional loopback address for IPv4. -
INET4_BROADCAST
The broadcast-all address for IPv4. -
INET6_ANY
The "any" address for IPv6. -
INET6_LOOPBACK
The loopback address for IPv6.
-
-
Method Details
-
toOptimalString
Get the optimal string representation of an IP address. For IPv6 addresses, this representation will be more compact that the default.- Parameters:
inetAddress- the address (must not benull)- Returns:
- the string representation (not
null)
-
toOptimalString
Get the optimal string representation of the bytes of an IP address.- Parameters:
addressBytes- the address bytes (must not benull)- Returns:
- the string representation (not
null)
-
toURLString
Get a string representation of the given address which is suitable for use as the host component of a URL.- Parameters:
inetAddress- the address (must not benull)useHostNameIfPresent-trueto preserve the host name string in the address,falseto always give an IP address string- Returns:
- the string representation (not
null)
-
toURLString
Get a string representation of the given address bytes which is suitable for use as the host component of a URL.- Parameters:
addressBytes- the address bytes (must not benull)- Returns:
- the string representation (not
null)
-
toInet6Address
Get the IPv6 equivalent of the given address. If the address is IPv4 then it is returned as a compatibility address.- Parameters:
inetAddress- the address to convert (must not benull)- Returns:
- the converted address (not
null)
-
getHostNameIfResolved
Get the host name of the given address, if it is resolved. Otherwise, returnnull.- Parameters:
inetAddress- the address to check (must not benull)- Returns:
- the host name, or
nullif the address has no host name and is unresolved
-
getHostNameIfResolved
Get the host name of the given address, if it is resolved. Otherwise, returnnull.- Parameters:
socketAddress- the socket address to check (must not benull)- Returns:
- the host name, or
nullif the address has no host name and is unresolved
-
getResolved
public static InetSocketAddress getResolved(URI uri, int defaultPort, Class<? extends InetAddress> addressType) throws UnknownHostException Get a resolved socket address from the given URI.- Parameters:
uri- the URI (must not benull)defaultPort- the default port to use if none is given (must be in the range1 ≤ n ≤ 65535addressType- the class of theInetAddressto search for (must not benull)- Returns:
- the socket address, or
nullif the URI does not have a host component - Throws:
UnknownHostException- if address resolution failed
-
getResolved
Get the resolved socket address from the given URI.- Parameters:
uri- the URI (must not benull)defaultPort- the default port to use if none is given (must be in the range1 ≤ n ≤ 65535- Returns:
- the socket address, or
nullif the URI does not have a host component - Throws:
UnknownHostException- if address resolution failed
-
getResolvedInetAddress
public static <T extends InetAddress> T getResolvedInetAddress(URI uri, Class<T> addressType) throws UnknownHostException Get an Internet address for a URI destination, resolving the host name if necessary.- Type Parameters:
T- the type of theInetAddressto search for- Parameters:
uri- the destination URI- Returns:
- the address, or
nullif no authority is present in the URI - Throws:
UnknownHostException- if the URI host was existent but could not be resolved to a valid address
-
getResolvedInetAddress
Get an Internet address for a URI destination, resolving the host name if necessary.- Parameters:
uri- the destination URI- Returns:
- the address, or
nullif no authority is present in the URI - Throws:
UnknownHostException- if the URI host was existent but could not be resolved to a valid address
-
getResolved
Get a copy of the given socket address, but with a resolved address component.- Parameters:
address- the (possibly unresolved) address (must not benull)- Returns:
- the resolved address (not
null) - Throws:
UnknownHostException- if address resolution failed
-
getResolved
public static InetSocketAddress getResolved(InetSocketAddress address, Class<? extends InetAddress> addressType) throws UnknownHostException Get a copy of the given socket address, but with a resolved address component of the given type.- Parameters:
address- the (possibly unresolved) address (must not benull)addressType- the class of theInetAddressto search for (must not benull)- Returns:
- the resolved address (not
null) - Throws:
UnknownHostException- if address resolution failed, or if no addresses of the given type were found, or if the given address was already resolved but is not of the given address type
-
getAddressByNameAndType
public static <T extends InetAddress> T getAddressByNameAndType(String hostName, Class<T> addressType) throws UnknownHostException Resolve the given host name, returning the first answer with the given address type.- Type Parameters:
T- the type of theInetAddressto search for- Parameters:
hostName- the host name to resolve (must not benull)addressType- the class of theInetAddressto search for (must not benull)- Returns:
- the resolved address (not
null) - Throws:
UnknownHostException- if address resolution failed or if no addresses of the given type were found
-
getAllAddressesByNameAndType
public static <T extends InetAddress> T[] getAllAddressesByNameAndType(String hostName, Class<T> addressType) throws UnknownHostException Resolve the given host name, returning all answers with the given address type.- Type Parameters:
T- the type of theInetAddressto search for- Parameters:
hostName- the host name to resolve (must not benull)addressType- the class of theInetAddressto search for (must not benull)- Returns:
- the resolved addresses (not
null) - Throws:
UnknownHostException- if address resolution failed or if no addresses of the given type were found
-
getInet4Address
Get an IPv4 address from four integer segments. Each segment must be between 0 and 255.- Parameters:
s1- the first segments2- the second segments3- the third segments4- the fourth segment- Returns:
- the address (not
null)
-
getInet6Address
public static Inet6Address getInet6Address(int s1, int s2, int s3, int s4, int s5, int s6, int s7, int s8) Get an IPv6 address from eight integer segments. Each segment must be between 0 and 65535 (0xffff).- Parameters:
s1- the first segments2- the second segments3- the third segments4- the fourth segments5- the fifth segments6- the sixth segments7- the seventh segments8- the eighth segment- Returns:
- the address (not
null)
-
isInet6Address
Checks whether given String is a valid IPv6 address.- Parameters:
address- address textual representation- Returns:
trueifaddressis a valid IPv6 address,falseotherwise
-
parseInet6Address
Parse an IPv6 address into anInet6Addressobject.- Parameters:
address- the address to parse- Returns:
- the parsed address, or
nullif the address is not valid
-
parseInet6Address
Parse an IPv6 address into anInet6Addressobject.- Parameters:
address- the address to parse (must not benull)hostName- the host name to use in the resultant object, ornullto use the string representation of the address- Returns:
- the parsed address, or
nullif the address is not valid
-
parseInet6AddressOrFail
Parse an IPv6 address into anInet6Addressobject, throwing an exception on failure.- Parameters:
address- the address to parse- Returns:
- the parsed address (not
null) - Throws:
IllegalArgumentException- if the address is not valid
-
parseInet6AddressOrFail
Parse an IPv6 address into anInet6Addressobject.- Parameters:
address- the address to parse (must not benull)hostName- the host name to use in the resultant object, ornullto use the string representation of the address- Returns:
- the parsed address (not
null) - Throws:
IllegalArgumentException- if the address is not valid
-
isInet4Address
Checks whether given String is a valid IPv4 address.- Parameters:
address- address textual representation- Returns:
trueifaddressis a valid IPv4 address,falseotherwise
-
parseInet4Address
Parse an IPv4 address into anInet4Addressobject.- Parameters:
address- the address to parse- Returns:
- the parsed address, or
nullif the address is not valid
-
parseInet4Address
Parse an IPv4 address into anInet4Addressobject.- Parameters:
address- the address to parsehostName- the host name to use in the resultant object, ornullto use the string representation of the address- Returns:
- the parsed address, or
nullif the address is not valid
-
parseInet4AddressOrFail
Parse an IPv4 address into anInet4Addressobject, throwing an exception on failure.- Parameters:
address- the address to parse- Returns:
- the parsed address (not
null) - Throws:
IllegalArgumentException- if the address is not valid
-
parseInet4AddressOrFail
Parse an IPv4 address into anInet4Addressobject.- Parameters:
address- the address to parse (must not benull)hostName- the host name to use in the resultant object, ornullto use the string representation of the address- Returns:
- the parsed address (not
null) - Throws:
IllegalArgumentException- if the address is not valid
-
parseInetAddress
Parse an IP address into anInetAddressobject.- Parameters:
address- the address to parse- Returns:
- the parsed address, or
nullif the address is not valid
-
parseInetAddress
Parse an IP address into anInetAddressobject.- Parameters:
address- the address to parsehostName- the host name to use in the resultant object, ornullto use the string representation of the address- Returns:
- the parsed address, or
nullif the address is not valid
-
parseInetAddressOrFail
Parse an IP address into anInetAddressobject, throwing an exception on failure.- Parameters:
address- the address to parse- Returns:
- the parsed address (not
null) - Throws:
IllegalArgumentException- if the address is not valid
-
parseInetAddressOrFail
Parse an IP address into anInetAddressobject.- Parameters:
address- the address to parse (must not benull)hostName- the host name to use in the resultant object, ornullto use the string representation of the address- Returns:
- the parsed address (not
null) - Throws:
IllegalArgumentException- if the address is not valid
-
parseCidrAddress
Parse a CIDR address into aCidrAddressobject.- Parameters:
address- the address to parse- Returns:
- the parsed address, or
nullif the address is not valid
-
parseInet6AddressToBytes
Converts IPv6 address from textual representation to bytes.If given string doesn't represent valid IPv6 address, the method returns
null.- Parameters:
address- address textual representation- Returns:
- byte array representing the address, or
nullif the address is not valid
-
parseInet4AddressToBytes
Converts IPv4 address from textual representation to bytes.If given string doesn't represent valid IPv4 address, the method returns
null.This only supports decimal notation.
- Parameters:
address- address textual representation- Returns:
- byte array representing the address, or
nullif the address is not valid
-
parseInetAddressToBytes
Converts an IP address from textual representation to bytes.If given string doesn't represent valid IP address, the method returns
null.- Parameters:
address- address textual representation- Returns:
- byte array representing the address, or
nullif the address is not valid
-
getScopeId
Get the scope ID of the given address (if it is an IPv6 address).- Returns:
- the scope ID, or 0 if there is none or the address is an IPv4 address
-
getScopeId
Attempt to get the scope ID of the given string. If the string is numeric then the number is parsed and returned as-is. If the scope is a string, then a search for the matching network interface will occur.- Parameters:
scopeName- the scope number or name as a string (must not benull)- Returns:
- the scope ID, or 0 if no matching scope could be found
-
getScopeId
Attempt to get the scope ID of the given string. If the string is numeric then the number is parsed and returned as-is. If the scope is a string, then a search for the matching network interface will occur.- Parameters:
scopeName- the scope number or name as a string (must not benull)compareWith- the address to compare with, to ensure that the wrong local scope is not selected (may benull)- Returns:
- the scope ID, or 0 if no matching scope could be found
-
findInterfaceWithScopeId
-
getScopeId
-
getScopeId
-
getURIFromAddress
public static URI getURIFromAddress(String scheme, InetSocketAddress socketAddress, int defaultPort) throws URISyntaxException Extract a base URI from the given scheme and socket address. The address is not resolved.- Parameters:
scheme- the URI schemesocketAddress- the host socket addressdefaultPort- the protocol default port, or -1 if there is none- Returns:
- the URI instance
- Throws:
URISyntaxException- if the URI failed to be constructed for some reason
-
getProtocolFamily
Get the protocol family of the given Internet address.- Parameters:
inetAddress- the address (must not benull)- Returns:
- the protocol family (not
null)
-