saker.build Documentation TaskDoc JavaDoc Packages
public class NetworkUtils
Utility class for network related functionalities.
Methods
public static InetSocketAddress
parseInetSocketAddress(String address, int defaultport)
Parses the specified string address argument and converts it to an InetSocketAddress.
public static int
Parses the argument string port number and validates it to be in the required range.
Parses the specified string address argument and converts it to an InetSocketAddress.

The argument address is expected to be in the following format:

  • IPv4 address without port: 1.2.3.4
  • IPv4 address with port: 1.2.3.4:12345
  • IPv6 address without port: [1234:56789:abcd:efAB:CDEF:0000:0000:0000]
  • IPv6 address without port, zeros omitted: [1234:56789:abcd:efAB:CDEF::]
  • IPv6 address with port: [1234:56789:abcd:efAB:CDEF:0000:0000:0000]:12345
  • Network name without port: localhost, MY-PC-ON-LAN
  • Network name with port: localhost:12345, MY-PC-ON-LAN:12345
The address part of the argument will be resolved using InetAddress.getByName(String).

If no port number is specified, the default port argument will be used.

addressThe address to parse.
defaultportThe default port to use if the argument contains none.
The resolved socket address.
UnknownHostExceptionIf the address resolution fails.
NullPointerExceptionIf the argument is null.
IllegalArgumentExceptionIf the address format is invalid, or the port number is out of range.
Parses the argument string port number and validates it to be in the required range.

The methods parses the argument string as an integer. The resulting value will be validated to be in the range of network port numbers ([0, 65535]). The method will allow the port number zero to be specified.

portThe port number string to parse.
The port number if the argument can be parsed and is in the specified range.
NumberFormatExceptionIf the argument is not a valid integral number.
IllegalArgumentExceptionIf the port number is out of range.