The constructor takes a string as an argument which represents a netmask, as per the CIDR notation -- whether this
netmask be IPv4 or IPv6. It then extracts the network address (before the /) and the CIDR prefix (after the /), and
tells through the #matches() method whether a candidate InetAddress object fits in the recorded range.
As byte arrays as returned by InetAddress.getByName() are always in network byte order, finding a match
is therefore as simple as testing whether the n first bits (where n is the CIDR) are the same in both byte arrays
(the one of the network address and the one of the candidate address). We do that by first doing byte comparisons,
then testing the last bits if any (that is, if the remainder of the integer division of the CIDR by 8 is not 0).
As a bonus, if no '/' is found in the input, it is assumed that an exact address match is required.
| Modifier and Type | Field and Description |
|---|---|
| private final String | expression
The argument to the constructor, used for .toString() |
| private final boolean | foundPort
Should we use the port pattern when matching |
| private final int | lastByteShift
The right shift to apply to the last byte if CIDR % 8 is not 0; if it is 0, this variable is set to 0 |
| private final byte[] | netaddr
The byte array representing the address extracted from the expression |
| private final int | nrBytes
The number of bytes to test for equality (CIDR / 8) |
| private final Pattern | portPattern
The regular expression used to test for the server port (optional). |
| private static final StringManager |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | equals(Object
the reference object with which to compare. o)Overrides java. Indicates whether some other object is "equal to" this one. |
| public int | |
| public boolean | Returns: true on match, false otherwiseThe addr, int java. to testThe port to test port)Test if a given address and port matches this netmask. |
| public boolean | Returns: true on match, false otherwiseThe addr)java. to testTest if a given address matches this netmask. |
| public boolean | Returns: true on match, false otherwiseThe addr, boolean java. to testIndicates, whether we already checked the port checkedPort)Test if a given address matches this netmask. |
| public String |
| expression | back to summary |
|---|---|
| private final String expression The argument to the constructor, used for .toString() | |
| foundPort | back to summary |
|---|---|
| private final boolean foundPort Should we use the port pattern when matching | |
| lastByteShift | back to summary |
|---|---|
| private final int lastByteShift The right shift to apply to the last byte if CIDR % 8 is not 0; if it is 0, this variable is set to 0 | |
| netaddr | back to summary |
|---|---|
| private final byte[] netaddr The byte array representing the address extracted from the expression | |
| nrBytes | back to summary |
|---|---|
| private final int nrBytes The number of bytes to test for equality (CIDR / 8) | |
| portPattern | back to summary |
|---|---|
| private final Pattern portPattern The regular expression used to test for the server port (optional). | |
| sm | back to summary |
|---|---|
| private static final StringManager sm | |
| NetMask | back to summary |
|---|---|
| public NetMask(final String input) Constructor
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object o) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| matches | back to summary |
|---|---|
| public boolean matches(final InetAddress addr, int port) Test if a given address and port matches this netmask.
| |
| matches | back to summary |
|---|---|
| public boolean matches(final InetAddress addr) Test if a given address matches this netmask.
| |
| matches | back to summary |
|---|---|
| public boolean matches(final InetAddress addr, boolean checkedPort) Test if a given address matches this netmask.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |