Package org.wildfly.common.net
Class CidrAddress
- java.lang.Object
-
- org.wildfly.common.net.CidrAddress
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<CidrAddress>
public final class CidrAddress extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<CidrAddress>
A Classless Inter-Domain Routing address. This is the combination of an IP address and a netmask.- Author:
- David M. Lloyd
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static CidrAddressINET4_ANY_CIDRThe CIDR address representing all IPv4 addresses.static CidrAddressINET6_ANY_CIDRThe CIDR address representing all IPv6 addresses.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareAddressBytesTo(byte[] otherBytes, int otherNetmaskBits, int scopeId)intcompareTo(CidrAddress other)static CidrAddresscreate(byte[] addressBytes, int netmaskBits)Create a new CIDR address.static CidrAddresscreate(java.net.InetAddress networkAddress, int netmaskBits)Create a new CIDR address.booleanequals(java.lang.Object obj)booleanequals(CidrAddress obj)java.net.Inet4AddressgetBroadcastAddress()Get the broadcast address for this CIDR block.intgetNetmaskBits()Get the netmask bits.java.net.InetAddressgetNetworkAddress()Get the network address.intgetScopeId()Get the match address scope ID (if it is an IPv6 address).inthashCode()booleanmatches(byte[] bytes)Determine if this CIDR address matches the given address bytes.booleanmatches(byte[] bytes, int scopeId)Determine if this CIDR address matches the given address bytes.booleanmatches(java.net.Inet4Address address)Determine if this CIDR address matches the given address.booleanmatches(java.net.Inet6Address address)Determine if this CIDR address matches the given address.booleanmatches(java.net.InetAddress address)Determine if this CIDR address matches the given address.booleanmatches(CidrAddress address)Determine if this CIDR address matches the given CIDR address.java.lang.StringtoString()
-
-
-
Field Detail
-
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 Detail
-
create
public static CidrAddress create(java.net.InetAddress networkAddress, int netmaskBits)
Create a new CIDR address.- Parameters:
networkAddress- the network address (must not benull)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 benull, 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(java.net.InetAddress address)
Determine if this CIDR address matches the given address.- Parameters:
address- the address to test- Returns:
trueif the address matches,falseotherwise
-
matches
public boolean matches(byte[] bytes)
Determine if this CIDR address matches the given address bytes.- Parameters:
bytes- the address bytes to test- Returns:
trueif the address bytes match,falseotherwise
-
matches
public boolean matches(byte[] bytes, int scopeId)Determine if this CIDR address matches the given address bytes.- Parameters:
bytes- the address bytes to testscopeId- the scope ID, or 0 to match no scope- Returns:
trueif the address bytes match,falseotherwise
-
matches
public boolean matches(java.net.Inet4Address address)
Determine if this CIDR address matches the given address.- Parameters:
address- the address to test- Returns:
trueif the address matches,falseotherwise
-
matches
public boolean matches(java.net.Inet6Address address)
Determine if this CIDR address matches the given address.- Parameters:
address- the address to test- Returns:
trueif the address matches,falseotherwise
-
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:
trueif the given block is enclosed by this one,falseotherwise
-
getNetworkAddress
public java.net.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 java.net.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) thennullis returned.- Returns:
- the broadcast address for this CIDR block, or
nullif 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:
compareToin interfacejava.lang.Comparable<CidrAddress>
-
compareAddressBytesTo
public int compareAddressBytesTo(byte[] otherBytes, int otherNetmaskBits, int scopeId)
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
equals
public boolean equals(CidrAddress obj)
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-