Package de.aristaflow.adept2.util.net
Class NetworkTools
- java.lang.Object
- 
- de.aristaflow.adept2.util.net.NetworkTools
 
- 
 public class NetworkTools extends Object This class provides tooling related to networking, especially handling of IP addresses with trailing subnet masks in CIDR notation.
- 
- 
Field SummaryFields Modifier and Type Field Description static StringIP_V4_ADDRESS_PATTERNThe regexp pattern for an IPv4 address.
 - 
Constructor SummaryConstructors Constructor Description NetworkTools()
 - 
Method SummaryAll Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static InetAddressfindInterface(byte[] subnetAddress, byte mask)Finds the IPv4 address of a network interface of this host that is in the designated subnet.static InetAddressfindInterface(InetAddress subnetAddress, byte mask)Finds the IPv4 address of a network interface of this host that is in the designated subnet.static StringgetIp4AddressString(byte[] address, byte mask)Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).static StringgetIp4AddressString(Inet4Address address, byte mask)Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).protected static intgetNetworkPrefix(byte[] address, byte mask)Gets the network prefix of the designated IPv4 address using the designated mask (CIDR notation).static booleaninSubnet(byte[] address, byte[] subnet, byte mask)Gets whether the designated IPv4 address is in the designated subnet (subnet address + mask).static booleanisLocalAddress(InetAddress ipToCheck)Checks if the specified IPv4 address is one of that addresses which are assigned to this host or if it is an address assigned to a remote host.static Pair<byte[],Byte>parseIp4Address(String address)Parses an IPv4 address from the designated string.static Pair<Inet4Address,Byte>resolveIp4Address(String address)Resolves an IPv4 address from the designated string.static byte[]toBytes(int address)Deprecated.Do no longer represent an IP(v4) address asintbut always asbyte[].static inttoInt(byte[] address)Deprecated.Do no longer represent an IP(v4) address asintbut always asbyte[].protected static inttoIntMask(byte mask)Converts the designated mask (i.
 
- 
- 
- 
Field Detail- 
IP_V4_ADDRESS_PATTERNpublic static final String IP_V4_ADDRESS_PATTERN The regexp pattern for an IPv4 address.- See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
getIp4AddressStringpublic static String getIp4AddressString(Inet4Address address, byte mask) Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).- Parameters:
- address- The IP address as- byte[]for which to get a string representation.
- mask- The subnet mask for the string representation if (- 0 < mask <= 32).
- Returns:
- A string representation for the designated IP (v4) address including an optional subnet mask.
 
 - 
getIp4AddressStringpublic static String getIp4AddressString(byte[] address, byte mask) Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).- Parameters:
- address- The IP address as- byte[]for which to get a string representation.
- mask- The subnet mask for the string representation if (- 0 < mask <= 32).
- Returns:
- A string representation for the designated IP (v4) address including an optional subnet mask.
 
 - 
parseIp4Addresspublic static Pair<byte[],Byte> parseIp4Address(String address) Parses an IPv4 address from the designated string. This may include a subnet mask in CIDR notation.- Parameters:
- address- The IPv4 address/subnet which to parse.
- Returns:
- The parsed IPv4 address and the subnet mask. If no subnet mask is provided,
         32will be returned as subnet mask to represent a single IP address.
- Throws:
- IllegalArgumentException- If the designated address is not a valid IPv4 address (optionally including a subnet mask in CIDR notation), an- IllegalArgumentExceptionwill be thrown.
 
 - 
resolveIp4Addresspublic static Pair<Inet4Address,Byte> resolveIp4Address(String address) throws UnknownHostException Resolves an IPv4 address from the designated string. This may include a subnet mask in CIDR notation.- Parameters:
- address- The IPv4 address/subnet which to parse and resolve.
- Returns:
- The parsed IPv4 address and the subnet mask. If no subnet mask is provided,
         32will be returned as subnet mask to represent a single IP address.
- Throws:
- UnknownHostException- If the designated address cannot be resolved to a corresponding host, an- UnknownHostExceptionwill be thrown.
 
 - 
toBytes@Deprecated public static byte[] toBytes(int address) Deprecated.Do no longer represent an IP(v4) address asintbut always asbyte[]. This also simplifies switching to IPv6.Converts the designatedintrepresenting an IPv4 address to an array of 4bytes containing each address element separately.- Parameters:
- address- The IPv4 address represented as- int.
- Returns:
- 4 bytes for the designated IPv4 address.
 
 - 
toInt@Deprecated public static int toInt(byte[] address) Deprecated.Do no longer represent an IP(v4) address asintbut always asbyte[]. This also simplifies switching to IPv6.Converts the (4) designatedbytes to anint. This is used to represent IPv4 addresses.- Parameters:
- address- The IPv4 address represented as 4- bytes.
- Returns:
- An intrepresenting the designated 4bytes.
 
 - 
toIntMaskprotected static int toIntMask(byte mask) Converts the designated mask (i. e. a number from CIDR notation) to anintsimply containing the designated amount of consecutive1s starting on the highest bit.- Parameters:
- mask- The subnet mask from CIDR notation, which is a number between 0 and 32.
- Returns:
- An inthaving the designated amount of consecutive1s.
 
 - 
getNetworkPrefixprotected static int getNetworkPrefix(byte[] address, byte mask)Gets the network prefix of the designated IPv4 address using the designated mask (CIDR notation).- Parameters:
- address- The IP address for which to get the network prefix.
- mask- The subnet mask from CIDR notation, which is a number between 0 and 32.
- Returns:
- The network prefix for the designated IPv4 address using the designated mask.
 
 - 
inSubnetpublic static boolean inSubnet(byte[] address, byte[] subnet, byte mask)Gets whether the designated IPv4 address is in the designated subnet (subnet address + mask). This just returns whether the network prefix ofaddressandsubnetare equal with respect tomask.- Parameters:
- address- The IPv4 address which to check for whether it is in the designated subnet.
- subnet- The address of the subnet.
- mask- The subnet mask from CIDR notation, which is a number between 0 and 32.
- Returns:
- Whether the designated IPv4 address is in the designated subnet (subnet address + mask).
 
 - 
findInterfacepublic static InetAddress findInterface(InetAddress subnetAddress, byte mask) throws SocketException Finds the IPv4 address of a network interface of this host that is in the designated subnet. If no corresponding network interface is found,nullwill be returned.- Parameters:
- subnetAddress- The subnet address.
- mask- The subnet mask from CIDR notation, which is a number between 0 and 32.
- Returns:
- The IPv4 address of a network interface of this host that is in the designated subnet
         or nullif no corresponding network interface is found.
- Throws:
- SocketException- If an I/O error occurs when retrieving all network interfaces of this host, a- SocketExceptionwill be thrown.
 
 - 
findInterfacepublic static InetAddress findInterface(byte[] subnetAddress, byte mask) throws SocketException Finds the IPv4 address of a network interface of this host that is in the designated subnet. If no corresponding network interface is found,nullwill be returned.- Parameters:
- subnetAddress- The subnet address.
- mask- The subnet mask from CIDR notation, which is a number between 0 and 32.
- Returns:
- The IPv4 address of a network interface of this host that is in the designated subnet
         or nullif no corresponding network interface is found.
- Throws:
- SocketException- If an I/O error occurs when retrieving all network interfaces of this host, a- SocketExceptionwill be thrown.
 
 - 
isLocalAddresspublic static boolean isLocalAddress(InetAddress ipToCheck) throws SocketException Checks if the specified IPv4 address is one of that addresses which are assigned to this host or if it is an address assigned to a remote host.- Parameters:
- ipToCheck- The IP address to check.
- Returns:
- Whether the designated IPv4 address belongs to this host (or a remote host).
- Throws:
- SocketException- If an I/O error occurs when retrieving all network interfaces of this host, a- SocketExceptionwill be thrown.
 
 
- 
 
-