Class IPv6Address

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<IPv6Address>

    public final class IPv6Address
    extends java.lang.Object
    implements java.lang.Comparable<IPv6Address>, java.io.Serializable
    Immutable representation of an IPv6 address.
    See Also:
    Serialized Form
    • Constructor Detail

      • IPv6Address

        IPv6Address​(long highBits,
                    long lowBits)
    • Method Detail

      • fromLongs

        public static IPv6Address fromLongs​(long highBits,
                                            long lowBits)
        Construct an IPv6Address from two longs representing the 64 highest and 64 lowest bits. It is usually easier to construct IPv6Addresses from a String or an InetAddress. The internal representation of an IPv6Address is exactly these two longs though, so if you already happen to have them, this provides a very efficient way to construct an IPv6Address.
        Parameters:
        highBits - highest order bits
        lowBits - lowest order bits
        Returns:
        ipv6 address constructed from two longs
      • fromString

        public static IPv6Address fromString​(java.lang.String string)
        Create an IPv6 address from its String representation. For example "1234:5678:abcd:0000:9876:3210:ffff:ffff" or "2001::ff" or even "::". IPv4-Mapped IPv6 addresses such as "::ffff:123.456.123.456" are also supported.
        Parameters:
        string - string representation
        Returns:
        IPv6 address
      • tryParseStringArrayIntoLongArray

        private static long[] tryParseStringArrayIntoLongArray​(java.lang.String string,
                                                               java.lang.String longNotation)
      • fromInetAddress

        public static IPv6Address fromInetAddress​(java.net.InetAddress inetAddress)
        Create an IPv6 address from a java.net.Inet6Address.
        Parameters:
        inetAddress - Inet6Address representation
        Returns:
        IPv6 address
      • toInetAddress

        public java.net.InetAddress toInetAddress()
                                           throws java.net.UnknownHostException
        Throws:
        java.net.UnknownHostException
      • fromByteArray

        public static IPv6Address fromByteArray​(byte[] bytes)
        Create an IPv6 address from a byte array.
        Parameters:
        bytes - byte array with 16 bytes (interpreted unsigned)
        Returns:
        IPv6 address
      • toByteArray

        public byte[] toByteArray()
        Returns:
        byte[] representation
      • fromBigInteger

        public static IPv6Address fromBigInteger​(java.math.BigInteger bigInteger)
        Create an IPv6 address from a (positive) BigInteger. The magnitude of the BigInteger represents the IPv6 address value. Or in other words, the BigInteger with value N defines the Nth possible IPv6 address.
        Parameters:
        bigInteger - BigInteger value
        Returns:
        IPv6 address
      • toBigInteger

        public java.math.BigInteger toBigInteger()
        Returns:
        BigInteger representation. The magnitude of the BigInteger represents the IPv6 address value. Or in other words, the BigInteger with value N defines the Nth possible IPv6 address.
      • add

        public IPv6Address add​(int value)
        Addition. Will never overflow, but wraps around when the highest ip address has been reached.
        Parameters:
        value - value to add
        Returns:
        new IPv6 address
      • subtract

        public IPv6Address subtract​(int value)
        Subtraction. Will never underflow, but wraps around when the lowest ip address has been reached.
        Parameters:
        value - value to substract
        Returns:
        new IPv6 address
      • maskWithNetworkMask

        public IPv6Address maskWithNetworkMask​(IPv6NetworkMask networkMask)
        Mask the address with the given network mask.
        Parameters:
        networkMask - network mask
        Returns:
        an address of which the last 128 - networkMask.asPrefixLength() bits are zero
      • maximumAddressWithNetworkMask

        public IPv6Address maximumAddressWithNetworkMask​(IPv6NetworkMask networkMask)
        Calculate the maximum address with the given network mask.
        Parameters:
        networkMask - network mask
        Returns:
        an address of which the last 128 - networkMask.asPrefixLength() bits are one
      • setBit

        public IPv6Address setBit​(int bit)
        Set a bit in the address.
        Parameters:
        bit - to set (in the range [0, 127])
        Returns:
        an address with the given bit set
      • isIPv4Mapped

        public boolean isIPv4Mapped()
        Returns true if the address is an IPv4-mapped IPv6 address. In these addresses, the first 80 bits are zero, the next 16 bits are one, and the remaining 32 bits are the IPv4 address.
        Returns:
        true if the address is an IPv4-mapped IPv6 addresses.
      • isMulticast

        public boolean isMulticast()
        Returns:
        true if the address is an IPv6 multicast address (an address in the network ff00::/8)
      • isSiteLocal

        public boolean isSiteLocal()
        Returns:
        true if the address is an IPv6 site-local address (an address in the network fec0::/48)
      • isLinkLocal

        public boolean isLinkLocal()
        Returns:
        true if the address is an IPv6 link-local address (an address in the network fe80::/64)
      • toString

        public java.lang.String toString()
        Returns a string representation of the IPv6 address. It will use shorthand notation and special notation for IPv4-mapped IPv6 addresses whenever possible.
        Overrides:
        toString in class java.lang.Object
        Returns:
        String representation of the IPv6 address
      • toIPv4MappedAddressString

        private java.lang.String toIPv4MappedAddressString()
      • toShortHandNotationString

        private java.lang.String toShortHandNotationString()
      • toArrayOfShortStrings

        private java.lang.String[] toArrayOfShortStrings()
      • toLongString

        public java.lang.String toLongString()
        Returns:
        String representation of the IPv6 address, never using shorthand notation.
      • toArrayOfZeroPaddedstrings

        private java.lang.String[] toArrayOfZeroPaddedstrings()
      • toShortArray

        private short[] toShortArray()
      • startAndLengthOfLongestRunOfZeroes

        int[] startAndLengthOfLongestRunOfZeroes()
      • countConsecutiveZeroes

        private int countConsecutiveZeroes​(short[] shorts,
                                           int offset)
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

        public int compareTo​(IPv6Address that)
        Specified by:
        compareTo in interface java.lang.Comparable<IPv6Address>
      • getHighBits

        public long getHighBits()
      • getLowBits

        public long getLowBits()
      • numberOfTrailingZeroes

        public int numberOfTrailingZeroes()
      • numberOfTrailingOnes

        public int numberOfTrailingOnes()
      • numberOfLeadingZeroes

        public int numberOfLeadingZeroes()
      • numberOfLeadingOnes

        public int numberOfLeadingOnes()