Class InetAddressRange


  • public final class InetAddressRange
    extends java.lang.Object
    Describes a range of IP addresses specified in CIDR notation.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  InetAddressRange.Builder  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] base  
      private static java.lang.String BITS  
      private byte[] broadcast  
      private static java.lang.String IP4_BYTE  
      private static java.util.regex.Pattern IP4_CIDR  
      private static java.lang.String IP4_DOT_BYTE  
      private static java.lang.String IP4_PREFIX  
      private static java.util.regex.Pattern IP6_CIDR  
      private static java.lang.String IP6_PART  
      private static java.lang.String IP6_WORD  
      private byte[] mask  
      private int networkZoneBits  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private InetAddressRange​(byte[] base, int bits)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] broadcastAddress()
      Retrieves the broadcast address of this range as a MSB-first byte array.
      boolean contains​(byte[] address)
      Tests whether this range contains the given IP address.
      boolean contains​(java.net.InetAddress address)
      Tests whether this range contains the given InetAddress.
      boolean contains​(InetAddressRange other)
      Tests whether this range completely contains a given other range.
      boolean equals​(java.lang.Object obj)  
      byte[] first​(boolean inclusive)
      Retrieves the first address of this range as a MSB-first byte array.
      static InetAddressRange fromCIDR​(java.lang.String cidr)
      Creates an InetAddressRange for a CIDR.
      int hashCode()  
      static boolean isCIDR​(java.lang.String cidr)
      Tests whether a given string is a valid CIDR.
      boolean isIpV4()
      Tells whether this is an IPv4 address range.
      boolean isIpV6()
      Tells whether this is an IPv6 address range.
      byte[] last​(boolean inclusive)
      Retrieves the last address of this range as a MSB-first byte array.
      int networkZoneBits()
      Retrieves the number of bits for the network zone.
      long numberOfAddresses​(boolean inclusive)
      Determines the number of IP addresses in the range.
      boolean overlaps​(InetAddressRange other)
      Tests whether this range overlaps a given other range.
      int subnetBits()
      Retrieves the number of bits for the subnet.
      java.lang.String toString()  
      private java.lang.String toStringIp4()  
      private java.lang.String toStringIp6()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • IP4_CIDR

        private static final java.util.regex.Pattern IP4_CIDR
      • IP6_CIDR

        private static final java.util.regex.Pattern IP6_CIDR
      • base

        private final byte[] base
      • mask

        private final byte[] mask
      • broadcast

        private final byte[] broadcast
      • networkZoneBits

        private final int networkZoneBits
    • Constructor Detail

      • InetAddressRange

        private InetAddressRange​(byte[] base,
                                 int bits)
    • Method Detail

      • fromCIDR

        public static InetAddressRange fromCIDR​(java.lang.String cidr)
        Creates an InetAddressRange for a CIDR.
        Parameters:
        cidr - the CIDR
        Returns:
        an InetAddressRange
        Throws:
        java.lang.IllegalArgumentException - if the cidr cannot be parsed as a CIDR.
      • isCIDR

        public static boolean isCIDR​(java.lang.String cidr)
        Tests whether a given string is a valid CIDR.
        Parameters:
        cidr - the string to test
        Returns:
        true if the string can be parsed as a CIDR; false otherwise
      • isIpV4

        public boolean isIpV4()
        Tells whether this is an IPv4 address range.
        Returns:
        true if this is an IPv4 address range, false otherwise
      • isIpV6

        public boolean isIpV6()
        Tells whether this is an IPv6 address range.
        Returns:
        true if this is an IPv6 address range, false otherwise
      • first

        public byte[] first​(boolean inclusive)
        Retrieves the first address of this range as a MSB-first byte array.

        If subnetBits() <= 1, the address returned is always the zeroth address.

        Parameters:
        inclusive - whether to consider the zeroth address the first.
        Returns:
        the first address of the range
      • last

        public byte[] last​(boolean inclusive)
        Retrieves the last address of this range as a MSB-first byte array.

        If subnetBits() <= 1, the address returned is always the broadcastAddress().

        Parameters:
        inclusive - whether to consider the direct broadcast address the last.
        Returns:
        the last address of the range
      • broadcastAddress

        public byte[] broadcastAddress()
        Retrieves the broadcast address of this range as a MSB-first byte array.
        Returns:
        the broadcast address of the range
      • contains

        public boolean contains​(java.net.InetAddress address)
        Tests whether this range contains the given InetAddress.
        Parameters:
        address - InetAddress to test
        Returns:
        true if the address is in the range; false otherwise
      • contains

        public boolean contains​(byte[] address)
        Tests whether this range contains the given IP address.
        Parameters:
        address - the IP address to test, as an MSB-first byte array
        Returns:
        true if the address is in the range; false otherwise
      • contains

        public boolean contains​(InetAddressRange other)
        Tests whether this range completely contains a given other range.
        Parameters:
        other - InetAddressRange to test
        Returns:
        true if the other range is completely contained in this range; false otherwise
      • overlaps

        public boolean overlaps​(InetAddressRange other)
        Tests whether this range overlaps a given other range.
        Parameters:
        other - InetAddressRange to test
        Returns:
        true if this range overlaps with the other range; false otherwise
      • networkZoneBits

        public int networkZoneBits()
        Retrieves the number of bits for the network zone.
        Returns:
        the number of bits for the network zone
      • subnetBits

        public int subnetBits()
        Retrieves the number of bits for the subnet.
        Returns:
        the number of bits for the subnet
      • numberOfAddresses

        public long numberOfAddresses​(boolean inclusive)
        Determines the number of IP addresses in the range.

        If subnetBits() <= 1, the count always includes the first and last address.

        Parameters:
        inclusive - whether to include the first and last (broadcast) addresses in the count
        Returns:
        the number of addresses in the subnet
      • hashCode

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

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toString

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

        private java.lang.String toStringIp4()
      • toStringIp6

        private java.lang.String toStringIp6()