Class SubnetUtils

    • Field Detail

      • ADDRESS_PATTERN

        private static final java.util.regex.Pattern ADDRESS_PATTERN
      • CIDR_PATTERN

        private static final java.util.regex.Pattern CIDR_PATTERN
      • address

        private final int address
      • broadcast

        private final int broadcast
      • inclusiveHostCount

        private boolean inclusiveHostCount
        Whether the broadcast/network address are included in host count
      • netmask

        private final int netmask
      • network

        private final int network
    • Constructor Detail

      • SubnetUtils

        public SubnetUtils​(java.lang.String cidrNotation)
        Constructs an instance from a CIDR-notation string, e.g. "192.168.0.1/16"
        Parameters:
        cidrNotation - A CIDR-notation string, e.g. "192.168.0.1/16"
        Throws:
        java.lang.IllegalArgumentException - if the parameter is invalid, i.e. does not match n.n.n.n/m where n=1-3 decimal digits, m = 1-2 decimal digits in range 0-32
      • SubnetUtils

        public SubnetUtils​(java.lang.String address,
                           java.lang.String mask)
        Constructs an instance from a dotted decimal address and a dotted decimal mask.
        Parameters:
        address - An IP address, e.g. "192.168.0.1"
        mask - A dotted decimal netmask e.g. "255.255.0.0"
        Throws:
        java.lang.IllegalArgumentException - if the address or mask is invalid, i.e. does not match n.n.n.n where n=1-3 decimal digits and the mask is not all zeros
    • Method Detail

      • format

        private static java.lang.String format​(int[] octets)
        Converts a 4-element array into dotted decimal format.
      • matchAddress

        private static int matchAddress​(java.util.regex.Matcher matcher)
        Extracts the components of a dotted decimal address and pack into an integer using a regex match
      • rangeCheck

        private static int rangeCheck​(int value,
                                      int begin,
                                      int end)
        Checks integer boundaries. Checks if a value x is in the range [begin,end]. Returns x if it is in range, throws an exception otherwise.
      • toArray4

        private static int[] toArray4​(int val)
        Converts a packed integer address into a 4-element array
      • toInteger

        private static int toInteger​(java.lang.String address)
        Converts a dotted decimal format address to a packed integer format.
      • getNext

        public SubnetUtils getNext()
        Gets the next subnet for this instance.
        Returns:
        the next subnet for this instance.
      • getPrevious

        public SubnetUtils getPrevious()
        Gets the previous subnet for this instance.
        Returns:
        the next previous for this instance.
      • isInclusiveHostCount

        public boolean isInclusiveHostCount()
        Tests if the return value of SubnetUtils.SubnetInfo.getAddressCount() includes the network and broadcast addresses.
        Returns:
        true if the host count includes the network and broadcast addresses
        Since:
        2.2
      • setInclusiveHostCount

        public void setInclusiveHostCount​(boolean inclusiveHostCount)
        Sets to true if you want the return value of SubnetUtils.SubnetInfo.getAddressCount() to include the network and broadcast addresses. This also applies to SubnetUtils.SubnetInfo.isInRange(int)
        Parameters:
        inclusiveHostCount - true if network and broadcast addresses are to be included
        Since:
        2.2
      • toString

        public java.lang.String toString()
        Converts this instance to a debug String.
        Overrides:
        toString in class java.lang.Object
        Returns:
        this instance to a debug String.
        Since:
        3.11.0