Class NetworkUtil


  • public class NetworkUtil
    extends java.lang.Object
    Author:
    Kir
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static int getFreePort​(int i)  
      static java.lang.String getHostAddressWithoutScope​(java.net.Inet6Address ipv6Address)
      Returns host address without scope id from provided IPv6 address.
      static java.net.InetAddress[] getSelfAddresses()
      static java.net.InetAddress[] getSelfAddresses​(Filter<java.net.InetAddress> filter)
      Returns all self addresses accepted by provided filter.
      static java.lang.String hostAndPort​(java.lang.String ipOrHost, java.lang.Integer portNumber)
      Forms a textual representation of a host:port address considering special ":" meaning in IPv6
      static boolean isLocalHost​(java.lang.String hostname)
      Checks whether supplied hostname belongs to the current host
      static boolean isPortBusy​(int port)  
      static boolean isPortBusy​(int port, boolean showThreadDump)  
      static boolean isPortBusy​(java.lang.String host, int port, int connectTimeoutMsecs)  
      static boolean isPortBusy​(java.lang.String host, int port, int connectTimeoutMsecs, boolean showThreadDump)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getSelfAddresses

        @NotNull
        public static java.net.InetAddress[] getSelfAddresses()
                                                       throws java.net.SocketException
        Returns:
        all IPv4 addresses on the current machine.
        Throws:
        java.net.SocketException - if socket exception occurs
      • getSelfAddresses

        @NotNull
        public static java.net.InetAddress[] getSelfAddresses​(@Nullable
                                                              Filter<java.net.InetAddress> filter)
                                                       throws java.net.SocketException
        Returns all self addresses accepted by provided filter. IPv6 link local addresses are always ignored. Loopback address is ignored, unless it's the only one available address on current machine.
        Parameters:
        filter - address filter or null, if all addresses must be returned
        Returns:
        all addresses matched by filter
        Throws:
        java.net.SocketException - if socket exception occurs
      • isPortBusy

        public static boolean isPortBusy​(int port)
      • isPortBusy

        public static boolean isPortBusy​(int port,
                                         boolean showThreadDump)
      • isPortBusy

        public static boolean isPortBusy​(java.lang.String host,
                                         int port,
                                         int connectTimeoutMsecs)
      • isPortBusy

        public static boolean isPortBusy​(java.lang.String host,
                                         int port,
                                         int connectTimeoutMsecs,
                                         boolean showThreadDump)
      • getFreePort

        public static int getFreePort​(int i)
      • getHostAddressWithoutScope

        @NotNull
        public static java.lang.String getHostAddressWithoutScope​(@NotNull
                                                                  java.net.Inet6Address ipv6Address)
        Returns host address without scope id from provided IPv6 address. Scope id is required locally, but must not be used by client establishing connection to some IPv6 address.
        Parameters:
        ipv6Address - IPv6 address
        Returns:
        host address without scope id
      • isLocalHost

        public static boolean isLocalHost​(@NotNull
                                          java.lang.String hostname)
        Checks whether supplied hostname belongs to the current host
        Parameters:
        hostname - the host name to check
        Returns:
        true, if hostname belongs to calling machine and false otherwise
      • hostAndPort

        public static java.lang.String hostAndPort​(@NotNull
                                                   java.lang.String ipOrHost,
                                                   @Nullable
                                                   java.lang.Integer portNumber)
        Forms a textual representation of a host:port address considering special ":" meaning in IPv6
        Since:
        2018.1