public abstract class Addresses extends Object
Performance Warning: Many methods in this class are very slow on Windows (approximately 200ms + 50ms per interface). Consider caching results when possible.
| Modifier and Type | Method and Description |
|---|---|
static void |
clearCaches()
Clears all internal caches.
|
static SortedSet<String> |
getAddresses()
Gets all non-IPv6 addresses excluding local, broadcast, and multicast addresses.
|
static SortedSet<String> |
getAddresses(boolean includeLocal,
boolean includeIPv6)
Warning: When includeLocal is false,
all returned addresses should be routable, but they are not necessarily
appropriate for external use.
|
static SortedSet<String> |
getAddresses(boolean includeSiteLocal,
boolean includeLoopbackAndWildcard,
boolean includeIPv6)
Warning: When includeSiteLocal and includeLoopbackAndWildcard are false,
all returned addresses should be routable, but they are not necessarily
appropriate for external use.
|
static SortedSet<String> |
getAddresses(boolean includeSiteLocal,
boolean includeLoopbackAndWildcard,
boolean includeIPv6,
boolean includeIPv6Temporary)
Warning: When includeSiteLocal and includeLoopbackAndWildcard are false,
all returned addresses should be routable, but they are not necessarily
appropriate for external use.
|
static SortedSet<String> |
getAllAddresses()
Gets all addresses excluding only link-local and multicast addresses.
|
static String |
getAnyAddress()
Gets the first non-local IPv4 address found.
|
static Set<AddressType> |
getConnectedAddressTypes()
Efficiently get all connected address types in one pass.
|
static byte[] |
getIP(String host)
Caching version of InetAddress.getByName(host).getAddress(), which is slow.
|
static byte[] |
getIP(String host,
boolean preferIPv6)
For literal IP addresses, same as getIP(String).
|
static byte[] |
getIPOnly(String host)
Caching version of InetAddress.getByName(host).getAddress(), which is slow.
|
static List<byte[]> |
getIPs(String host)
For literal IP addresses, same as getIP(String).
|
static int |
getPort(String port)
Convenience method to convert and validate a port String
without throwing an exception.
|
static byte[] |
getYggdrasilAddress()
Caller should cache results for better performance.
|
static boolean |
isConnected()
Do we have any non-loop, non-wildcard IPv4 address at all?
Use getConnectedAddressTypes() to get all types at once for better performance.
|
static boolean |
isConnected(AddressType type)
Do we have any address of this type?
Use getConnectedAddressTypes() to get all types at once for better performance.
|
static boolean |
isConnectedIPv6()
Do we have any non-loop, non-wildcard IPv6 address at all?
Use getConnectedAddressTypes() to get all types at once for better performance.
|
static boolean |
isDeprecated(Inet6Address addr)
Determines if an IPv6 address is deprecated.
|
static boolean |
isDynamic(Inet6Address addr)
Determines if an IPv6 address is dynamic.
|
static boolean |
isIPAddress(String host)
Checks if the host is a valid IP address (IPv4 or IPv6).
|
static boolean |
isIPv4Address(String host)
Checks if the host is a valid IPv4 address.
|
static boolean |
isIPv6Address(String host)
Checks if the host is a valid IPv6 address.
|
static boolean |
isTemporary(Inet6Address addr)
Determines if an IPv6 address is temporary.
|
static void |
main(String[] args)
Print out the local addresses
|
static String |
toCanonicalString(byte[] addr)
Same as toString() but returns IPv6 in compressed form per RFC 5952.
|
static String |
toCanonicalString(byte[] addr,
int port)
Same as toString() but returns IPv6 in compressed form per RFC 5952.
|
static String |
toCanonicalString(String host)
Converts IPv6 to compressed form per RFC 5952.
|
static String |
toString(byte[] addr)
Convenience method to convert an IP address to a String without throwing an exception.
|
static String |
toString(byte[] addr,
int port)
Convenience method to convert an IP address and port to a String without throwing an exception.
|
static String |
useIPv6TempAddresses()
Checks if temporary IPv6 addresses are enabled system-wide.
|
public static void clearCaches()
public static SortedSet<String> getAddresses()
public static SortedSet<String> getAddresses(boolean includeLocal, boolean includeIPv6)
includeLocal - whether to include local addresses and deprecated IPv6 addressesincludeIPv6 - whether to include IPv6 addressespublic static SortedSet<String> getAddresses(boolean includeSiteLocal, boolean includeLoopbackAndWildcard, boolean includeIPv6)
includeSiteLocal - whether to include private addresses like 192.168.x.x and deprecated IPv6 addressesincludeLoopbackAndWildcard - whether to include loopback addresses (127.x.x.x) and wildcard (0.0.0.0)includeIPv6 - whether to include IPv6 addressespublic static SortedSet<String> getAddresses(boolean includeSiteLocal, boolean includeLoopbackAndWildcard, boolean includeIPv6, boolean includeIPv6Temporary)
includeSiteLocal - whether to include private addresses like 192.168.x.x and deprecated IPv6 addressesincludeLoopbackAndWildcard - whether to include loopback addresses (127.x.x.x) and wildcard (0.0.0.0)includeIPv6 - whether to include IPv6 addressesincludeIPv6Temporary - whether to include IPv6 temporary addressespublic static SortedSet<String> getAllAddresses()
public static String getAnyAddress()
public static Set<AddressType> getConnectedAddressTypes()
public static byte[] getIP(String host)
host - DNS or IPv4 or IPv6 address; if null or empty returns nullpublic static byte[] getIP(String host, boolean preferIPv6)
host - DNS or IPv4 or IPv6 address; if null returns nullpreferIPv6 - whether to prefer IPv6 addressespublic static byte[] getIPOnly(String host)
host - literal IPv4 or IPv6 address; if null returns nullpublic static List<byte[]> getIPs(String host)
host - DNS or IPv4 or IPv6 address; if null returns nullpublic static int getPort(String port)
port - the port string to convertpublic static byte[] getYggdrasilAddress()
public static boolean isConnected()
public static boolean isConnected(AddressType type)
type - the address type to checkpublic static boolean isConnectedIPv6()
public static boolean isDeprecated(Inet6Address addr)
addr - IPv6 address of a local interface, as returned from getAddresses()public static boolean isDynamic(Inet6Address addr)
addr - IPv6 address of a local interface, as returned from getAddresses()public static boolean isIPAddress(String host)
host - the host to checkpublic static boolean isIPv4Address(String host)
host - the host to checkpublic static boolean isIPv6Address(String host)
host - the host to checkpublic static boolean isTemporary(Inet6Address addr)
addr - IPv6 address of a local interface, as returned from getAddresses()public static void main(String[] args)
args - command line argumentspublic static String toCanonicalString(byte[] addr)
addr - the IP address as byte arraypublic static String toCanonicalString(byte[] addr, int port)
addr - the IP address as byte arrayport - the port numberpublic static String toCanonicalString(String host)
host - the host string to convertpublic static String toString(byte[] addr)
addr - the IP address as byte arraypublic static String toString(byte[] addr, int port)
addr - the IP address as byte arrayport - the port numberpublic static String useIPv6TempAddresses()