summaryrefslogtreecommitdiff
path: root/common/framework
diff options
context:
space:
mode:
authorRemi NGUYEN VAN <reminv@google.com>2021-03-18 04:53:40 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-03-18 04:53:40 +0000
commite9966a525f5b1293ddfb41ed0e6817c24d46ccc8 (patch)
tree795072a675fa5acffcff0b3a14fbf460d1600099 /common/framework
parent34ee25f55ecc4c4f77c9371db25c7d078e856bf5 (diff)
parent7606dec9507e190938e3ba1148800c2d3168faa4 (diff)
downloadnet-e9966a525f5b1293ddfb41ed0e6817c24d46ccc8.tar.gz
Merge "Move trimV4AddrZeros to libs/net" am: 54fcb1500e am: 3bb6f4c4eb am: 7606dec950
Original change: https://android-review.googlesource.com/c/platform/frameworks/libs/net/+/1637461 Change-Id: I05d5a2ed5d9f6a850770493c45eedf6ef18c1fd5
Diffstat (limited to 'common/framework')
-rw-r--r--common/framework/com/android/net/module/util/Inet4AddressUtils.java26
1 files changed, 26 insertions, 0 deletions
diff --git a/common/framework/com/android/net/module/util/Inet4AddressUtils.java b/common/framework/com/android/net/module/util/Inet4AddressUtils.java
index a1d34a0f..87f43d51 100644
--- a/common/framework/com/android/net/module/util/Inet4AddressUtils.java
+++ b/common/framework/com/android/net/module/util/Inet4AddressUtils.java
@@ -163,4 +163,30 @@ public class Inet4AddressUtils {
throws IllegalArgumentException {
return intToInet4AddressHTH(prefixLengthToV4NetmaskIntHTH(prefixLength));
}
+
+ /**
+ * Trim leading zeros from IPv4 address strings
+ * Non-v4 addresses and host names remain unchanged.
+ * For example, 192.168.000.010 -> 192.168.0.10
+ * @param addr a string representing an ip address
+ * @return a string properly trimmed
+ */
+ public static String trimAddressZeros(String addr) {
+ if (addr == null) return null;
+ String[] octets = addr.split("\\.");
+ if (octets.length != 4) return addr;
+ StringBuilder builder = new StringBuilder(16);
+ String result = null;
+ for (int i = 0; i < 4; i++) {
+ try {
+ if (octets[i].length() > 3) return addr;
+ builder.append(Integer.parseInt(octets[i]));
+ } catch (NumberFormatException e) {
+ return addr;
+ }
+ if (i < 3) builder.append('.');
+ }
+ result = builder.toString();
+ return result;
+ }
}