inet.ipaddr

Class IPAddressSection

  • All Implemented Interfaces:
    IPAddressPart, java.io.Serializable, java.lang.Comparable<IPAddressSegmentGrouping>
    Direct Known Subclasses:
    IPv4AddressSection, IPv6AddressSection


    public abstract class IPAddressSection
    extends IPAddressSegmentGrouping
    A section of an IPAddress. It is a series of individual address segments.

    IPAddressSection objects are immutable. Some of the derived state is created upon demand and cached. This also makes them thread-safe. Almost all operations that can be performed on IPAddress objects can also be performed on IPAddressSection objects and vice-versa.

    See Also:
    Serialized Form
    • Method Detail

      • getBitsPerSegment

        public abstract int getBitsPerSegment()
      • getBytesPerSegment

        public abstract int getBytesPerSegment()
      • isIPv4

        public boolean isIPv4()
      • isIPv6

        public boolean isIPv6()
      • getByteIndex

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

        public abstract int getSegmentIndex(java.lang.Integer networkPrefixLength)
      • getNetworkSection

        public abstract IPAddressSection getNetworkSection(int networkPrefixLength,
                                                           boolean withPrefixLength)
      • getNetworkSection

        public abstract IPAddressSection getNetworkSection(int networkPrefixLength)
      • getHostSection

        public abstract IPAddressSection getHostSection(int networkPrefixLength)
      • getMaskPrefixLength

        public java.lang.Integer getMaskPrefixLength(boolean network)
        If this address section is equivalent to the mask for a CIDR prefix, it returns that prefix length. Otherwise, it returns null. A CIDR network mask is an address with all 1s in the network section and then all 0s in the host section. A CIDR host mask is an address with all 0s in the network section and then all 1s in the host section. The prefix length 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. This method applies only to the lower value of the range if this section represents multiple values.
        Parameters:
        network - whether to check for 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
      • getHostBits

        public java.lang.Integer getHostBits()
      • getSegmentPrefixLength

        public static java.lang.Integer getSegmentPrefixLength(int bitsPerSegment,
                                                               java.lang.Integer networkPrefixLength,
                                                               int segmentIndex)
        Across the address prefixes are: IPv6: (null):...:(null):(1 to 16):(0):...:(0) or IPv4: ...(null).(1 to 8).(0)...
      • getSegmentPrefixLength

        public static java.lang.Integer getSegmentPrefixLength(int bitsPerSegment,
                                                               int segmentBits)
        Across the address prefixes are: IPv6: (null):...:(null):(1 to 16):(0):...:(0) or IPv4: ...(null).(1 to 8).(0)...
      • 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:
      • getSegmentCount

        public int getSegmentCount()
      • copySegments

        public void copySegments(int start,
                                 int end,
                                 IPAddressSegment[] segs,
                                 int index)
      • isDecimalSameAsOctal

        public static boolean isDecimalSameAsOctal(boolean checkRange,
                                                   IPAddressPart part)
        Returns:
        whether this section in decimal appears the same as this segment in octal. This is true if all the values lies between 0 and 8 (so the octal and decimal values are the same)
      • contains

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

        public abstract IPAddressSection[] subtract(IPAddressSection 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. Keep in mind this is set subtraction, not subtraction of segment values. We have a subnet of addresses and we are removing some of those addresses.
        Parameters:
        other -
        Returns:
        the difference
        Throws:
        IPAddressTypeException - if the two sections are not comparable
      • toPrefixedEquivalent

        public IPAddressSection toPrefixedEquivalent()
        Returns the CIDR address section for which the range of addresses in this section 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 is returned.
        Returns:
      • toMinimalPrefixed

        public IPAddressSection toMinimalPrefixed()
        Constructs an equivalent address section with the smallest CIDR prefix length possible (largest network), such that the address represents the exact same range of addresses.
        Returns:
      • toSupernet

        public IPAddressSection toSupernet(java.lang.Integer prefixLengthDecrement)
        Return an address section for the network encompassing this address section. The bits indicate the number to reduce the 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 IPAddressSection toSupernet()
        Return an address section for the network encompassing this address section, 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
      • 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 section can be specified only using the section's lower value and the prefix length If no such prefix exists, returns null If this address section represents a single value, returns the bit length
        Returns:
      • getLowerSection

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

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

        public abstract IPAddressSegment[] getLowerSegments()
        If this represents an address section with ranging values, returns an array of address segments representing the lower values of the range If this represents an address section with a single value in each segment, returns an array containing the segments for this section.
        Returns:
      • getUpperSegments

        public abstract IPAddressSegment[] getUpperSegments()
        If this represents an address section with ranging values, returns an an array of address segments representing the upper values of the rang If this represents an address section with a single value in each segment, returns an array containing the segments for this section.
        Returns:
      • getSegments

        public abstract IPAddressSegment[] getSegments()
        Returns:
        an array containing the segments
      • sectionIterator

        public abstract java.util.Iterator<? extends IPAddressSection> sectionIterator()
      • iterator

        public abstract java.util.Iterator<? extends IPAddressSegment[]> iterator()
      • isEntireAddress

        public boolean isEntireAddress()
      • getSegmentStrings

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

        public abstract 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
      • toFullString

        public abstract 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.
      • toNormalizedString

        public abstract 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.
      • toCompressedString

        public abstract java.lang.String toCompressedString()
        This produces the shortest valid string. For subnets the string will not have wildcards in host segments (there will be zeros instead), only in network segments.
      • toNetworkPrefixLengthString

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

        public abstract java.lang.String toSubnetString()
        Produces a consistent subnet string. In the case of IPv4, this means that wildcards are used instead of a network prefix. In the case of IPv6, a prefix will be used and the host section will be compressed with ::.
      • toNormalizedWildcardString

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

        public abstract java.lang.String toCanonicalWildcardString()
        This produces a string similar to the canonical string and 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 abstract java.lang.String toCompressedWildcardString()
        This is similar to toNormalizedWildcardString, avoiding the CIDR prefix, but with compression as well.
      • toSQLWildcardString

        public abstract 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 -it uses IPAddress.SEGMENT_SQL_SINGLE_WILDCARD
      • toHexString

        public java.lang.String toHexString(boolean withPrefix)
        Writes this address as a single hexadecimal value with always the exact same number of characters, with or without a preceding 0x prefix. For IPv4 there are 8 hex characters, for IPv6 there are 32 hex 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.
      • toStandardStringCollection

        public IPAddressPartStringCollection toStandardStringCollection()
        Returns at most a couple dozen string representations: -mixed (1:2:3:4:5:6:1.2.3.4) -upper and lower case -full compressions or no compression (a:0:0:c:d:0:e:f or a::c:d:0:e:f or a:0:b:c:d::e:f) -full leading zeros (000a:0000:000b:000c:000d:0000:000e:000f) -combinations thereof
        Returns:
      • toAllStringCollection

        public IPAddressPartStringCollection toAllStringCollection()
        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 -choosing specific segments for full leading zeros (::a:b can be ::000a:b, ::a:000b, or ::000a:000b) -choosing which zero-segments to compress (0:0:a:: can be ::a:0:0:0:0:0 or 0:0:a::) -mixed representation (1:2:3:4:5:6:1.2.3.4) -all combinations of such variations Variations omitted from this method: -mixed case of a-f, which you can easily handle yourself with String.equalsIgnoreCase -adding a variable number of leading zeros (::a can be ::0a, ::00a, ::000a) -choosing any number of zero-segments anywhere to compress (:: can be 0:0:0::0:0)
        Returns:
      • toDatabaseSearchStringCollection

        public IPAddressPartStringCollection toDatabaseSearchStringCollection()
        Returns a set of strings for search the standard string representations in a database -compress the largest compressible segments or no compression (a:0:0:c:d:0:e:f or a::c:d:0:e:f) -upper/lowercase is not considered because many databases are case-insensitive
        Returns:
      • getParts

        public IPAddressPart[] getParts(IPAddressSection.IPStringBuilderOptions options)
        Get all representations of this address including this IPAddressSection. This includes:
        • alternative segment groupings expressed as IPAddressSegmentGrouping
        • conversions to IPv6, and alternative representations of those IPv6 addresses
        Parameters:
        options -
        Returns:
      • getStartsWithSQLClause

        public void getStartsWithSQLClause(java.lang.StringBuilder builder,
                                           java.lang.String expression)
        This method gives you an SQL clause that allows you to search the database for the front part of an address or addresses in a given network. This is not as simple as it sounds, because the same address can be written in different ways (especially for IPv6) and in addition, addresses in the same network can have different beginnings (eg 1.0.0.0/7 are all addresses from 0.0.0.0 to 1.255.255.255), so you can see they start with both 1 and 0. You can reduce the number of possible beginnings by choosing a segment boundary for the network prefix. The SQL produced works for MySQL. For a different database type, use getStartsWithSQLClause(StringBuilder, String, IPAddressSQLTranslator)
        Parameters:
        builder -
        expression - the expression that must match the condition, whether a column name or other
      • getStartsWithSQLClause

        public void getStartsWithSQLClause(java.lang.StringBuilder builder,
                                           java.lang.String expression,
                                           IPAddressSQLTranslator translator)