Class IPRange


  • public class IPRange
    extends Object
    Represents a range of IP addresses.
    • Field Detail

      • addressLength

        private final int addressLength
        Number of bits within the address. 32 bits for IPv4 address, 128 bits for IPv6 addresses.
      • network

        @Nonnull
        private final BitSet network
        The IP network address for the range.
      • host

        @Nullable
        private final BitSet host
        The IP host address, if a host address rather than a network address was specified.
      • mask

        @Nonnull
        private BitSet mask
        The netmask for the range.
    • Constructor Detail

      • IPRange

        public IPRange​(@Nonnull
                       InetAddress address,
                       int maskSize)
                throws IllegalArgumentException
        Constructor.
        Parameters:
        address - address to base the range on; may be the network address or the address of a host within the network
        maskSize - the number of bits in the netmask
        Throws:
        IllegalArgumentException - if the address or mask are invalid
      • IPRange

        public IPRange​(@Nonnull
                       byte[] address,
                       int maskSize)
                throws IllegalArgumentException
        Constructor.
        Parameters:
        address - address to base the range on; may be the network address or the address of a host within the network
        maskSize - the number of bits in the netmask
        Throws:
        IllegalArgumentException - if the address or mask are invalid
    • Method Detail

      • getNetworkAddress

        @Nullable
        public InetAddress getNetworkAddress()
        Returns the network address corresponding to this range as an InetAddress.
        Returns:
        network address as an InetAddress
      • getHostAddress

        @Nullable
        public InetAddress getHostAddress()
        Returns the host address originally specified for this range, if it was a host address rather than a network address. Returns null if the address specified was a network address.
        Returns:
        host address as an InetAddress, or null
      • validateV6Address

        private static void validateV6Address​(@Nonnull @NotEmpty
                                              String address)
                                       throws IllegalArgumentException
        Validate an IPv6 address for use as the base of a CIDR block.

        Just checks that any non-empty components are valid hexadecimal integers in the right range; leaves most of the hard work to the InetAddress parser.

        Parameters:
        address - the address to validate
        Throws:
        IllegalArgumentException - if expression cannot be parsed
      • contains

        public boolean contains​(@Nonnull
                                InetAddress address)
        Determines whether the given address is contained in the IP range.
        Parameters:
        address - the address to check
        Returns:
        true if the address is in the range, false it not
      • contains

        public boolean contains​(@Nonnull
                                byte[] address)
        Determines whether the given address is contained in the IP range.
        Parameters:
        address - the address to check
        Returns:
        true if the address is in the range, false it not
      • toBitSet

        @Nonnull
        protected BitSet toBitSet​(@Nonnull
                                  byte[] bytes)
        Converts a byte array to a BitSet. The supplied byte array is assumed to have the most significant bit in element 0.
        Parameters:
        bytes - the byte array with most significant bit in element 0.
        Returns:
        the BitSet
      • toByteArray

        @Nonnull
        private byte[] toByteArray​(@Nonnull
                                   BitSet bits)
        Convert a BitSet representing an address into an equivalent array of bytes, sized according to the address length of this IPRange.
        Parameters:
        bits - BitSet representing an address
        Returns:
        array of bytes representing the same address
      • toInetAddress

        @Nullable
        private InetAddress toInetAddress​(@Nullable
                                          BitSet bits)
        Convert a BitSet representing an address into an equivalent InetAddress.

        Returns null for either a null BitSet or for any problems encountered by InetAddress.

        Parameters:
        bits - BitSet representing an address
        Returns:
        InetAddress representing the same address