FileDocCategorySizeDatePackage
IpPrefix.javaAPI DocAndroid 5.1 API8059Thu Mar 12 22:22:10 GMT 2015android.net

IpPrefix

public final class IpPrefix extends Object implements android.os.Parcelable
This 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
CREATOR
Implement 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}.

param
address the IP address. Must be non-null and exactly 4 or 16 bytes long.
param
prefixLength the prefix length. Must be >= 0 and <= (32 or 128) (IPv4 or IPv6).
hide

        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}.

param
address the IP address. Must be non-null.
param
prefixLength the prefix length. Must be >= 0 and <= (32 or 128) (IPv4 or IPv6).
hide

        // 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}.

param
prefix the prefix to parse
hide

        // 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 voidcheckAndMaskAddressAndPrefixLength()

        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 intdescribeContents()
Implement the Parcelable interface.

        return 0;
    
public booleanequals(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.

param
obj the object to be tested for equality.
return
{@code true} if both objects are equal, {@code false} otherwise.

        if (!(obj instanceof IpPrefix)) {
            return false;
        }
        IpPrefix that = (IpPrefix) obj;
        return Arrays.equals(this.address, that.address) && this.prefixLength == that.prefixLength;
    
public java.net.InetAddressgetAddress()
Returns a copy of the first IP address in the prefix. Modifying the returned object does not change this object's contents.

return
the address in the form of a byte array.

        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 intgetPrefixLength()
Returns the prefix length of this {@code IpPrefix}.

return
the prefix length.

        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
the address in the form of a byte array.

        return address.clone();
    
public inthashCode()
Gets the hashcode of the represented IP prefix.

return
the appropriate hashcode value.

        return Arrays.hashCode(address) + 11 * prefixLength;
    
public java.lang.StringtoString()
Returns a string representation of this {@code IpPrefix}.

return
a string such as {@code "192.0.2.0/24"} or {@code "2001:db8:1:2::/64"}.

        try {
            return InetAddress.getByAddress(address).getHostAddress() + "/" + prefixLength;
        } catch(UnknownHostException e) {
            // Cosmic rays?
            throw new IllegalStateException("IpPrefix with invalid address! Shouldn't happen.", e);
        }
    
public voidwriteToParcel(android.os.Parcel dest, int flags)
Implement the Parcelable interface.

        dest.writeByteArray(address);
        dest.writeInt(prefixLength);