ICMP fix and Packet class should store and provide access to the raw payload in case...
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / utils / HexEncode.java
index 557b848b73aa8e1327f95cc9f6c0c21f77bb6d3a..41b4b7a57ddc8deae989f3eb98605a10ca7a8bcd 100644 (file)
@@ -17,21 +17,31 @@ import java.math.BigInteger;
  *
  */
 public class HexEncode {
-       /**
-        * This method converts byte array into String format without ":" inserted.
-        */
+    /**
+     * This method converts byte array into String format without ":" inserted.
+     * 
+     * @param bytes
+     *            The byte array to convert to string
+     * @return The hexadecimal representation of the byte array. If bytes is
+     *         null, "null" string is returned
+     */
     public static String bytesToHexString(byte[] bytes) {
-        int i;
+
+        if (bytes == null) {
+            return "null";
+        }
+
         String ret = "";
-        String tmp;
         StringBuffer buf = new StringBuffer();
-        for (i = 0; i < bytes.length; i++) {
-            if (i > 0)
+        for (int i = 0; i < bytes.length; i++) {
+            if (i > 0) {
                 ret += ":";
-            short u8byte = (short) ((short) bytes[i] & 0xff);
-            tmp = Integer.toHexString(u8byte);
-            if (tmp.length() == 1)
+            }
+            short u8byte = (short) (bytes[i] & 0xff);
+            String tmp = Integer.toHexString(u8byte);
+            if (tmp.length() == 1) {
                 buf.append("0");
+            }
             buf.append(tmp);
         }
         ret = buf.toString();
@@ -45,24 +55,31 @@ public class HexEncode {
         int i = 0;
         for (; i < (16 - arr.length); i++) {
             buf.append("0");
-            if ((i & 0x01) == 1)
+            if ((i & 0x01) == 1) {
                 buf.append(":");
+            }
         }
         for (int j = 0; j < arr.length; j++) {
             buf.append(arr[j]);
-            if ((((i + j) & 0x01) == 1) && (j < (arr.length - 1)))
+            if ((((i + j) & 0x01) == 1) && (j < (arr.length - 1))) {
                 buf.append(":");
+            }
         }
         return buf.toString();
     }
 
+
     public static byte[] bytesFromHexString(String values) {
-        String[] octets = values.split(":");
-        byte[] ret = new byte[octets.length];
-        int i;
+        String target = "";
+        if (values != null) {
+            target = values;
+        }
+        String[] octets = target.split(":");
 
-        for (i = 0; i < octets.length; i++)
+        byte[] ret = new byte[octets.length];
+        for (int i = 0; i < octets.length; i++) {
             ret[i] = Integer.valueOf(octets[i], 16).byteValue();
+        }
         return ret;
     }
 
@@ -71,21 +88,24 @@ public class HexEncode {
         return value;
     }
 
-       /**
-        * This method converts byte array into HexString format with ":" inserted.
-        */
+    /**
+     * This method converts byte array into HexString format with ":" inserted.
+     */
     public static String bytesToHexStringFormat(byte[] bytes) {
-        int i;
+        if (bytes == null) {
+            return "null";
+        }
         String ret = "";
-        String tmp;
         StringBuffer buf = new StringBuffer();
-        for (i = 0; i < bytes.length; i++) {
-            if (i > 0)
+        for (int i = 0; i < bytes.length; i++) {
+            if (i > 0) {
                 buf.append(":");
-            short u8byte = (short) ((short) bytes[i] & 0xff);
-            tmp = Integer.toHexString(u8byte);
-            if (tmp.length() == 1)
+            }
+            short u8byte = (short) (bytes[i] & 0xff);
+            String tmp = Integer.toHexString(u8byte);
+            if (tmp.length() == 1) {
                 buf.append("0");
+            }
             buf.append(tmp);
         }
         ret = buf.toString();