Class Ip4Address

  • All Implemented Interfaces:
    java.lang.Comparable<IpAddress>

    public final class Ip4Address
    extends IpAddress
    A class representing an IPv4 address. This class is immutable.
    • Method Detail

      • toInt

        public int toInt()
        Returns the integer value of this IPv4 address.
        Returns:
        the IPv4 address's value as an integer
      • valueOf

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

        public static Ip4Address valueOf​(byte[] value)
        Converts a byte array into an IPv4 address.
        Parameters:
        value - the IPv4 address value stored in network byte order (i.e., the most significant byte first)
        Returns:
        an IPv4 address
        Throws:
        java.lang.IllegalArgumentException - if the argument is invalid
      • valueOf

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

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

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

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

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

        public static Ip4Address makeMaskPrefix​(int prefixLength)
        Creates an IPv4 network mask prefix.
        Parameters:
        prefixLength - the length of the mask prefix. Must be in the interval [0, 32]
        Returns:
        a new IPv4 address that contains a mask prefix of the specified length
        Throws:
        java.lang.IllegalArgumentException - if the argument is invalid
      • makeMaskedAddress

        public static Ip4Address makeMaskedAddress​(Ip4Address address,
                                                   int prefixLength)
        Creates an IPv4 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]
        Returns:
        a new IPv4 address that is masked with a mask prefix of the specified length
        Throws:
        java.lang.IllegalArgumentException - if the prefix length is invalid