Class NetUtils


  • @Internal
    public class NetUtils
    extends Object
    Utility for various network related tasks (such as finding free ports).
    • Constructor Detail

      • NetUtils

        public NetUtils()
    • Method Detail

      • getHostnameFromFQDN

        public static String getHostnameFromFQDN​(String fqdn)
        Turn a fully qualified domain name (fqdn) into a hostname. If the fqdn has multiple subparts (separated by a period '.'), it will take the first part. Otherwise it takes the entire fqdn.
        Parameters:
        fqdn - The fully qualified domain name.
        Returns:
        The hostname.
      • getCorrectHostnamePort

        public static URL getCorrectHostnamePort​(String hostPort)
        Converts a string of the form "host:port" into an URL.
        Parameters:
        hostPort - The "host:port" string.
        Returns:
        The converted URL.
      • parseHostPortAddress

        public static InetSocketAddress parseHostPortAddress​(String hostPort)
        Converts a string of the form "host:port" into an InetSocketAddress.
        Parameters:
        hostPort - The "host:port" string.
        Returns:
        The converted InetSocketAddress.
      • socketToUrl

        public static URL socketToUrl​(InetSocketAddress socketAddress)
        Converts an InetSocketAddress to a URL. This method assigns the "http://" schema to the URL by default.
        Parameters:
        socketAddress - the InetSocketAddress to be converted
        Returns:
        a URL object representing the provided socket address with "http://" schema
      • acceptWithoutTimeout

        public static Socket acceptWithoutTimeout​(ServerSocket serverSocket)
                                           throws IOException
        Calls ServerSocket.accept() on the provided server socket, suppressing any thrown SocketTimeoutExceptions. This is a workaround for the underlying JDK-8237858 bug in JDK 11 that can cause errant SocketTimeoutExceptions to be thrown at unexpected times.

        This method expects the provided ServerSocket has no timeout set (SO_TIMEOUT of 0), indicating an infinite timeout. It will suppress all SocketTimeoutExceptions, even if a ServerSocket with a non-zero timeout is passed in.

        Parameters:
        serverSocket - a ServerSocket with SO_TIMEOUT set to 0; if SO_TIMEOUT is greater than 0, then this method will suppress SocketTimeoutException; must not be null; SO_TIMEOUT option must be set to 0
        Returns:
        the new Socket
        Throws:
        IOException - see ServerSocket.accept()
        See Also:
        JDK-8237858
      • getAvailablePort

        public static NetUtils.Port getAvailablePort()
        Find a non-occupied port.
        Returns:
        A non-occupied port.
      • unresolvedHostToNormalizedString

        public static String unresolvedHostToNormalizedString​(String host)
        Returns an address in a normalized format for Pekko. When an IPv6 address is specified, it normalizes the IPv6 address to avoid complications with the exact URL match policy of Pekko.
        Parameters:
        host - The hostname, IPv4 or IPv6 address
        Returns:
        host which will be normalized if it is an IPv6 address
      • unresolvedHostAndPortToNormalizedString

        public static String unresolvedHostAndPortToNormalizedString​(String host,
                                                                     int port)
        Returns a valid address for Pekko. It returns a String of format 'host:port'. When an IPv6 address is specified, it normalizes the IPv6 address to avoid complications with the exact URL match policy of Pekko.
        Parameters:
        host - The hostname, IPv4 or IPv6 address
        port - The port
        Returns:
        host:port where host will be normalized if it is an IPv6 address
      • ipAddressToUrlString

        public static String ipAddressToUrlString​(InetAddress address)
        Encodes an IP address properly as a URL string. This method makes sure that IPv6 addresses have the proper formatting to be included in URLs.
        Parameters:
        address - The IP address to encode.
        Returns:
        The proper URL string encoded IP address.
      • ipAddressAndPortToUrlString

        public static String ipAddressAndPortToUrlString​(InetAddress address,
                                                         int port)
        Encodes an IP address and port to be included in URL. in particular, this method makes sure that IPv6 addresses have the proper formatting to be included in URLs.
        Parameters:
        address - The address to be included in the URL.
        port - The port for the URL address.
        Returns:
        The proper URL string encoded IP address and port.
      • socketAddressToUrlString

        public static String socketAddressToUrlString​(InetSocketAddress address)
        Encodes an IP address and port to be included in URL. in particular, this method makes sure that IPv6 addresses have the proper formatting to be included in URLs.
        Parameters:
        address - The socket address with the IP address and port.
        Returns:
        The proper URL string encoded IP address and port.
      • hostAndPortToUrlString

        public static String hostAndPortToUrlString​(String host,
                                                    int port)
                                             throws UnknownHostException
        Normalizes and encodes a hostname and port to be included in URL. In particular, this method makes sure that IPv6 address literals have the proper formatting to be included in URLs.
        Parameters:
        host - The address to be included in the URL.
        port - The port for the URL address.
        Returns:
        The proper URL string encoded IP address and port.
        Throws:
        UnknownHostException - Thrown, if the hostname cannot be translated into a URL.
      • getPortRangeFromString

        public static Iterator<Integer> getPortRangeFromString​(String rangeDefinition)
                                                        throws NumberFormatException
        Returns an iterator over available ports defined by the range definition.
        Parameters:
        rangeDefinition - String describing a single port, a range of ports or multiple ranges.
        Returns:
        Set of ports from the range definition
        Throws:
        NumberFormatException - If an invalid string is passed.
      • createSocketFromPorts

        public static ServerSocket createSocketFromPorts​(Iterator<Integer> portsIterator,
                                                         NetUtils.SocketFactory factory)
        Tries to allocate a socket from the given sets of ports.
        Parameters:
        portsIterator - A set of ports to choose from.
        factory - A factory for creating the SocketServer
        Returns:
        null if no port was available or an allocated socket.
      • getWildcardIPAddress

        public static String getWildcardIPAddress()
        Returns the wildcard address to listen on all interfaces.
        Returns:
        Either 0.0.0.0 or :: depending on the IP setup.
      • isValidClientPort

        public static boolean isValidClientPort​(int port)
        Check whether the given port is in right range when connecting to somewhere.
        Parameters:
        port - the port to check
        Returns:
        true if the number in the range 1 to 65535
      • isValidHostPort

        public static boolean isValidHostPort​(int port)
        check whether the given port is in right range when getting port from local system.
        Parameters:
        port - the port to check
        Returns:
        true if the number in the range 0 to 65535