public final class IpPrefix extends Object implements Parcelable
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.Parcelable.ClassLoaderCreator<T>, Parcelable.Creator<T>
Modifier and Type | Field and Description |
---|---|
static Parcelable.Creator<IpPrefix> |
CREATOR
Implement the Parcelable interface.
|
CONTENTS_FILE_DESCRIPTOR, PARCELABLE_ELIDE_DUPLICATES, PARCELABLE_WRITE_RETURN_VALUE
Constructor and Description |
---|
IpPrefix(byte[] address,
int prefixLength)
Constructs a new
IpPrefix from a byte array containing an IPv4 or IPv6 address in
network byte order and a prefix length. |
IpPrefix(InetAddress address,
int prefixLength)
Constructs a new
IpPrefix from an IPv4 or IPv6 address and a prefix length. |
IpPrefix(String prefix)
Constructs a new IpPrefix from a string such as "192.0.2.1/24" or "2001:db8::1/64".
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(InetAddress address)
Determines whether the prefix contains the specified address.
|
int |
describeContents()
Implement the Parcelable interface.
|
boolean |
equals(Object obj)
Compares this
IpPrefix object against the specified object in obj . |
InetAddress |
getAddress()
Returns a copy of the first IP address in the prefix.
|
int |
getPrefixLength()
Returns the prefix length of this
IpPrefix . |
byte[] |
getRawAddress()
Returns a copy of the IP address bytes in network order (the highest order byte is the zeroth
element).
|
int |
hashCode()
Gets the hashcode of the represented IP prefix.
|
String |
toString()
Returns a string representation of this
IpPrefix . |
void |
writeToParcel(Parcel dest,
int flags)
Implement the Parcelable interface.
|
public static final Parcelable.Creator<IpPrefix> CREATOR
public IpPrefix(byte[] address, int prefixLength)
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 192.0.2.1/24
is silently converted to 192.0.2.0/24
.address
- the IP address. Must be non-null and exactly 4 or 16 bytes long.prefixLength
- the prefix length. Must be >= 0 and <= (32 or 128) (IPv4 or IPv6).public IpPrefix(InetAddress address, int prefixLength)
IpPrefix
from an IPv4 or IPv6 address and a prefix length. Silently
truncates the address to the prefix length, so for example 192.0.2.1/24
is silently
converted to 192.0.2.0/24
.address
- the IP address. Must be non-null.prefixLength
- the prefix length. Must be >= 0 and <= (32 or 128) (IPv4 or IPv6).public IpPrefix(String prefix)
192.0.2.1/24
is silently converted to 192.0.2.0/24
.prefix
- the prefix to parsepublic boolean equals(Object obj)
IpPrefix
object against the specified object in obj
. Two
objects are equal if they have the same startAddress and prefixLength.equals
in class Object
obj
- the object to be tested for equality.true
if both objects are equal, false
otherwise.Object.hashCode()
,
HashMap
public int hashCode()
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public InetAddress getAddress()
public byte[] getRawAddress()
public int getPrefixLength()
IpPrefix
.public boolean contains(InetAddress address)
address
- An InetAddress
to test.true
if the prefix covers the given address.public String toString()
IpPrefix
.public int describeContents()
describeContents
in interface Parcelable
Parcelable.CONTENTS_FILE_DESCRIPTOR
public void writeToParcel(Parcel dest, int flags)
writeToParcel
in interface Parcelable
dest
- The Parcel in which the object should be written.flags
- Additional flags about how the object should be written.
May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE
.