Class Subnet


  • public class Subnet
    extends java.lang.Object
    A IP subnet using the CIDR notation. Currently, only IP version 4 address are supported.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int BYTE_MASK  
      private static int IP_MASK_V4  
      private static long IP_MASK_V6  
      private java.net.InetAddress subnet  
      private int subnetInt
      An int representation of a subnet for IPV4 addresses
      private long subnetLong
      An long representation of a subnet for IPV6 addresses
      private long subnetMask  
      private int suffix  
    • Constructor Summary

      Constructors 
      Constructor Description
      Subnet​(java.net.InetAddress subnet, int mask)
      Creates a subnet from CIDR notation.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)  
      boolean inSubnet​(java.net.InetAddress address)
      Checks if the InetAddress is within this subnet
      private int toInt​(java.net.InetAddress inetAddress)
      Converts an IP address into an integer
      private long toLong​(java.net.InetAddress inetAddress)
      Converts an IP address into a long
      java.lang.String toString()  
      private long toSubnet​(java.net.InetAddress address)
      Converts an IP address to a subnet using the provided mask
      • Methods inherited from class java.lang.Object

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

      • subnet

        private java.net.InetAddress subnet
      • subnetInt

        private int subnetInt
        An int representation of a subnet for IPV4 addresses
      • subnetLong

        private long subnetLong
        An long representation of a subnet for IPV6 addresses
      • subnetMask

        private long subnetMask
      • suffix

        private int suffix
    • Constructor Detail

      • Subnet

        public Subnet​(java.net.InetAddress subnet,
                      int mask)
        Creates a subnet from CIDR notation. For example, the subnet 192.168.0.0/24 would be created using the InetAddress 192.168.0.0 and the mask 24.
        Parameters:
        subnet - The InetAddress of the subnet
        mask - The mask
    • Method Detail

      • toInt

        private int toInt​(java.net.InetAddress inetAddress)
        Converts an IP address into an integer
      • toLong

        private long toLong​(java.net.InetAddress inetAddress)
        Converts an IP address into a long
      • toSubnet

        private long toSubnet​(java.net.InetAddress address)
        Converts an IP address to a subnet using the provided mask
        Parameters:
        address - The address to convert into a subnet
        Returns:
        The subnet as an integer
      • inSubnet

        public boolean inSubnet​(java.net.InetAddress address)
        Checks if the InetAddress is within this subnet
        Parameters:
        address - The InetAddress to check
        Returns:
        True if the address is within this subnet, false otherwise
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        See Also:
        Object.toString()
      • equals

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