inet.ipaddr

Class IPAddress

  • java.lang.Object
    • inet.ipaddr.IPAddress
    • Field Detail

      • RANGE_SEPARATOR_STR

        public static final java.lang.String RANGE_SEPARATOR_STR
      • SEGMENT_WILDCARD_STR

        public static final java.lang.String SEGMENT_WILDCARD_STR
      • SEGMENT_SQL_WILDCARD_STR

        public static final java.lang.String SEGMENT_SQL_WILDCARD_STR
      • SEGMENT_SQL_SINGLE_WILDCARD

        public static final char SEGMENT_SQL_SINGLE_WILDCARD
        See Also:
        Constant Field Values
      • SEGMENT_SQL_SINGLE_WILDCARD_STR

        public static final java.lang.String SEGMENT_SQL_SINGLE_WILDCARD_STR
    • Method Detail

      • toHostName

        public HostName toHostName()
        If this address was resolved from a host, returns that host. Otherwise, does a reverse name lookup.
      • toCanonicalHostName

        public HostName toCanonicalHostName()
        Does a reverse name lookup to get the canonical host name.
      • from

        public static IPAddress from(byte[] bytes)
      • from

        public static IPAddress from(byte[] bytes,
                                     java.lang.Integer prefixLength)
      • from

        public static IPAddress from(byte[] bytes,
                                     byte[] bytes2,
                                     java.lang.Integer prefixLength)
      • getLocalHost

        public static IPAddress getLocalHost()
                                      throws java.net.UnknownHostException
        Throws:
        java.net.UnknownHostException
      • getStandardLoopbackStrings

        public static java.lang.String[] getStandardLoopbackStrings(IPAddress.IPVersion version)
      • getSection

        public IPAddressSection getSection()
        Returns the address as an address section comprising all segments in the address.
        Returns:
      • getMaxSegmentValue

        public int getMaxSegmentValue()
      • getBytesPerSegment

        public int getBytesPerSegment()
      • getBitsPerSegment

        public int getBitsPerSegment()
      • getByteCount

        public abstract int getByteCount()
      • getSegmentCount

        public abstract int getSegmentCount()
      • getSegmentIndex

        public int getSegmentIndex(java.lang.Integer networkPrefixLength)
      • getByteIndex

        public int getByteIndex(java.lang.Integer networkPrefixLength)
      • getBitCount

        public abstract int getBitCount()
      • isMultipleByNetworkPrefix

        public boolean isMultipleByNetworkPrefix()
      • isMultiple

        public boolean isMultiple()
        Returns:
        whether this address represents more than one address. Such addresses include CIDR/IP addresses (eg 1.2.3.4/11) or wildcard addresses (eg 1.2.*.4) or range addresses (eg 1.2.3-4.5)
      • isPrefixed

        public boolean isPrefixed()
        Returns:
        whether this address represents a network prefix or the set of all addresses with the same network prefix
      • getNetworkPrefixLength

        public java.lang.Integer getNetworkPrefixLength()
      • getCount

        public java.math.BigInteger getCount()
        Gets the count of addresses that this address may represent. If this address is not a CIDR network prefix and it has no range, then there is only one such address.
        Returns:
      • getLower

        public abstract IPAddress getLower()
        If this represents an address with ranging values, returns an address representing the lower values of the range. If this represents an address with a single value in each segment, returns this.
        Returns:
      • getUpper

        public abstract IPAddress getUpper()
        If this represents an address with ranging values, returns an address representing the upper values of the range If this represents an address with a single value in each segment, returns this.
        Returns:
      • iterator

        public abstract java.util.Iterator<? extends IPAddress> iterator()
      • getAddresses

        public abstract java.lang.Iterable<? extends IPAddress> getAddresses()
        Returns:
        an object to iterate over the individual addresses represented by this object.
      • isIPv4

        public boolean isIPv4()
      • isIPv6

        public boolean isIPv6()
      • toIPv6

        public IPv6Address toIPv6()
        Returns:
        If this address is IPv6, or can be converted to IPv6, returns that IPv6Address. Otherwise, returns null.
      • isIPv4Convertible

        public abstract boolean isIPv4Convertible()
        Determines whether this address can be converted to IPv4, if not IPv4 already. Override this method to convert in your own way, or call setAddressConverter with your own converter object. You should also override toIPv4() to match the conversion. This method returns true for all IPv4 addresses.
        Returns:
      • isIPv6Convertible

        public abstract boolean isIPv6Convertible()
        Determines whether an address can be converted to IPv6, if not IPv6 already. Override this method to convert in your own way, or call setAddressConverter with your own converter object. You should also override toIPv6() to match the conversion. This method returns true for all IPv6 addresses.
        Returns:
      • isLinkLocal

        public abstract boolean isLinkLocal()
        See Also:
        InetAddress.isLinkLocalAddress()
      • isSiteLocal

        public abstract boolean isSiteLocal()
        See Also:
        InetAddress.isSiteLocalAddress()
      • isMulticast

        public abstract boolean isMulticast()
        See Also:
        InetAddress.isMulticastAddress()
      • isAnyLocal

        public boolean isAnyLocal()
        See Also:
        InetAddress.isAnyLocalAddress()
      • isLoopback

        public abstract boolean isLoopback()
        See Also:
        InetAddress.isLoopbackAddress()
      • getBytes

        public byte[] getBytes()
        Throws:
        IPAddressTypeException - if this address does not map to a single address. If you want to get subnet bytes or mask bytes, call getLowestBytes
      • getLowestBytes

        public byte[] getLowestBytes()
        Gets the bytes for the lowest address in the range represented by this address.
        Returns:
      • toInetAddress

        public java.net.InetAddress toInetAddress()
        Throws:
        IPAddressTypeException - if this address does not map to a single address, ie it is a subnet
      • isZero

        public boolean isZero()
      • hashCode

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

        public int compareTo(IPAddress other)
        Specified by:
        compareTo in interface java.lang.Comparable<IPAddress>
      • isSameAddress

        public boolean isSameAddress(IPAddress other)
      • equals

        public boolean equals(java.lang.Object o)
        Two IPAddress objects are equal if they represent the same set of addresses. Whether one or the other has an associated network prefix length is not considered. Also, an IPAddressString and IPAddress are considered equal if they represent the same set of addresses.
        Overrides:
        equals in class java.lang.Object
      • contains

        public boolean contains(IPAddress other)
        Parameters:
        other -
        Returns:
        whether this subnet contains the given address
      • subtract

        public abstract IPAddress[] subtract(IPAddress other)
        Subtract the give subnet from this subnet, returning an array of sections for the result (the subnets will not be contiguous so an array is required). Computes the subnet difference, the set of addresses in this address section but not in the provided section.
        Parameters:
        other -
        Returns:
        the difference
        Throws:
        IPAddressTypeException - if the two sections are not comparable
      • from

        public static IPAddress from(java.net.InetAddress inetAddress)
      • getSegmentStrings

        public java.lang.String[] getSegmentStrings()
      • toString

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

        public java.lang.String toCanonicalString()
        This produces a canonical string. RFC 5952 describes canonical representations. http://en.wikipedia.org/wiki/IPv6_address#Recommended_representation_as_text http://tools.ietf.org/html/rfc5952 Each address has a unique canonical string, not counting the prefix, which can give two equal addresses different strings.
      • toFullString

        public java.lang.String toFullString()
        This produces a string with no compressed segments and all segments of full length, which is 4 characters for IPv6 segments and 3 characters for IPv4 segments. Each address has a unique full string, not counting CIDR the prefix, which can give two equal addresses different strings.
      • toNormalizedString

        public java.lang.String toNormalizedString()
        The normalized string returned by this method is consistent with java.net.Inet4Address and java.net.Inet6Address. IPs are not compressed nor mixed in this representation. The string returned by this method is unique for each address, not counting CIDR the prefix, which can give two equal addresses different strings.
      • toCompressedString

        public java.lang.String toCompressedString()
        This produces the shortest valid string for the address. Each address has a unique compressed string, not counting the prefix, which can give two equal addresses different strings. For subnets the string will not have wildcards in host segments (there will be zeros instead), only in network segments.
      • toSubnetString

        public java.lang.String toSubnetString()
        Produces a consistent subnet string that looks like 1.2.*.* or 1:2::/16 In the case of IPv4, this means that wildcards are used instead of a network prefix when a network prefix has been supplied. In the case of IPv6, when a network prefix has been supplied, the prefix will be shown and the host section will be compressed with ::.
      • toNormalizedWildcardString

        public java.lang.String toNormalizedWildcardString()
        This produces a string similar to the normalized string but avoids the CIDR prefix. CIDR addresses will be shown with wildcards and ranges instead of using the CIDR prefix notation.
      • toCanonicalWildcardString

        public java.lang.String toCanonicalWildcardString()
        This produces a string similar to the canonical string but avoids the CIDR prefix. Addresses with a network prefix length will be shown with wildcards and ranges instead of using the CIDR prefix length notation. IPv6 addresses will be compressed according to the canonical representation.
      • toCompressedWildcardString

        public java.lang.String toCompressedWildcardString()
        This is similar to toNormalizedWildcardString, avoiding the CIDR prefix, but with compression as well.
      • toSQLWildcardString

        public java.lang.String toSQLWildcardString()
        This is the same as the string from toNormalizedWildcardString except that it uses IPAddress.SEGMENT_SQL_WILDCARD instead of IPAddress.SEGMENT_WILDCARD and also uses IPAddress.SEGMENT_SQL_SINGLE_WILDCARD
      • toNetworkPrefixLengthString

        public java.lang.String toNetworkPrefixLengthString()
        Returns a string with a CIDR network prefix length if this address has a network prefix length. For IPv6, the host section will be compressed with ::, for IPv4 the host section will be zeros.
        Returns:
      • toConvertedString

        public java.lang.String toConvertedString()
        Returns a mixed string if it represents a convertible IPv4 address, returns the normalized string otherwise.
        Returns:
      • toUNCHostName

        public abstract java.lang.String toUNCHostName()
        Generates the Microsoft UNC path component for this address
        Returns:
      • toReverseDNSLookupString

        public abstract java.lang.String toReverseDNSLookupString()
        Generates the reverse DNS lookup string For 8.255.4.4 it is 4.4.255.8.in-addr.arpa For 2001:db8::567:89ab it is b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa
        Returns:
      • toHexString

        public java.lang.String toHexString(boolean withPrefix)
        Writes this address as hexadecimal, with or without a preceding 0x prefix
      • toNormalizedString

        public java.lang.String toNormalizedString(IPAddressSection.StringOptions params)
        Constructs a string representing this address according to the given parameters
        Parameters:
        params - the parameters for the address string
      • toStandardStrings

        public java.lang.String[] toStandardStrings()
        Returns at most a few dozen string representations: -mixed (1:2:3:4:5:6:1.2.3.4) -full compressions (a:0:b:c:d:0:e:f or a::b:c:d:0:e:f or a:0:b:c:d::e:f) -full leading zeros (000a:0000:000b:000c:000d:0000:000e:000f) -all uppercase and all lowercase (a::a can be A::A) -combinations thereof
        Returns:
      • toAllStrings

        public java.lang.String[] toAllStrings()
        Produces almost all possible string variations

        Use this method with care... a single IPv6 address can have thousands of string representations.

        Examples:

        • "::" has 1297 such variations, but only 9 are considered standard
        • "a:b:c:0:d:e:f:1" has 1920 variations, but only 12 are standard

        Variations included in this method:

        • all standard variations from toStandardStrings()
        • adding a variable number of leading zeros (::a can be ::0a, ::00a, ::000a)
        • choosing any number of zero-segments to compress (:: can be 0:0:0::0:0)
        • mixed representation of all variations (1:2:3:4:5:6:1.2.3.4)
        • all uppercase and all lowercase (a::a can be A::A)
        • all combinations of such variations
        Variations omitted from this method: mixed case of a-f, which you can easily handle yourself with String.equalsIgnoreCase

        Returns:
        the strings
      • toStrings

        public java.lang.String[] toStrings(IPAddressSection.IPStringBuilderOptions options)
        Rather than using toAllStrings or StandardStrings, you can use this method to customize the list of strings produced for this address
      • toAddressString

        public IPAddressString toAddressString()
        Generates an IPAddressString object for this IPAddress object. This same IPAddress object can be retrieved from the resulting IPAddressString object using IPAddressString.getAddress() In general, users are intended to create IPAddress objects from IPAddressString objects, while the reverse direction is generally not all that useful. However, the reverse direction can be useful under certain circumstances. Not all IPAddressString objects can be converted to IPAddress objects, as is the case with IPAddressString objects corresponding to the types IPType.INVALID and IPType.EMPTY. Not all IPAddressString objects can be converted to IPAddress objects without specifying the IP version, as is the case with IPAddressString objects corresponding to the types IPType.PREFIX and IPType.ALL. So in the event you wish to store a collection of IPAddress objects with a collection of IPAddressString objects, and not all the IPAddressString objects can be converted to IPAddress objects, then you may wish to use a collection of only IPAddressString objects, in which case this method is useful.
        Returns:
        an IPAddressString object for this IPAddress.
      • toDelimitedSQLStrs

        public static java.lang.String toDelimitedSQLStrs(java.lang.String[] strs)
      • isRangeEquivalentToPrefix

        public boolean isRangeEquivalentToPrefix()
        Returns:
        whether this address represents more than one address and the set of addresses is determined entirely by the prefix length.
      • getMinPrefix

        public int getMinPrefix()
        Returns the smallest CIDR prefix possible (largest network), such that this address paired with that prefix represents the exact same range of addresses.
        Returns:
        See Also:
        IPAddressDivision.getMaskPrefixLength(boolean)
      • getEquivalentPrefix

        public java.lang.Integer getEquivalentPrefix()
        Returns a prefix length for which the range of this address can be specified only using the address lower value and the prefix length If no such prefix exists, returns null. Examples: 1.2.3.4 returns 32 1.2.*.* returns 16 1.2.*.0/24 returns 16 1.2.*.4 returns null 1.2.252-255.* returns 22 1.2.3.4/x returns x
        Returns:
      • toPrefixedEquivalent

        public IPAddress toPrefixedEquivalent()
        Returns the equivalent CIDR address for which the range of addresses represented is specified using just a single value and a prefix length in the returned section. Otherwise, returns null. If this address represents just a single address, this object is returned. Examples: 1.2.3.4 returns 1.2.3.4/32 1.2.*.* returns 1.2.0.0/16 1.2.*.0/24 returns 1.2.0.0/16 1.2.*.4 returns null 1.2.252-255.* returns 1.2.252.0/22 1.2.3.4/x returns the same address
        Returns:
      • toPrefixedMin

        public IPAddress toPrefixedMin()
        Constructs an equivalent address with the smallest CIDR prefix possible (largest network), such that the address represents the exact same range of addresses.
        Returns:
      • getMaskPrefixLength

        public java.lang.Integer getMaskPrefixLength(boolean network)
        If this address is equivalent to the mask for a CIDR prefix, it returns that prefix length. Otherwise, it returns null. A CIDR network mask is all 1s in the network section and then all 0s in the host section. A CIDR host mask is all 0s in the network section and then all 1s in the host section. The prefix is the length of the network section. Also, keep in mind that the prefix length returned by this method is not equivalent to the prefix length used to construct this object. The prefix length used to construct indicates the network and host portion of this address. The prefix length returned here indicates the whether the value of this address can be used as a mask for the network and host portion of any other address. Therefore the two values can be different values, or one can be null while the other is not.
        Parameters:
        network - whether to check if we are a network mask or a host mask
        Returns:
        the prefix length corresponding to this mask, or null if this address is not a CIDR prefix mask
      • isMaskCompatibleWithRange

        public boolean isMaskCompatibleWithRange(IPAddress mask,
                                                 java.lang.Integer networkPrefixLength)
        Check that the range in each segment resulting from the mask is contiguous, otherwise we cannot represent it. For instance, for the range 0 to 3 (bits are 00 to 11), if we mask all 4 numbers from 0 to 3 with 2 (ie bits are 10), then we are left with 1 and 3. 2 is not included. So we cannot represent 1 and 3 as a contiguous range. The underlying rule is that mask bits that are 0 must be above the resulting range in each segment. Any bit in the mask that is 0 must not fall below any bit in the masked segment range that is different between low and high Any network mask must eliminate each entire segment range. Any host mask is fine.
        Parameters:
        mask -
        networkPrefixLength -
        Returns:
      • toSubnet

        public abstract IPAddress toSubnet(IPAddress mask,
                                           java.lang.Integer networkPrefixLength)
                                    throws IPAddressTypeException
        Creates a subnet address using the given mask. Any existing prefix is removed as the mask is applied to all individual addresses. If networkPrefixLength is non-null, applies that prefix after the mask has been applied. If this represents multiple addresses, and applying the mask to all addresses creates a set of addresses that cannot be represented as a contiguous range, then IPAddressTypeException is thrown. See isMaskCompatibleWithRange(IPAddress, Integer)
        Throws:
        IPAddressTypeException
      • toSubnet

        public abstract IPAddress toSubnet(int networkPrefixLength)
        Creates a subnet address using the given CIDR prefix bits. Since no mask is applied to all of the addresses represented (as with the other toSubnet methods), any existing prefix or range remains the same before applying the additional prefix.
      • getNetworkSection

        public abstract IPAddressSection getNetworkSection(int networkPrefixLength,
                                                           boolean withPrefixLength)
        Generates the network section of the address. The returned section will have only as many segments as needed to hold the network as indicated by networkPrefixLength. If withPrefixLength is true, it will have networkPrefixLength as its associated prefix length, unless this address already has a smaller prefix length, in which case the existing prefix length is retained.
        Parameters:
        networkPrefixLength -
        withPrefixLength - whether the resulting section will have networkPrefixLength as the associated prefix length or not
        Returns:
      • getNetworkSection

        public IPAddressSection getNetworkSection(int networkPrefixLength)
        Generates the network section of the address. The returned section will have only as many segments as needed to hold the network as indicated by networkPrefixLength. It will have networkPrefixLength as its associated prefix length, unless this address already has a smaller prefix length, in which case the existing prefix length is retained.
        Parameters:
        networkPrefixLength -
        Returns:
      • getNetworkSection

        public abstract IPAddressSection getNetworkSection()
        Generates the network section of the address if the address is a CIDR prefix, otherwise it generates the entire address as a prefixed address with prefix matching the address bit length.
        Returns:
      • getHostSection

        public abstract IPAddressSection getHostSection(int networkPrefixLength)
        Generates the host section of the address. The returned section will have only as many segments as needed to hold the host as indicated by cidrBits.
        Parameters:
        networkPrefixLength -
        Returns:
      • getHostSection

        public abstract IPAddressSection getHostSection()
        Generates the host section of the address. The returned section will have only as many segments as needed as determined by the existing CIDR prefix length. If there is no CIDR prefix length, the host section will have 0 segments.
        Returns:
      • toSupernet

        public IPAddress toSupernet(java.lang.Integer prefixLengthDecrement)
        Return an address for the network encompassing this address. The bits indicate the number of additional network bits in the network address in comparison to this address.
        Parameters:
        prefixLengthDecrement - the number to reduce the network bits in order to create a larger network. If null, then this method has the same behaviour as toSupernet()
        Returns:
      • toSupernet

        public IPAddress toSupernet()
        Return an address for the network encompassing this address, with the network portion of the returned address extending to the furthest segment boundary located entirely within but not matching the network portion of this address, unless the network portion has no bits in which case the same address is returned.
        Returns:
        the encompassing network
      • getMatchesSQLClause

        public void getMatchesSQLClause(java.lang.StringBuilder builder,
                                        java.lang.String sqlExpression)
        returns a clause for matching this address. If this address is a subnet, this method will attempt to match every address in the subnet. Therefore it is much more efficient to use getNetworkSection().getStartsWithSQLClause() for a CIDR subnet.
        Parameters:
        builder -
        sqlExpression -
      • getMatchesSQLClause

        public void getMatchesSQLClause(java.lang.StringBuilder builder,
                                        java.lang.String sqlExpression,
                                        IPAddressSQLTranslator translator)
        returns a clause for matching this address. Similar to getMatchesSQLClause(StringBuilder builder, String sqlExpression) but allows you to tailor the SQL produced.
        Parameters:
        builder -
        sqlExpression -
        translator -