Class CidrAddress

java.lang.Object
io.smallrye.common.net.CidrAddress
All Implemented Interfaces:
Serializable, Comparable<CidrAddress>

public final class CidrAddress extends Object implements Serializable, Comparable<CidrAddress>
A Classless Inter-Domain Routing address. This is the combination of an IP address and a netmask.
See Also:
  • Field Details

    • INET4_ANY_CIDR

      public static final CidrAddress INET4_ANY_CIDR
      The CIDR address representing all IPv4 addresses.
    • INET6_ANY_CIDR

      public static final CidrAddress INET6_ANY_CIDR
      The CIDR address representing all IPv6 addresses.
  • Method Details

    • create

      public static CidrAddress create(InetAddress networkAddress, int netmaskBits)
      Create a new CIDR address.
      Parameters:
      networkAddress - the network address (must not be null)
      netmaskBits - the netmask bits (0-32 for IPv4, or 0-128 for IPv6)
      Returns:
      the CIDR address (not null)
    • create

      public static CidrAddress create(byte[] addressBytes, int netmaskBits)
      Create a new CIDR address.
      Parameters:
      addressBytes - the network address bytes (must not be null, must be 4 bytes for IPv4 or 16 bytes for IPv6)
      netmaskBits - the netmask bits (0-32 for IPv4, or 0-128 for IPv6)
      Returns:
      the CIDR address (not null)
    • matches

      public boolean matches(InetAddress address)
      Determine if this CIDR address matches the given address.
      Parameters:
      address - the address to test
      Returns:
      true if the address matches, false otherwise
    • matches

      public boolean matches(byte[] bytes)
      Determine if this CIDR address matches the given address bytes.
      Parameters:
      bytes - the address bytes to test
      Returns:
      true if the address bytes match, false otherwise
    • matches

      public boolean matches(byte[] bytes, int scopeId)
      Determine if this CIDR address matches the given address bytes.
      Parameters:
      bytes - the address bytes to test
      scopeId - the scope ID, or 0 to match no scope
      Returns:
      true if the address bytes match, false otherwise
    • matches

      public boolean matches(Inet4Address address)
      Determine if this CIDR address matches the given address.
      Parameters:
      address - the address to test
      Returns:
      true if the address matches, false otherwise
    • matches

      public boolean matches(Inet6Address address)
      Determine if this CIDR address matches the given address.
      Parameters:
      address - the address to test
      Returns:
      true if the address matches, false otherwise
    • matches

      public boolean matches(CidrAddress address)
      Determine if this CIDR address matches the given CIDR address. This will be true only when the given CIDR block is wholly enclosed by this one.
      Parameters:
      address - the address to test
      Returns:
      true if the given block is enclosed by this one, false otherwise
    • getNetworkAddress

      public InetAddress getNetworkAddress()
      Get the network address. The returned address has a resolved name consisting of the most compact valid string representation of the network of this CIDR address.
      Returns:
      the network address (not null)
    • getBroadcastAddress

      public Inet4Address getBroadcastAddress()
      Get the broadcast address for this CIDR block. If the block has no broadcast address (either because it is IPv6 or it is too small) then null is returned.
      Returns:
      the broadcast address for this CIDR block, or null if there is none
    • getNetmaskBits

      public int getNetmaskBits()
      Get the netmask bits. This will be in the range 0-32 for IPv4 addresses, and 0-128 for IPv6 addresses.
      Returns:
      the netmask bits
    • getScopeId

      public int getScopeId()
      Get the match address scope ID (if it is an IPv6 address).
      Returns:
      the scope ID, or 0 if there is none or the address is an IPv4 address
    • compareTo

      public int compareTo(CidrAddress other)
      Specified by:
      compareTo in interface Comparable<CidrAddress>
    • compareAddressBytesTo

      public int compareAddressBytesTo(byte[] otherBytes, int otherNetmaskBits, int scopeId)
      Compare the bytes of this address to the bytes in the given array.
      Parameters:
      otherBytes - the bytes to compare to (must not be null)
      otherNetmaskBits - the netmask bits for the byte array
      scopeId - the IPv6 scope ID, if any, or else zero
      Returns:
      -1, 0, or 1 if this address is less than, equal to, or greater than the given address
    • equals

      public boolean equals(Object obj)
      Returns true if this address is equal to the given object, or false if it is not.
      Overrides:
      equals in class Object
      Parameters:
      obj - the other address
      Returns:
      true if this address is equal to the given object, or false if it is not
    • equals

      public boolean equals(CidrAddress obj)
      Returns true if this address is equal to the given address, or false if it is not.
      Parameters:
      obj - the other address
      Returns:
      true if this address is equal to the given address, or false if it is not
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object