Package org.kiwiproject.net
Class KiwiInternetAddresses
- java.lang.Object
-
- org.kiwiproject.net.KiwiInternetAddresses
-
public class KiwiInternetAddresses extends Object
Utilities forInetAddressand other things related to internet addresses. Note that Google Guava also has theInetAddressesclass, 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 theInetAddressclass 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 withKiwiInternetAddresses.SimpleHostInfo. Otherwise when working withInetAddressinstances, usingInetAddress.getLoopbackAddress()is about the only other default value you could use.Last, note that
KiwiInternetAddresses.SimpleHostInfoonly contains the host name and host address as a string.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKiwiInternetAddresses.SimpleHostInfoSimple value class encapsulating a host name and IP address
-
Constructor Summary
Constructors Constructor Description KiwiInternetAddresses()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Optional<InetAddress>getLocalHostInetAddress()Get local host as an optional ofInetAddressstatic InetAddressgetLocalHostInetAddress(InetAddress defaultValue)Get local host as aInetAddress.static InetAddressgetLocalHostInetAddress(Supplier<InetAddress> defaultValueSupplier)Get local host as aInetAddress.static Optional<KiwiInternetAddresses.SimpleHostInfo>getLocalHostInfo()Get local host as an optional ofKiwiInternetAddresses.SimpleHostInfo.static KiwiInternetAddresses.SimpleHostInfogetLocalHostInfo(Supplier<KiwiInternetAddresses.SimpleHostInfo> defaultValueSupplier)Get local host as aKiwiInternetAddresses.SimpleHostInfo.static KiwiInternetAddresses.SimpleHostInfogetLocalHostInfo(KiwiInternetAddresses.SimpleHostInfo defaultValue)Get local host as aKiwiInternetAddresses.SimpleHostInfo.static com.google.common.net.HostAndPorthostAndPortFrom(String url)Returns aHostAndPortfrom the given URL.static com.google.common.net.HostAndPorthostAndPortFrom(URL url)Returns aHostAndPortfrom the given URL.static Optional<Integer>portFrom(String url)Returns the port in the given URL, or an empty optional otherwise.static Optional<Integer>portFrom(URL url)Returns the port in the given URL, or an empty optional otherwise.
-
-
-
Method Detail
-
getLocalHostInfo
public static Optional<KiwiInternetAddresses.SimpleHostInfo> getLocalHostInfo()
Get local host as an optional ofKiwiInternetAddresses.SimpleHostInfo.- Returns:
- optional containing local host info, or empty if any error occurred
-
getLocalHostInetAddress
public static Optional<InetAddress> getLocalHostInetAddress()
Get local host as an optional ofInetAddress- Returns:
- optional containing local host info, or empty if any error occurred
-
getLocalHostInfo
public static KiwiInternetAddresses.SimpleHostInfo getLocalHostInfo(KiwiInternetAddresses.SimpleHostInfo defaultValue)
Get local host as aKiwiInternetAddresses.SimpleHostInfo.- Parameters:
defaultValue- the default value to use if local host could not be obtained for any reason- Returns:
- the local host info, or the specified
defaultValueif any error occurred
-
getLocalHostInetAddress
public static InetAddress getLocalHostInetAddress(InetAddress defaultValue)
Get local host as aInetAddress.- 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
defaultValueif any error occurred
-
getLocalHostInfo
public static KiwiInternetAddresses.SimpleHostInfo getLocalHostInfo(Supplier<KiwiInternetAddresses.SimpleHostInfo> defaultValueSupplier)
Get local host as aKiwiInternetAddresses.SimpleHostInfo.- Parameters:
defaultValueSupplier- supplier for the default value to use if local host could not be obtained for any reason- Returns:
- the local host info, or the value obtained from the
defaultValueSupplierif any error occurred
-
getLocalHostInetAddress
public static InetAddress getLocalHostInetAddress(Supplier<InetAddress> defaultValueSupplier)
Get local host as aInetAddress.- 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
defaultValueSupplierif 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-1if there is no port. Here we change that to anOptional
-
hostAndPortFrom
public static com.google.common.net.HostAndPort hostAndPortFrom(String url)
Returns aHostAndPortfrom 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 aHostAndPortfrom the given URL.- Parameters:
url- a URL- Returns:
- HostAndPort instance
-
-