summaryrefslogtreecommitdiff
path: root/core/java/android/net/IpPrefix.java
diff options
context:
space:
mode:
authorPaul Hu <paulhu@google.com>2019-04-01 00:52:53 -0700
committerandroid-build-merger <android-build-merger@google.com>2019-04-01 00:52:53 -0700
commitb2913320cd27bfcf045d43bd5e701d41a13f5554 (patch)
tree56a3d2432fc4e8bd6e18a0de85a50de58f5a0be4 /core/java/android/net/IpPrefix.java
parentcdc66500632805ecc26ad42288ecb8b646fe17c5 (diff)
parentbf3e6f5e754a6f3c4e6c3ee396790884b9d56989 (diff)
Merge "Fix ApfCapabilities, LinkAddress, RouteInfo, IpPrefix API issues." am: f9d61f1c6d am: 96f34c3151
am: bf3e6f5e75 Change-Id: I599dcdb2312a3e1bb1de8e03f349df207c7cc075
Diffstat (limited to 'core/java/android/net/IpPrefix.java')
-rw-r--r--core/java/android/net/IpPrefix.java20
1 files changed, 11 insertions, 9 deletions
diff --git a/core/java/android/net/IpPrefix.java b/core/java/android/net/IpPrefix.java
index 402bffdc2a97..8cfe6df678c7 100644
--- a/core/java/android/net/IpPrefix.java
+++ b/core/java/android/net/IpPrefix.java
@@ -16,6 +16,7 @@
package android.net;
+import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.annotation.TestApi;
@@ -71,7 +72,7 @@ public final class IpPrefix implements Parcelable {
*
* @hide
*/
- public IpPrefix(@NonNull byte[] address, int prefixLength) {
+ public IpPrefix(@NonNull byte[] address, @IntRange(from = 0, to = 128) int prefixLength) {
this.address = address.clone();
this.prefixLength = prefixLength;
checkAndMaskAddressAndPrefixLength();
@@ -88,7 +89,7 @@ public final class IpPrefix implements Parcelable {
*/
@SystemApi
@TestApi
- public IpPrefix(@NonNull InetAddress address, int prefixLength) {
+ public IpPrefix(@NonNull InetAddress address, @IntRange(from = 0, to = 128) int prefixLength) {
// 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();
@@ -150,13 +151,13 @@ public final class IpPrefix implements Parcelable {
*
* @return the address in the form of a byte array.
*/
- public InetAddress getAddress() {
+ public @NonNull InetAddress getAddress() {
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;
+ throw new IllegalArgumentException("Address is invalid");
}
}
@@ -166,7 +167,7 @@ public final class IpPrefix implements Parcelable {
*
* @return the address in the form of a byte array.
*/
- public byte[] getRawAddress() {
+ public @NonNull byte[] getRawAddress() {
return address.clone();
}
@@ -175,6 +176,7 @@ public final class IpPrefix implements Parcelable {
*
* @return the prefix length.
*/
+ @IntRange(from = 0, to = 128)
public int getPrefixLength() {
return prefixLength;
}
@@ -183,10 +185,10 @@ public final class IpPrefix implements Parcelable {
* Determines whether the prefix contains the specified address.
*
* @param address An {@link InetAddress} to test.
- * @return {@code true} if the prefix covers the given address.
+ * @return {@code true} if the prefix covers the given address. {@code false} otherwise.
*/
- public boolean contains(InetAddress address) {
- byte[] addrBytes = (address == null) ? null : address.getAddress();
+ public boolean contains(@NonNull InetAddress address) {
+ byte[] addrBytes = address.getAddress();
if (addrBytes == null || addrBytes.length != this.address.length) {
return false;
}
@@ -201,7 +203,7 @@ public final class IpPrefix implements Parcelable {
* @param otherPrefix the prefix to test
* @hide
*/
- public boolean containsPrefix(IpPrefix otherPrefix) {
+ public boolean containsPrefix(@NonNull IpPrefix otherPrefix) {
if (otherPrefix.getPrefixLength() < prefixLength) return false;
final byte[] otherAddress = otherPrefix.getRawAddress();
NetworkUtils.maskRawAddress(otherAddress, prefixLength);