*/
package org.opendaylight.mdsal.model.ietf.util;
+import org.eclipse.jdt.annotation.NonNull;
+
/**
* IPv4 address parsing for ietf-inet-types ipv4-address. This is an internal implementation class, not meant to be
* exposed in any shape or form to the outside world, as the code relies on the fact that the strings presented to it
*/
final class Ipv4Utils {
private Ipv4Utils() {
- throw new UnsupportedOperationException();
+
}
- static void fillIpv4Bytes(final byte[] bytes, final int byteStart, final String str, final int strStart,
+ static void fillIpv4Bytes(final byte @NonNull[] bytes, final int byteStart, final String str, final int strStart,
final int strLimit) {
int out = byteStart;
int val = 0;
bytes[out++] = (byte) val;
val = 0;
} else {
- val = 10 * val + (c - '0');
+ val = 10 * val + c - '0';
}
}
bytes[out] = (byte) val;
}
+
+ static int addressBits(final String str, final int limit) {
+ int prev = 0;
+ int current = 0;
+ for (int i = 0; i < limit; ++i) {
+ final char c = str.charAt(i);
+ if (c == '.') {
+ prev = prev << 8 | current;
+ current = 0;
+ } else {
+ current = 10 * current + c - '0';
+ }
+ }
+ return prev << 8 | current;
+ }
+
+ static byte @NonNull[] addressBytes(final String str, final int limit) {
+ final byte[] bytes = new byte[4];
+ Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, limit);
+ return bytes;
+ }
+
+ static String addressString(final int bits) {
+ return new StringBuilder(15)
+ .append(bits >>> 24).append('.')
+ .append(bits >>> 16 & 0xFF).append('.')
+ .append(bits >>> 8 & 0xFF).append('.')
+ .append(bits & 0xFF)
+ .toString();
+ }
}