IpPrefixpublic final class IpPrefix extends Object implements android.os.ParcelableThis class represents an IP prefix, i.e., a contiguous block of IP addresses aligned on a
power of two boundary (also known as an "IP subnet"). A prefix is specified by two pieces of
information:
- A starting IP address (IPv4 or IPv6). This is the first IP address of the prefix.
- A prefix length. This specifies the length of the prefix by specifing the number of bits
in the IP address, starting from the most significant bit in network byte order, that
are constant for all addresses in the prefix.
For example, the prefix 192.0.2.0/24 covers the 256 IPv4 addresses from
192.0.2.0 to 192.0.2.255 , inclusive, and the prefix
2001:db8:1:2 covers the 2^64 IPv6 addresses from 2001:db8:1:2:: to
2001:db8:1:2:ffff:ffff:ffff:ffff , inclusive.
Objects of this class are immutable. |
Fields Summary |
---|
private final byte[] | address | private final int | prefixLength | public static final Creator | CREATORImplement the Parcelable interface. |
Constructors Summary |
---|
public IpPrefix(byte[] address, int prefixLength)Constructs a new {@code IpPrefix} from a byte array containing an IPv4 or IPv6 address in
network byte order and a prefix length. Silently truncates the address to the prefix length,
so for example {@code 192.0.2.1/24} is silently converted to {@code 192.0.2.0/24}.
this.address = address.clone();
this.prefixLength = prefixLength;
checkAndMaskAddressAndPrefixLength();
| public IpPrefix(InetAddress address, int prefixLength)Constructs a new {@code IpPrefix} from an IPv4 or IPv6 address and a prefix length. Silently
truncates the address to the prefix length, so for example {@code 192.0.2.1/24} is silently
converted to {@code 192.0.2.0/24}.
// We don't reuse the (byte[], int) constructor because it calls clone() on the byte array,
// which is unnecessary because getAddress() already returns a clone.
this.address = address.getAddress();
this.prefixLength = prefixLength;
checkAndMaskAddressAndPrefixLength();
| public IpPrefix(String prefix)Constructs a new IpPrefix from a string such as "192.0.2.1/24" or "2001:db8::1/64".
Silently truncates the address to the prefix length, so for example {@code 192.0.2.1/24}
is silently converted to {@code 192.0.2.0/24}.
// We don't reuse the (InetAddress, int) constructor because "error: call to this must be
// first statement in constructor". We could factor out setting the member variables to an
// init() method, but if we did, then we'd have to make the members non-final, or "error:
// cannot assign a value to final variable address". So we just duplicate the code here.
Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(prefix);
this.address = ipAndMask.first.getAddress();
this.prefixLength = ipAndMask.second;
checkAndMaskAddressAndPrefixLength();
|
Methods Summary |
---|
private void | checkAndMaskAddressAndPrefixLength()
if (address.length != 4 && address.length != 16) {
throw new IllegalArgumentException(
"IpPrefix has " + address.length + " bytes which is neither 4 nor 16");
}
NetworkUtils.maskRawAddress(address, prefixLength);
| public int | describeContents()Implement the Parcelable interface.
return 0;
| public boolean | equals(java.lang.Object obj)Compares this {@code IpPrefix} object against the specified object in {@code obj}. Two
objects are equal if they have the same startAddress and prefixLength.
if (!(obj instanceof IpPrefix)) {
return false;
}
IpPrefix that = (IpPrefix) obj;
return Arrays.equals(this.address, that.address) && this.prefixLength == that.prefixLength;
| public java.net.InetAddress | getAddress()Returns a copy of the first IP address in the prefix. Modifying the returned object does not
change this object's contents.
try {
return InetAddress.getByAddress(address);
} catch (UnknownHostException e) {
// Cannot happen. InetAddress.getByAddress can only throw an exception if the byte
// array is the wrong length, but we check that in the constructor.
return null;
}
| public int | getPrefixLength()Returns the prefix length of this {@code IpPrefix}.
return prefixLength;
| public byte[] | getRawAddress()Returns a copy of the IP address bytes in network order (the highest order byte is the zeroth
element). Modifying the returned array does not change this object's contents.
return address.clone();
| public int | hashCode()Gets the hashcode of the represented IP prefix.
return Arrays.hashCode(address) + 11 * prefixLength;
| public java.lang.String | toString()Returns a string representation of this {@code IpPrefix}.
try {
return InetAddress.getByAddress(address).getHostAddress() + "/" + prefixLength;
} catch(UnknownHostException e) {
// Cosmic rays?
throw new IllegalStateException("IpPrefix with invalid address! Shouldn't happen.", e);
}
| public void | writeToParcel(android.os.Parcel dest, int flags)Implement the Parcelable interface.
dest.writeByteArray(address);
dest.writeInt(prefixLength);
|
|