Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Inherited Constants](https://developer.android.com/reference/android/net/IpPrefix#inhconstants) \| [Fields](https://developer.android.com/reference/android/net/IpPrefix#lfields) \| [Ctors](https://developer.android.com/reference/android/net/IpPrefix#pubctors) \| [Methods](https://developer.android.com/reference/android/net/IpPrefix#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/net/IpPrefix#inhmethods)

# IpPrefix

*** ** * ** ***

` public final class IpPrefix `   
` extends https://developer.android.com/reference/java/lang/Object ` ` implements https://developer.android.com/reference/android/os/Parcelable `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.net.IpPrefix |

<br />

*** ** * ** ***

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.

<br />

## Summary

### Inherited constants

From interface `https://developer.android.com/reference/android/os/Parcelable` `int` `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR`

Descriptor bit used with `https://developer.android.com/reference/android/os/Parcelable#describeContents()`: indicates that the Parcelable object's flattened representation includes a file descriptor.
`int` `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE`

Flag for use with `https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)`: the object being written is a return value, that is the result of a function such as "`Parcelable someFunction()`", "`void someFunction(out Parcelable)`", or "`void someFunction(inout Parcelable)`".

| ### Fields ||
|---|---|
| ` public static final https://developer.android.com/reference/android/os/Parcelable.Creator<https://developer.android.com/reference/android/net/IpPrefix>` | `https://developer.android.com/reference/android/net/IpPrefix#CREATOR` Implement the Parcelable interface. |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/net/IpPrefix#IpPrefix(java.net.InetAddress,%20int)(https://developer.android.com/reference/java/net/InetAddress address, int prefixLength) ` Constructs a new `IpPrefix` from an IPv4 or IPv6 address and a prefix length. |

| ### Public methods ||
|---|---|
| ` boolean` | ` https://developer.android.com/reference/android/net/IpPrefix#contains(java.net.InetAddress)(https://developer.android.com/reference/java/net/InetAddress address) ` Determines whether the prefix contains the specified address. |
| ` int` | ` https://developer.android.com/reference/android/net/IpPrefix#describeContents()() ` Implement the Parcelable interface. |
| ` boolean` | ` https://developer.android.com/reference/android/net/IpPrefix#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Compares this `IpPrefix` object against the specified object in `obj`. |
| ` https://developer.android.com/reference/java/net/InetAddress` | ` https://developer.android.com/reference/android/net/IpPrefix#getAddress()() ` Returns a copy of the first IP address in the prefix. |
| ` int` | ` https://developer.android.com/reference/android/net/IpPrefix#getPrefixLength()() ` Returns the prefix length of this `IpPrefix`. |
| ` byte[]` | ` https://developer.android.com/reference/android/net/IpPrefix#getRawAddress()() ` Returns a copy of the IP address bytes in network order (the highest order byte is the zeroth element). |
| ` int` | ` https://developer.android.com/reference/android/net/IpPrefix#hashCode()() ` Gets the hashcode of the represented IP prefix. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/net/IpPrefix#toString()() ` Returns a string representation of this `IpPrefix`. |
| ` void` | ` https://developer.android.com/reference/android/net/IpPrefix#writeToParcel(android.os.Parcel,%20int)(https://developer.android.com/reference/android/os/Parcel dest, int flags) ` Implement the Parcelable interface. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/android/os/Parcelable ` ` abstract int` ` https://developer.android.com/reference/android/os/Parcelable#describeContents()() `

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.
` abstract void` ` https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)(https://developer.android.com/reference/android/os/Parcel dest, int flags) `

Flatten this object in to a Parcel.

## Fields

### CREATOR

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final Creator<IpPrefix> CREATOR
```

Implement the Parcelable interface.

<br />

## Public constructors

### IpPrefix

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public IpPrefix (InetAddress address, 
                int prefixLength)
```

Constructs a new `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`.

<br />

| Parameters ||
|---|---|
| `address` | `InetAddress`: the IP address. Must be non-null. <br /> |
| `prefixLength` | `int`: the prefix length. Must be \>= 0 and \<= (32 or 128) (IPv4 or IPv6). Value is between 0 and 128 inclusive <br /> |

## Public methods

### contains

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean contains (InetAddress address)
```

Determines whether the prefix contains the specified address.

<br />

| Parameters ||
|---|---|
| `address` | `InetAddress`: An `https://developer.android.com/reference/java/net/InetAddress` to test. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if the prefix covers the given address. `false` otherwise. <br /> |

### describeContents

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int describeContents ()
```

Implement the Parcelable interface.

<br />

| Returns ||
|---|---|
| `int` | a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either `0` or - `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR` |

### equals

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean equals (Object obj)
```

Compares this `IpPrefix` object against the specified object in `obj`. Two objects are equal if they have the same startAddress and prefixLength.

<br />

| Parameters ||
|---|---|
| `obj` | `Object`: the object to be tested for equality. This value may be `null`. <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if both objects are equal, `false` otherwise. <br /> |

### getAddress

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public InetAddress getAddress ()
```

Returns a copy of the first IP address in the prefix. Modifying the returned object does not change this object's contents.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/InetAddress` | the address in the form of a byte array. This value cannot be `null`. <br /> |

### getPrefixLength

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getPrefixLength ()
```

Returns the prefix length of this `IpPrefix`.

<br />

| Returns ||
|---|---|
| `int` | the prefix length. Value is between 0 and 128 inclusive <br /> |

### getRawAddress

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Returns ||
|---|---|
| `byte[]` | the address in the form of a byte array. This value cannot be `null`. <br /> |

### hashCode

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int hashCode ()
```

Gets the hashcode of the represented IP prefix.

<br />

| Returns ||
|---|---|
| `int` | the appropriate hashcode value. <br /> |

### toString

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Returns a string representation of this `IpPrefix`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string such as `"192.0.2.0/24"` or `"2001:db8:1:2::/64"`. <br /> |

### writeToParcel

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void writeToParcel (Parcel dest, 
                int flags)
```

Implement the Parcelable interface.

<br />

| Parameters ||
|---|---|
| `dest` | `Parcel`: The Parcel in which the object should be written. This value cannot be `null`. <br /> |
| `flags` | `int`: Additional flags about how the object should be written. May be 0 or `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE`. Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE` |