2 * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.model.ietf.util;
10 import org.eclipse.jdt.annotation.NonNull;
13 * IPv4 address parsing for ietf-inet-types ipv4-address. This is an internal implementation class, not meant to be
14 * exposed in any shape or form to the outside world, as the code relies on the fact that the strings presented to it
15 * have been previously validated to conform to the regular expressions defined in the YANG model.
17 final class Ipv4Utils {
22 static void fillIpv4Bytes(final byte @NonNull[] bytes, final int byteStart, final String str, final int strStart,
26 for (int i = strStart; i < strLimit; ++i) {
27 final char c = str.charAt(i);
29 bytes[out++] = (byte) val;
32 val = 10 * val + c - '0';
36 bytes[out] = (byte) val;
39 static int addressBits(final String str, final int limit) {
42 for (int i = 0, shift = 24; i < limit; ++i) {
43 final char c = str.charAt(i);
45 prev |= current << shift;
49 current = 10 * current + c - '0';
52 return prev | current;
55 static byte @NonNull[] addressBytes(final String str, final int limit) {
56 final byte[] bytes = new byte[4];
57 Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, limit);
61 static String addressString(final int bits) {
62 return new StringBuilder(15)
63 .append(bits >>> 24).append('.')
64 .append(bits >>> 16 & 0xFF).append('.')
65 .append(bits >>> 8 & 0xFF).append('.')