Bug 2280 - MatchConvertorImpl bugs found during test creation
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / util / ByteUtil.java
index bf2631fc02666897dec12e087f7d34a10ca27b9f..671c081393b8d91f7262559dea3f166992927674 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.openflowplugin.openflow.md.util;
 
 import com.google.common.base.Preconditions;
-
 import java.math.BigInteger;
 import java.util.Arrays;
 
@@ -62,7 +61,7 @@ public abstract class ByteUtil {
      * @return a long representing the unsigned int
      */
     public static final long bytesToUnsignedInt(final byte[] bytes) {
-        Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly two bytes long.");
+        Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
         long unsignedInt = 0;
         unsignedInt |= bytes[0] & 0xFF;
         unsignedInt <<= 8;
@@ -74,6 +73,23 @@ public abstract class ByteUtil {
         return unsignedInt;
     }
 
+    /**
+     * Converts a 3 byte array of unsigned bytes to unsigned int
+     *
+     * @param bytes an array of 4 unsigned bytes
+     * @return a long representing the unsigned int
+     */
+    public static final long bytesToUnsignedMedium(final byte[] bytes) {
+        Preconditions.checkArgument(bytes.length == 3, "Input byte array must be exactly three bytes long.");
+        long unsignedMedium = 0;
+        unsignedMedium |= bytes[0] & 0xFF;
+        unsignedMedium <<= 8;
+        unsignedMedium |= bytes[1] & 0xFF;
+        unsignedMedium <<= 8;
+        unsignedMedium |= bytes[2] & 0xFF;
+        return unsignedMedium;
+    }
+
     /**
      * Converts a 2 byte array of unsigned bytes to unsigned short
      *
@@ -104,6 +120,20 @@ public abstract class ByteUtil {
         return bytes;
     }
 
+    /**
+     * Converts unsigned integer to a 3 byte array of unsigned bytes
+     *
+     * @param unsignedInt representing the unsigned integer
+     * @return bytes an array of 3 unsigned bytes
+     */
+    public static byte[] unsignedMediumToBytes(final Long unsignedInt) {
+        byte[] bytes = new byte[3];
+        bytes[2] = (byte) (unsignedInt & 0xFF);
+        bytes[1] = (byte) ((unsignedInt >> 8) & 0xFF);
+        bytes[0] = (byte) ((unsignedInt >> 16) & 0xFF);
+        return bytes;
+    }
+
     /**
      * Converts unsigned short to a 2 byte array of unsigned bytes
      *