Class KiwiInternetAddresses


  • public class KiwiInternetAddresses
    extends Object
    Utilities for InetAddress and other things related to internet addresses. Note that Google Guava also has the InetAddresses class, so if something is not in here, check there. This set of utilities only contains things that are not in the JDK or Guava.

    Note that some of the methods accept (e.g. as default value) or return KiwiInternetAddresses.SimpleHostInfo. This is because the InetAddress class has no public constructors and can only be instantiated by its static factory methods. As a result, if you want to specify your own custom host name and/or IP address as the default value, for example to use a different host alias or subnet IP, then you must use the methods that work with KiwiInternetAddresses.SimpleHostInfo. Otherwise when working with InetAddress instances, using InetAddress.getLoopbackAddress() is about the only other default value you could use.

    Last, note that KiwiInternetAddresses.SimpleHostInfo only contains the host name and host address as a string.

    • Constructor Detail

      • KiwiInternetAddresses

        public KiwiInternetAddresses()
    • Method Detail

      • getLocalHostInetAddress

        public static Optional<InetAddress> getLocalHostInetAddress()
        Get local host as an optional of InetAddress
        Returns:
        optional containing local host info, or empty if any error occurred
      • getLocalHostInetAddress

        public static InetAddress getLocalHostInetAddress​(InetAddress defaultValue)
        Get local host as a InetAddress.
        Parameters:
        defaultValue - the default value to use if local host address could not be obtained for any reason
        Returns:
        the local host info, or the specified defaultValue if any error occurred
      • getLocalHostInetAddress

        public static InetAddress getLocalHostInetAddress​(Supplier<InetAddress> defaultValueSupplier)
        Get local host as a InetAddress.
        Parameters:
        defaultValueSupplier - supplier for the default value to use if local host address could not be obtained for any reason
        Returns:
        the local host info, or the value obtained from the defaultValueSupplier if any error occurred
      • portFrom

        public static Optional<Integer> portFrom​(String url)
        Returns the port in the given URL, or an empty optional otherwise.
        Parameters:
        url - a URL
        Returns:
        an optional containing the port, or empty
        Throws:
        UncheckedMalformedURLException - if the given string URL is not valid
        See Also:
        URL(String)
      • portFrom

        public static Optional<Integer> portFrom​(URL url)
        Returns the port in the given URL, or an empty optional otherwise.
        Parameters:
        url - a URL
        Returns:
        an optional containing the port, or empty if the URL did not have a port
        See Also:
        URL.getPort()
        Implementation Note:
        The URL.getPort() returns -1 if there is no port. Here we change that to an Optional
      • hostAndPortFrom

        public static com.google.common.net.HostAndPort hostAndPortFrom​(String url)
        Returns a HostAndPort from the given URL.
        Parameters:
        url - a URL as a string
        Returns:
        HostAndPort instance
      • hostAndPortFrom

        public static com.google.common.net.HostAndPort hostAndPortFrom​(URL url)
        Returns a HostAndPort from the given URL.
        Parameters:
        url - a URL
        Returns:
        HostAndPort instance