Class NetUtil


  • public final class NetUtil
    extends Object
    A class that holds a number of network-related constants.

    This class borrowed some of its methods from a modified fork of the Inet6Util class which was part of Apache Harmony.

    • Field Detail

      • LOCALHOST4

        public static final Inet4Address LOCALHOST4
        The Inet4Address that represents the IPv4 loopback address '127.0.0.1'
      • LOCALHOST6

        public static final Inet6Address LOCALHOST6
        The Inet6Address that represents the IPv6 loopback address '::1'
      • SOMAXCONN

        public static final int SOMAXCONN
        The SOMAXCONN value of the current machine. If failed to get the value, 200 is used as a default value for Windows and 128 for others.
    • Method Detail

      • isIpV4StackPreferred

        public static boolean isIpV4StackPreferred()
        Returns true if IPv4 should be used even if the system supports both IPv4 and IPv6. Setting this property to true will disable IPv6 support. The default value of this property is false.
        See Also:
        Java SE networking properties
      • isIpV6AddressesPreferred

        public static boolean isIpV6AddressesPreferred()
        Returns true if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address. The default value of this property is false.
        See Also:
        Java SE networking properties
      • createByteArrayFromIpAddressString

        public static byte[] createByteArrayFromIpAddressString​(String ipAddressString)
        Creates an byte[] based on an ipAddressString. No error handling is performed here.
      • createInetAddressFromIpAddressString

        public static InetAddress createInetAddressFromIpAddressString​(String ipAddressString)
        Creates an InetAddress based on an ipAddressString or might return null if it can't be parsed. No error handling is performed here.
      • intToIpAddress

        public static String intToIpAddress​(int i)
        Converts a 32-bit integer into an IPv4 address.
      • bytesToIpAddress

        public static String bytesToIpAddress​(byte[] bytes)
        Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.
        Throws:
        IllegalArgumentException - if length is not 4 nor 16
      • bytesToIpAddress

        public static String bytesToIpAddress​(byte[] bytes,
                                              int offset,
                                              int length)
        Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.
        Throws:
        IllegalArgumentException - if length is not 4 nor 16
      • isValidIpV6Address

        public static boolean isValidIpV6Address​(String ip)
      • isValidIpV6Address

        public static boolean isValidIpV6Address​(CharSequence ip)
      • isValidIpV4Address

        public static boolean isValidIpV4Address​(CharSequence ip)
        Takes a CharSequence and parses it to see if it is a valid IPV4 address.
        Returns:
        true, if the string represents an IPV4 address in dotted notation, false otherwise
      • isValidIpV4Address

        public static boolean isValidIpV4Address​(String ip)
        Takes a String and parses it to see if it is a valid IPV4 address.
        Returns:
        true, if the string represents an IPV4 address in dotted notation, false otherwise
      • getByName

        public static Inet6Address getByName​(CharSequence ip,
                                             boolean ipv4Mapped)
        Returns the Inet6Address representation of a CharSequence IP address.

        The ipv4Mapped parameter specifies how IPv4 addresses should be treated. "IPv4 mapped" format as defined in rfc 4291 section 2 is supported.

        Parameters:
        ip - CharSequence IP address to be converted to a Inet6Address
        ipv4Mapped -
        • true To allow IPv4 mapped inputs to be translated into Inet6Address
        • false Consider IPv4 mapped addresses as invalid.
        Returns:
        Inet6Address representation of the ip or null if not a valid IP address.
      • toSocketAddressString

        public static String toSocketAddressString​(InetSocketAddress addr)
        Returns the String representation of an InetSocketAddress.

        The output does not include Scope ID.

        Parameters:
        addr - InetSocketAddress to be converted to an address string
        Returns:
        String containing the text-formatted IP address
      • toSocketAddressString

        public static String toSocketAddressString​(String host,
                                                   int port)
        Returns the String representation of a host port combo.
      • toAddressString

        public static String toAddressString​(InetAddress ip,
                                             boolean ipv4Mapped)
        Returns the String representation of an InetAddress.

        The output does not include Scope ID.

        Parameters:
        ip - InetAddress to be converted to an address string
        ipv4Mapped -
        • true to stray from strict rfc 5952 and support the "IPv4 mapped" format defined in rfc 4291 section 2 while still following the updated guidelines in rfc 5952 section 4
        • false to strictly follow rfc 5952
        Returns:
        String containing the text-formatted IP address
      • isFamilySupported

        public static boolean isFamilySupported​(InetAddress address,
                                                ProtocolFamily family)
        Returns true if the ProtocolFamily is supported by the given InetAddress.
        Parameters:
        address - the address.
        family - the family.
        Returns:
        true if supported, false otherwise.
      • isFamilySupported

        public static boolean isFamilySupported​(Class<? extends InetAddress> addressType,
                                                ProtocolFamily family)
        Returns true if the ProtocolFamily is supported by the given address type.
        Parameters:
        addressType - the address type.
        family - the family.
        Returns:
        true if supported, false otherwise.
      • addressType

        public static Class<? extends InetAddress> addressType​(ProtocolFamily family)
        Returns the address type for the given ProtocolFamily.
        Parameters:
        family - the family.
        Returns:
        the address type or null if not supported.
      • localHost

        public static InetAddress localHost​(ProtocolFamily family)
        Returns the localhost InetAddress for this family.
        Parameters:
        family - the family.
        Returns:
        localhost address.