-
- All Implemented Interfaces:
public class NetworkUtilsUtility methods and fields to use when working with network addresses.
-
-
Field Summary
Fields Modifier and Type Field Description public final static StringIN6_ADDR_ANYpublic final static StringIN4_ADDR_ANYpublic final static StringIN_ADDR_ANYpublic final static intMAX_PORT_NUMBERpublic final static intMIN_PORT_NUMBER
-
Method Summary
Modifier and Type Method Description static booleanisWindowsAutoConfiguredIPv4Address(InetAddress add)Determines whether the address is the result of windows auto configuration. static intgetRandomPortNumber()Returns a random local port number that user applications could bind to. static intgetRandomPortNumber(int min, int max)Returns a random local port number in the interval [min, max]. static booleanisIPv4Address(String address)Verifies whether address could be an IPv4 address string. static booleanisIPv6Address(String address)Verifies whether address could be an IPv6 address string. static booleanisValidIPAddress(String address)Checks whether address is a valid IP address string. static Array<byte>strToIPv4(String ipv4AddrStr)Creates a byte array containing the specified ipv4AddStr. static Array<byte>strToIPv6(String ipv6AddrStr)Creates a byte array containing the specified ipv6AddStr. static Array<byte>mappedIPv4ToRealIPv4(Array<byte> addr)Returns an IPv4 address matching the one mapped in the IPv6 addr. static InetAddressgetInetAddress(String hostAddress)Creates an InetAddress from the specified hostAddress. static booleanisValidPortNumber(int port)Determines whether port is a valid port number bindable by an application (i.e. static booleanisInterfaceLoopback(NetworkInterface iface)Determines whether or not the iface interface is a loopback interface. static booleanisInterfaceUp(NetworkInterface iface)Determines, if possible, whether or not the iface interface is up. static booleanisInterfaceVirtual(NetworkInterface iface)Determines, if possible, whether or not the iface interface is a virtual interface (e.g. static StringstripScopeID(String ipv6Address)Returns a String that is guaranteed not to contain an address scope specified (i.e. -
-
Method Detail
-
isWindowsAutoConfiguredIPv4Address
static boolean isWindowsAutoConfiguredIPv4Address(InetAddress add)
Determines whether the address is the result of windows auto configuration. (i.e. One that is in the 169.254.0.0 network)
- Parameters:
add- the address to inspect
-
getRandomPortNumber
static int getRandomPortNumber()
Returns a random local port number that user applications could bind to. (i.e. above 1024).
-
getRandomPortNumber
static int getRandomPortNumber(int min, int max)
Returns a random local port number in the interval [min, max].
- Parameters:
min- the minimum allowed value for the returned port number.max- the maximum allowed value for the returned port number.
-
isIPv4Address
static boolean isIPv4Address(String address)
Verifies whether address could be an IPv4 address string.
- Parameters:
address- the String that we'd like to determine as an IPv4 address.
-
isIPv6Address
static boolean isIPv6Address(String address)
Verifies whether address could be an IPv6 address string.
- Parameters:
address- the String that we'd like to determine as an IPv6 address.
-
isValidIPAddress
static boolean isValidIPAddress(String address)
Checks whether address is a valid IP address string.
- Parameters:
address- the address that we'd like to check
-
strToIPv4
static Array<byte> strToIPv4(String ipv4AddrStr)
Creates a byte array containing the specified ipv4AddStr.
- Parameters:
ipv4AddrStr- a String containing an IPv4 address.
-
strToIPv6
static Array<byte> strToIPv6(String ipv6AddrStr)
Creates a byte array containing the specified ipv6AddStr.
- Parameters:
ipv6AddrStr- a String containing an IPv6 address.
-
mappedIPv4ToRealIPv4
static Array<byte> mappedIPv4ToRealIPv4(Array<byte> addr)
Returns an IPv4 address matching the one mapped in the IPv6 addr. Both input and returned value are in network order.
- Parameters:
addr- a String representing an IPv4-Mapped address in textual format
-
getInetAddress
static InetAddress getInetAddress(String hostAddress)
Creates an InetAddress from the specified hostAddress. The point of using the method rather than creating the address by yourself is that it would first check whether the specified hostAddress is indeed a valid ip address. It this is the case, the method would create the InetAddress using the InetAddress.getByAddress() method so that no DNS resolution is attempted by the JRE. Otherwise it would simply use InetAddress.getByName() so that we would an InetAddress instance even at the cost of a potential DNS resolution.
- Parameters:
hostAddress- the String representation of the address that we would like to create an InetAddress instance for.
-
isValidPortNumber
static boolean isValidPortNumber(int port)
Determines whether port is a valid port number bindable by an application (i.e. an integer between 1024 and 65535).
- Parameters:
port- the port number that we'd like verified.
-
isInterfaceLoopback
static boolean isInterfaceLoopback(NetworkInterface iface)
Determines whether or not the iface interface is a loopback interface. We use this method as a replacement to the NetworkInterface.isLoopback() method that only comes with java 1.6.
- Parameters:
iface- the inteface that we'd like to determine as loopback or not.
-
isInterfaceUp
static boolean isInterfaceUp(NetworkInterface iface)
Determines, if possible, whether or not the iface interface is up. We use this method so that we could use 's isUp() when running a JVM that supports it and return a default value otherwise.
- Parameters:
iface- the interface that we'd like to determine as Up or Down.
-
isInterfaceVirtual
static boolean isInterfaceVirtual(NetworkInterface iface)
Determines, if possible, whether or not the iface interface is a virtual interface (e.g. VPN, MIPv6 tunnel, etc.) or not. We use this method so that we could use java.net.NetworkInterface's isVirtual() when running a JVM that supports it and return a default value otherwise.
- Parameters:
iface- the interface that we'd like to determine as virtual or not.
-
stripScopeID
static String stripScopeID(String ipv6Address)
Returns a String that is guaranteed not to contain an address scope specified (i.e. removes the %scopeID at the end of IPv6 addresses returned by Java. Takes into account the presence or absence of square brackets encompassing the address.
- Parameters:
ipv6Address- the address whose scope ID we'd like to get rid of.
-
-
-
-