f8797a65660b374262d0771a2ef13b42124f0244
[mdsal.git] / model / ietf / ietf-type-util / src / main / java / org / opendaylight / mdsal / model / ietf / util / Ipv4Utils.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.model.ietf.util;
9
10 /**
11  * IPv4 address parsing for ietf-inet-types ipv4-address. This is an internal implementation class, not meant to be
12  * exposed in any shape or form to the outside world, as the code relies on the fact that the strings presented to it
13  * have been previously validated to conform to the regular expressions defined in the YANG model.
14  */
15 final class Ipv4Utils {
16     private Ipv4Utils() {
17         throw new UnsupportedOperationException();
18     }
19
20     static void fillIpv4Bytes(final byte[] bytes, final int byteStart, final String str, final int strStart,
21             final int strLimit) {
22         int out = byteStart;
23         int val = 0;
24         for (int i = strStart; i < strLimit; ++i) {
25             final char c = str.charAt(i);
26             if (c == '.') {
27                 bytes[out++] = (byte) val;
28                 val = 0;
29             } else {
30                 val = 10 * val + (c - '0');
31             }
32         }
33
34         bytes[out] = (byte) val;
35     }
36 }