final String str = ipv4AddressString(addr);
final byte[] bytes = new byte[INET4_LENGTH];
final int percent = str.indexOf('%');
- fillIpv4Bytes(bytes, str, percent == -1 ? str.length() : percent);
+ Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, percent == -1 ? str.length() : percent);
return bytes;
}
final int slash = str.lastIndexOf('/');
final byte[] bytes = new byte[INET4_LENGTH + 1];
- fillIpv4Bytes(bytes, str, slash);
+ Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, slash);
bytes[INET4_LENGTH] = (byte)Integer.parseInt(str.substring(slash + 1), 10);
return bytes;
}
- private static void fillIpv4Bytes(final byte[] bytes, final String str, final int limit) {
- int out = 0;
- int val = 0;
- for (int i = 0; i < limit; ++i) {
- final char c = str.charAt(i);
- if (c == '.') {
- bytes[out++] = (byte) val;
- val = 0;
- } else {
- val = 10 * val + (c - '0');
- }
- }
-
- bytes[out] = (byte) val;
- }
-
/**
* Create an Ipv6Address by interpreting input bytes as an IPv6 address.
*
--- /dev/null
+/*
+ * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.model.ietf.util;
+
+/**
+ * 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
+ * have been previously validated to conform to the regular expressions defined in the YANG model.
+ */
+final class Ipv4Utils {
+ private Ipv4Utils() {
+ throw new UnsupportedOperationException();
+ }
+
+ static void fillIpv4Bytes(final byte[] bytes, final int byteStart, final String str, final int strStart,
+ final int strLimit) {
+ int out = byteStart;
+ int val = 0;
+ for (int i = strStart; i < strLimit; ++i) {
+ final char c = str.charAt(i);
+ if (c == '.') {
+ bytes[out++] = (byte) val;
+ val = 0;
+ } else {
+ val = 10 * val + (c - '0');
+ }
+ }
+
+ bytes[out] = (byte) val;
+ }
+}
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
-import com.google.common.net.InetAddresses;
-import java.net.Inet4Address;
-import java.net.InetAddress;
import javax.annotation.Nonnull;
/**
/* frankenstein - v4 attached to v6, mixed notation */
if (ch == '.' && ((j + INADDR4SZ) <= INADDR6SZ)) {
- /* this has passed the regexp so it is fairly safe to parse it
- * straight away. As v4 addresses do not suffer from the same
- * deficiencies as the java v6 implementation we can invoke it
- * straight away and be done with it
+ /*
+ * This has passed the regexp so it is fairly safe to parse it
+ * straight away. Use the Ipv4Utils for that.
*/
- Preconditions.checkArgument(j != (INADDR6SZ - INADDR4SZ - 1), "Invalid v4 in v6 mapping in %s", addrStr);
- InetAddress _inet_form = InetAddresses.forString(addrStr.substring(curtok, addrStrLen));
-
- Preconditions.checkArgument(_inet_form instanceof Inet4Address);
- System.arraycopy(_inet_form.getAddress(), 0, dst, j, INADDR4SZ);
+ Ipv4Utils.fillIpv4Bytes(dst, j, addrStr, curtok, addrStrLen);
j += INADDR4SZ;
-
saw_xdigit = false;
break;
}