am 2ebd3612: am f352491a: Merge "Trim leading zeros from ipv4 addrs." into ics-mr1
* commit '2ebd3612d82f728873f87cdbea9fa5513b5cfb39': Trim leading zeros from ipv4 addrs.
This commit is contained in:
@@ -250,4 +250,31 @@ public class NetworkUtils {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trim leading zeros from IPv4 address strings
|
||||||
|
* Our base libraries will interpret that as octel..
|
||||||
|
* Must leave non v4 addresses and host names alone.
|
||||||
|
* For example, 192.168.000.010 -> 192.168.0.10
|
||||||
|
* TODO - fix base libraries and remove this function
|
||||||
|
* @param addr a string representing an ip addr
|
||||||
|
* @return a string propertly trimmed
|
||||||
|
*/
|
||||||
|
public static String trimV4AddrZeros(String addr) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user