Class IpAddress

  • All Implemented Interfaces:
    java.lang.Comparable<IpAddress>
    Direct Known Subclasses:
    Ip4Address, Ip6Address

    public class IpAddress
    extends java.lang.Object
    implements java.lang.Comparable<IpAddress>
    A class representing an IP address. This class is immutable.
    • Constructor Detail

      • IpAddress

        protected IpAddress​(IpAddress.Version version,
                            byte[] value)
        Constructor for given IP address version and address octets.
        Parameters:
        version - the IP address version
        value - the IP address value stored in network byte order (i.e., the most significant byte first)
        Throws:
        java.lang.IllegalArgumentException - if the arguments are invalid
      • IpAddress

        protected IpAddress()
        Default constructor for Kryo serialization.
    • Method Detail

      • version

        public IpAddress.Version version()
        Returns the IP version of this address.
        Returns:
        the version
      • isIp4

        public boolean isIp4()
        Tests whether the IP version of this address is IPv4.
        Returns:
        true if the IP version of this address is IPv4, otherwise false.
      • isIp6

        public boolean isIp6()
        Tests whether the IP version of this address is IPv6.
        Returns:
        true if the IP version of this address is IPv6, otherwise false.
      • getIp4Address

        public Ip4Address getIp4Address()
        Gets the Ip4Address view of the IP address.
        Returns:
        the Ip4Address view of the IP address if it is IPv4, otherwise null
      • getIp6Address

        public Ip6Address getIp6Address()
        Gets the Ip6Address view of the IP address.
        Returns:
        the Ip6Address view of the IP address if it is IPv6, otherwise null
      • toOctets

        public byte[] toOctets()
        Returns the IP address as a byte array.
        Returns:
        a byte array
      • toInetAddress

        public java.net.InetAddress toInetAddress()
        Returns the IP address as InetAddress.
        Returns:
        InetAddress
      • byteLength

        public static int byteLength​(IpAddress.Version version)
        Computes the IP address byte length for a given IP version.
        Parameters:
        version - the IP version
        Returns:
        the IP address byte length for the IP version
        Throws:
        java.lang.IllegalArgumentException - if the IP version is invalid
      • valueOf

        public static IpAddress valueOf​(int value)
        Converts an integer into an IPv4 address.
        Parameters:
        value - an integer representing an IPv4 address value
        Returns:
        an IP address
      • valueOf

        public static IpAddress valueOf​(IpAddress.Version version,
                                        byte[] value)
        Converts a byte array into an IP address.
        Parameters:
        version - the IP address version
        value - the IP address value stored in network byte order (i.e., the most significant byte first)
        Returns:
        an IP address
        Throws:
        java.lang.IllegalArgumentException - if the arguments are invalid
      • valueOf

        public static IpAddress valueOf​(IpAddress.Version version,
                                        byte[] value,
                                        int offset)
        Converts a byte array and a given offset from the beginning of the array into an IP address.

        The IP address is stored in network byte order (i.e., the most significant byte first).

        Parameters:
        version - the IP address version
        value - the value to use
        offset - the offset in bytes from the beginning of the byte array
        Returns:
        an IP address
        Throws:
        java.lang.IllegalArgumentException - if the arguments are invalid
      • valueOf

        public static IpAddress valueOf​(java.net.InetAddress inetAddress)
        Converts an InetAddress into an IP address.
        Parameters:
        inetAddress - the InetAddress value to use
        Returns:
        an IP address
        Throws:
        java.lang.IllegalArgumentException - if the argument is invalid
      • valueOf

        public static IpAddress valueOf​(java.lang.String value)
        Converts an IPv4 or IPv6 string literal (e.g., "10.2.3.4" or "1111:2222::8888") into an IP address.
        Parameters:
        value - an IP address value in string form
        Returns:
        an IP address
        Throws:
        java.lang.IllegalArgumentException - if the argument is invalid
      • makeMaskPrefix

        public static IpAddress makeMaskPrefix​(IpAddress.Version version,
                                               int prefixLength)
        Creates an IP network mask prefix.
        Parameters:
        version - the IP address version
        prefixLength - the length of the mask prefix. Must be in the interval [0, 32] for IPv4, or [0, 128] for IPv6
        Returns:
        a new IP address that contains a mask prefix of the specified length
        Throws:
        java.lang.IllegalArgumentException - if the arguments are invalid
      • makeMaskedAddress

        public static IpAddress makeMaskedAddress​(IpAddress address,
                                                  int prefixLength)
        Creates an IP address by masking it with a network mask of given mask length.
        Parameters:
        address - the address to mask
        prefixLength - the length of the mask prefix. Must be in the interval [0, 32] for IPv4, or [0, 128] for IPv6
        Returns:
        a new IP address that is masked with a mask prefix of the specified length
        Throws:
        java.lang.IllegalArgumentException - if the prefix length is invalid
      • isZero

        public boolean isZero()
        Check if this IP address is zero.
        Returns:
        true if this address is zero
      • isSelfAssigned

        public boolean isSelfAssigned()
        Check if this IP address is self-assigned.
        Returns:
        true if this address is self-assigned
      • isMulticast

        public boolean isMulticast()
        Check if this IP address is a multicast address.
        Returns:
        true if this address is a multicast address
      • isLinkLocal

        public boolean isLinkLocal()
        Check if this IP address is a link-local address.
        Returns:
        true if this address is a link-local address
      • compareTo

        public int compareTo​(IpAddress o)
        Specified by:
        compareTo in interface java.lang.Comparable<IpAddress>
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toIpPrefix

        public IpPrefix toIpPrefix()
        Generates an IP prefix.
        Returns:
        the IP prefix of the IP address