inet.ipaddr

Class IPAddress

    • 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)
      • 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.
        Overrides:
        getSection in class Address
        Returns:
      • getMaxSegmentValue

        public int getMaxSegmentValue()
        Specified by:
        getMaxSegmentValue in class Address
        Returns:
        the maximum possible segment value for this type of address. Note this is not the maximum value of the segments in this specific address.
      • getBytesPerSegment

        public int getBytesPerSegment()
      • getBitsPerSegment

        public int getBitsPerSegment()
      • isMultipleByNetworkPrefix

        public boolean isMultipleByNetworkPrefix()
      • getNetworkPrefixLength

        public java.lang.Integer getNetworkPrefixLength()
      • 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.
        Specified by:
        getLower in interface AddressComponent
        Specified by:
        getLower in interface AddressSegmentSeries
        Specified by:
        getLower in class Address
        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.
        Specified by:
        getUpper in interface AddressComponent
        Specified by:
        getUpper in interface AddressSegmentSeries
        Specified by:
        getUpper in class Address
        Returns:
      • reverseBits

        public abstract IPAddress reverseBits(boolean perByte)
        Returns a new IPAddress which has the bits reversed. If this represents a range of values that cannot be reversed, then this throws AddressTypeException. In such cases where isMultiple() is true, call iterator(), getLower(), getUpper() or some other methods to transform the address into an address representing a single value.
        Specified by:
        reverseBits in interface AddressComponent
        Specified by:
        reverseBits in interface AddressSegmentSeries
        Specified by:
        reverseBits in class Address
        Parameters:
        perByte - if true, only the bits in each byte are reversed, if false, then all bits in the address are reversed
        Returns:
      • iterator

        public abstract java.util.Iterator<? extends IPAddress> iterator()
        Description copied from interface: AddressComponent
        An address component can represent a single segment, address, or section, or it can represent multiple, typically a subnet or range of segment, address, or section values. Call AddressItem.isMultiple() to determine if this instance represents multiple. This method iterates through the individual elements.
        Specified by:
        iterator in interface AddressComponent
        Specified by:
        iterator in interface AddressSegmentSeries
        Specified by:
        iterator in class Address
        Returns:
      • 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()
      • isLocal

        public boolean isLocal()
        Specified by:
        isLocal in class Address
      • isAnyLocal

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

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

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

        public boolean isSameAddress(IPAddress other)
      • 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 subnets 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 subnet but not in the provided subnet. If the address is not the same version, the default conversion will be applied, and it that fails, AddressTypeException will be thrown.
        Parameters:
        other -
        Returns:
        the difference
        Throws:
        AddressTypeException - if the two sections are not comparable
      • from

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

        public java.lang.String[] getSegmentStrings()
      • 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.
      • 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
      • toPrefixLengthString

        public java.lang.String toPrefixLengthString()
        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 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:
      • toBinaryString

        public java.lang.String toBinaryString()
        Writes this address as a single binary value with always the exact same number of characters If this section represents a range of values outside of the network prefix length, then this is printed as a range of two hex values.
      • toOctalString

        public java.lang.String toOctalString(boolean with0Prefix)
        Writes this address as a single octal value with always the exact same number of characters, with or without a preceding 0 prefix. If this section represents a range of values outside of the network prefix length, then this is printed as a range of two hex values.
      • toNormalizedString

        public java.lang.String toNormalizedString(IPAddressSection.IPStringOptions 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.
        Overrides:
        toAddressString in class Address
        Returns:
        an IPAddressString object for this IPAddress.
      • toDelimitedSQLStrs

        public static java.lang.String toDelimitedSQLStrs(java.lang.String[] strs)
      • 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. Otherwise, returns null. 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:
      • toMinPrefixedEquivalent

        public IPAddress toMinPrefixedEquivalent()
        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
      • mask

        public abstract IPAddress mask(IPAddress mask)
                                throws AddressTypeException
        Applies the given mask to all addresses represented by this IPAddress. Any existing prefix is removed as the mask is applied to all individual addresses. 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 within each segment, then AddressTypeException is thrown.
        Throws:
        AddressTypeException
      • maskNetwork

        public abstract IPAddress maskNetwork(IPAddress mask,
                                              int networkPrefixLength)
                                       throws AddressTypeException
        Applies the given mask up until the given prefix length to all addresses represented by this IPAddress. Any existing prefix length is removed as the mask is applied to all individual addresses. If networkPrefixLength is non-null, it is applied 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 within each segment, then AddressTypeException is thrown.
        Throws:
        AddressTypeException
      • applyPrefixLength

        public abstract IPAddress applyPrefixLength(int networkPrefixLength)
        Applies the given prefix length to create a new address. If this address has a prefix length that is smaller than the given one, then the method has no effect and simply returns this address.
        Specified by:
        applyPrefixLength in interface AddressSegmentSeries
        Specified by:
        applyPrefixLength in class Address
      • bitwiseOrNetwork

        public abstract IPAddress bitwiseOrNetwork(IPAddress mask,
                                                   int networkPrefixLength)
                                            throws AddressTypeException
        Does the bitwise disjunction with this address. Useful when subnetting. Any existing prefix length is dropped for the new prefix length and the mask is applied up to the end the new prefix length.
        Parameters:
        mask -
        networkPrefixLength - the new prefix length for the address
        Returns:
        Throws:
        AddressTypeException
      • 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 abstract 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:
      • removePrefixLength

        public abstract IPAddress removePrefixLength(boolean zeroed)
      • adjustPrefixLength

        public abstract IPAddress adjustPrefixLength(int adjustment)
        Description copied from interface: AddressSegmentSeries
        Increases or decreases prefix length by the given increment. When prefix length is increased, the bits moved within the prefix become zero. When the prefix is extended beyond the segment series boundary, it is removed.
        Specified by:
        adjustPrefixLength in interface AddressSegmentSeries
        Specified by:
        adjustPrefixLength in class Address
        Returns:
      • setPrefixLength

        public abstract IPAddress setPrefixLength(int prefixLength)
        Description copied from interface: AddressSegmentSeries
        Sets the prefix length. When the series already had a prefix, and the prefix length is increased, the bits moved within the prefix are zero. When the prefix is extended beyond the segment series boundary, it is removed.
        Specified by:
        setPrefixLength in interface AddressSegmentSeries
        Specified by:
        setPrefixLength in class Address
        Returns:
      • setPrefixLength

        public abstract IPAddress setPrefixLength(int prefixLength,
                                                  boolean zeroed)
      • 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 -