Move IPv4 string utilities
[mdsal.git] / model / ietf / ietf-type-util / src / main / java / org / opendaylight / mdsal / model / ietf / util / Ipv4Utils.java
index 9d2eb2e6b617cbee645242ddd328e4fd939c0765..c9ae02ebce2bd90711a61ca7fef3f7406b93832c 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.mdsal.model.ietf.util;
 
+import static com.google.common.base.Preconditions.checkArgument;
+
 import com.google.common.annotations.Beta;
 import org.eclipse.jdt.annotation.NonNull;
 
@@ -64,4 +66,19 @@ public final class Ipv4Utils {
     static String addressString(final int bits) {
         return (bits >>> 24) + "." + (bits >>> 16 & 0xFF) + "." + (bits >>> 8 & 0xFF) + "." + (bits & 0xFF);
     }
+
+    static String addressString(final byte @NonNull[] bytes) {
+        final StringBuilder sb = new StringBuilder(15);
+        appendIpv4String(sb, bytes);
+        return sb.toString();
+    }
+
+    static void appendIpv4String(final StringBuilder sb, final byte @NonNull[] bytes) {
+        checkArgument(bytes.length == INET4_LENGTH, "IPv4 address length is 4 bytes");
+
+        sb.append(Byte.toUnsignedInt(bytes[0]));
+        for (int i = 1; i < INET4_LENGTH; ++i) {
+            sb.append('.').append(Byte.toUnsignedInt(bytes[i]));
+        }
+    }
 }