Package org.apache.sshd.common.net
Class InetAddressRange
- java.lang.Object
-
- org.apache.sshd.common.net.InetAddressRange
-
public final class InetAddressRange extends java.lang.ObjectDescribes a range of IP addresses specified in CIDR notation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classInetAddressRange.Builder
-
Field Summary
Fields Modifier and Type Field Description private byte[]baseprivate static java.lang.StringBITSprivate byte[]broadcastprivate static java.lang.StringIP4_BYTEprivate static java.util.regex.PatternIP4_CIDRprivate static java.lang.StringIP4_DOT_BYTEprivate static java.lang.StringIP4_PREFIXprivate static java.util.regex.PatternIP6_CIDRprivate static java.lang.StringIP6_PARTprivate static java.lang.StringIP6_WORDprivate byte[]maskprivate intnetworkZoneBits
-
Constructor Summary
Constructors Modifier Constructor Description privateInetAddressRange(byte[] base, int bits)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]broadcastAddress()Retrieves the broadcast address of this range as a MSB-first byte array.booleancontains(byte[] address)Tests whether this range contains the given IP address.booleancontains(java.net.InetAddress address)Tests whether this range contains the givenInetAddress.booleancontains(InetAddressRange other)Tests whether this range completely contains a given other range.booleanequals(java.lang.Object obj)byte[]first(boolean inclusive)Retrieves the first address of this range as a MSB-first byte array.static InetAddressRangefromCIDR(java.lang.String cidr)Creates anInetAddressRangefor a CIDR.inthashCode()static booleanisCIDR(java.lang.String cidr)Tests whether a given string is a valid CIDR.booleanisIpV4()Tells whether this is an IPv4 address range.booleanisIpV6()Tells whether this is an IPv6 address range.byte[]last(boolean inclusive)Retrieves the last address of this range as a MSB-first byte array.intnetworkZoneBits()Retrieves the number of bits for the network zone.longnumberOfAddresses(boolean inclusive)Determines the number of IP addresses in the range.booleanoverlaps(InetAddressRange other)Tests whether this range overlaps a given other range.intsubnetBits()Retrieves the number of bits for the subnet.java.lang.StringtoString()private java.lang.StringtoStringIp4()private java.lang.StringtoStringIp6()
-
-
-
Field Detail
-
IP4_BYTE
private static final java.lang.String IP4_BYTE
- See Also:
- Constant Field Values
-
IP4_DOT_BYTE
private static final java.lang.String IP4_DOT_BYTE
- See Also:
- Constant Field Values
-
IP4_PREFIX
private static final java.lang.String IP4_PREFIX
- See Also:
- Constant Field Values
-
BITS
private static final java.lang.String BITS
- See Also:
- Constant Field Values
-
IP4_CIDR
private static final java.util.regex.Pattern IP4_CIDR
-
IP6_WORD
private static final java.lang.String IP6_WORD
- See Also:
- Constant Field Values
-
IP6_PART
private static final java.lang.String IP6_PART
- See Also:
- Constant Field Values
-
IP6_CIDR
private static final java.util.regex.Pattern IP6_CIDR
-
base
private final byte[] base
-
mask
private final byte[] mask
-
broadcast
private final byte[] broadcast
-
networkZoneBits
private final int networkZoneBits
-
-
Method Detail
-
fromCIDR
public static InetAddressRange fromCIDR(java.lang.String cidr)
Creates anInetAddressRangefor a CIDR.- Parameters:
cidr- the CIDR- Returns:
- an
InetAddressRange - Throws:
java.lang.IllegalArgumentException- if thecidrcannot be parsed as a CIDR.
-
isCIDR
public static boolean isCIDR(java.lang.String cidr)
Tests whether a given string is a valid CIDR.- Parameters:
cidr- the string to test- Returns:
trueif the string can be parsed as a CIDR;falseotherwise
-
isIpV4
public boolean isIpV4()
Tells whether this is an IPv4 address range.- Returns:
trueif this is an IPv4 address range,falseotherwise
-
isIpV6
public boolean isIpV6()
Tells whether this is an IPv6 address range.- Returns:
trueif this is an IPv6 address range,falseotherwise
-
first
public byte[] first(boolean inclusive)
Retrieves the first address of this range as a MSB-first byte array.If
subnetBits() <= 1, the address returned is always the zeroth address.- Parameters:
inclusive- whether to consider the zeroth address the first.- Returns:
- the first address of the range
-
last
public byte[] last(boolean inclusive)
Retrieves the last address of this range as a MSB-first byte array.If
subnetBits() <= 1, the address returned is always thebroadcastAddress().- Parameters:
inclusive- whether to consider the direct broadcast address the last.- Returns:
- the last address of the range
-
broadcastAddress
public byte[] broadcastAddress()
Retrieves the broadcast address of this range as a MSB-first byte array.- Returns:
- the broadcast address of the range
-
contains
public boolean contains(java.net.InetAddress address)
Tests whether this range contains the givenInetAddress.- Parameters:
address-InetAddressto test- Returns:
trueif the address is in the range;falseotherwise
-
contains
public boolean contains(byte[] address)
Tests whether this range contains the given IP address.- Parameters:
address- the IP address to test, as an MSB-first byte array- Returns:
trueif the address is in the range;falseotherwise
-
contains
public boolean contains(InetAddressRange other)
Tests whether this range completely contains a given other range.- Parameters:
other-InetAddressRangeto test- Returns:
trueif the other range is completely contained in this range;falseotherwise
-
overlaps
public boolean overlaps(InetAddressRange other)
Tests whether this range overlaps a given other range.- Parameters:
other-InetAddressRangeto test- Returns:
trueif this range overlaps with the other range;falseotherwise
-
networkZoneBits
public int networkZoneBits()
Retrieves the number of bits for the network zone.- Returns:
- the number of bits for the network zone
-
subnetBits
public int subnetBits()
Retrieves the number of bits for the subnet.- Returns:
- the number of bits for the subnet
-
numberOfAddresses
public long numberOfAddresses(boolean inclusive)
Determines the number of IP addresses in the range.If
subnetBits() <= 1, the count always includes the first and last address.- Parameters:
inclusive- whether to include the first and last (broadcast) addresses in the count- Returns:
- the number of addresses in the subnet
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
toStringIp4
private java.lang.String toStringIp4()
-
toStringIp6
private java.lang.String toStringIp6()
-
-