Refactoring: ByteUtil: change the method order and name
authorJuYoung Kim <j0.kim@samsung.com>
Sun, 17 Aug 2014 08:11:54 +0000 (17:11 +0900)
committerJuYoung Kim <j0.kim@samsung.com>
Thu, 21 Aug 2014 05:49:37 +0000 (14:49 +0900)
Change-Id: I53d22d4f112e595a374d0a1218cd92c8f92163cd
Signed-off-by: JuYoung Kim <j0.kim@samsung.com>
Conflicts:
org.tizen.dynamicanalyzer.common/src/org/tizen/dynamicanalyzer/util/ByteUtil.java

org.tizen.dynamicanalyzer.common/src/org/tizen/dynamicanalyzer/util/ByteUtil.java

index 577d706..7659300 100755 (executable)
@@ -30,27 +30,18 @@ import java.util.List;
 
 import org.tizen.dynamicanalyzer.model.Streamable;
 
+/*
+ * Change each object to the byte and vice versa.
+ * Swap endian order automatically. 
+ * add '\0' to the end of string automatically.
+ * String is endianness.
+ */
 public class ByteUtil {
+       private static final DALogger DA_LOG = DALogger.getInstance();
+       public static int recursive_checker = 0;
        
        public static byte EOS = '\0';
 
-       /**
-        * Convert string to byte
-        * 
-        * ByteUtils.toByte("1", *) = 0x01 ByteUtils.toByte("-1", *) = 0xff
-        * 
-        * @param value
-        * @param defaultValue
-        * @return
-        */
-       @Deprecated
-       public static byte toByte(String value, byte defaultValue) {
-               try {
-                       return Byte.parseByte(value);
-               } catch (Exception e) {
-                       return defaultValue;
-               }
-       }
 
        /**
         * Convert byte array(4byte) to int.
@@ -79,29 +70,29 @@ public class ByteUtil {
        }
 
        /**
-        * Convert byte array(8byte) to long.
+        * Convert int to byte array(4byte).
         * 
-        * @param src
-        * @param srcPos
-        * @return
+        * @param value
+        * @param dest
+        * @param destPos
         */
-       public static long toLong(byte[] src, int srcPos) {
-               long qword = 0;
-               for (int i = 0; i < 8; i++) {
-                       qword = (qword << 8) + (src[i + srcPos] & 0xFF);
+       public static void toBytes(int value, byte[] dest, int destPos) {
+               for (int i = 0; i < 4; i++) {
+                       dest[i + destPos] = (byte) (value >> ((7 - i) * 8));
                }
-               return ByteSwapper.swap(qword);
-               // return qword;
        }
 
        /**
-        * Convert byte array(8byte) to long.
+        * Convert int to byte array(4byte).
         * 
-        * @param src
+        * @param value
         * @return
         */
-       public static long toLong(byte[] src) {
-               return toLong(src, 0);
+       public static byte[] toBytes(int value) {
+               value = ByteSwapper.swap(value);
+               byte[] dest = new byte[4];
+               toBytes(value, dest, 0);
+               return dest;
        }
 
        /**
@@ -122,31 +113,31 @@ public class ByteUtil {
                long b4 = (qword >> 24) & 0xff;
                return new UnsignedInt(b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0);
        }
-       
+
        /**
-        * Convert int to byte array(4byte).
+        * Convert byte array(8byte) to long.
         * 
-        * @param value
-        * @param dest
-        * @param destPos
+        * @param src
+        * @param srcPos
+        * @return
         */
-       public static void toBytes(int value, byte[] dest, int destPos) {
-               for (int i = 0; i < 4; i++) {
-                       dest[i + destPos] = (byte) (value >> ((7 - i) * 8));
+       public static long toLong(byte[] src, int srcPos) {
+               long qword = 0;
+               for (int i = 0; i < 8; i++) {
+                       qword = (qword << 8) + (src[i + srcPos] & 0xFF);
                }
+               return ByteSwapper.swap(qword);
+               // return qword;
        }
 
        /**
-        * Convert int to byte array(4byte).
+        * Convert byte array(8byte) to long.
         * 
-        * @param value
+        * @param src
         * @return
         */
-       public static byte[] toBytes(int value) {
-               value = ByteSwapper.swap(value);
-               byte[] dest = new byte[4];
-               toBytes(value, dest, 0);
-               return dest;
+       public static long toLong(byte[] src) {
+               return toLong(src, 0);
        }
 
        /**
@@ -175,107 +166,6 @@ public class ByteUtil {
                return dest;
        }
 
-       /**
-        * Convert the string that formatted Octal or decimal or hexadecimal to byte array.
-        * Case of octal and Decimal, that change to three-digit string.
-        * Case of hexadecimal, that change to one byte. 
-        *       * 
-        * ByteUtils.toBytes(null) = null ByteUtils.toBytes("0E1F4E", 16) = [0x0e,
-        * 0xf4, 0x4e] ByteUtils.toBytes("48414e", 16) = [0x48, 0x41, 0x4e]
-        * 
-        * @param digits
-        *            string of number
-        * @param radix
-        *            base(can be 8, 10, 16)
-        * @return
-        * @throws NumberFormatException
-        */
-       @Deprecated
-       public static byte[] toBytes(String digits, int radix)
-                       throws IllegalArgumentException, NumberFormatException {
-               if (digits == null) {
-                       return null;
-               }
-               if (radix != 16 && radix != 10 && radix != 8) {
-                       throw new IllegalArgumentException("For input radix: \"" + radix
-                                       + "\"");
-               }
-               int divLen = (radix == 16) ? 2 : 3;
-               int length = digits.length();
-               if (length % divLen == 1) {
-                       throw new IllegalArgumentException("For input string: \"" + digits
-                                       + "\"");
-               }
-               length = length / divLen;
-               byte[] bytes = new byte[length];
-               for (int i = 0; i < length; i++) {
-                       int index = i * divLen;
-                       bytes[i] = (byte) (Short.parseShort(
-                                       digits.substring(index, index + divLen), radix));
-               }
-               return bytes;
-       }
-
-       /**
-        * Convert hexadecimal formatted string to byte array/
-        * The two-digit string change to one byte. 
-        * 
-        * ByteUtils.hexStringToBytes(null) = null
-        * ByteUtils.hexStringToBytes("0E1F4E") = [0x0e, 0xf4, 0x4e]
-        * ByteUtils.hexStringToBytes("48414e") = [0x48, 0x41, 0x4e]
-        * 
-        * @param digits
-        *            string of base-16 number
-        * @return
-        * @throws NumberFormatException
-        */
-       @Deprecated
-       public static byte[] hexStringToBytes(String digits)
-                       throws IllegalArgumentException, NumberFormatException {
-               if (digits == null) {
-                       return null;
-               }
-               int length = digits.length();
-               if (length % 2 == 1) {
-                       throw new IllegalArgumentException("For input string: \"" + digits
-                                       + "\"");
-               }
-               length = length / 2;
-               byte[] bytes = new byte[length];
-               for (int i = 0; i < length; i++) {
-                       int index = i * 2;
-                       bytes[i] = (byte) (Short.parseShort(
-                                       digits.substring(index, index + 2), 16));
-               }
-               return bytes;
-       }
-
-       public static byte[] stringToByte(String str) {
-               if (str.isEmpty()) {
-                       byte temp[] = new byte[1];
-                       temp[0] = EOS;
-                       return temp;
-               }
-
-               int size = str.length();
-               byte[] temp = null;
-               if (EOS != str.charAt(size - 1)) {
-                       temp = new byte[size + 1];
-                       System.arraycopy(str.getBytes(), 0, temp, 0, str.getBytes().length);
-                       temp[temp.length - 1] = EOS;
-               } else {
-                       temp = new byte[size];
-                       System.arraycopy(str.getBytes(), 0, temp, 0, str.getBytes().length);
-               }
-               return temp;
-       }
-
-       public static byte[] toBytes(short input) {
-               input = ByteSwapper.swap(input);
-               int size = Short.SIZE / Byte.SIZE;
-               return ByteBuffer.allocate(size).putShort(input).array();
-       }
-
        public static short toShort(byte[] input, int from) {
                int size = Short.SIZE / Byte.SIZE;
                byte[] temp = new byte[size];
@@ -284,16 +174,10 @@ public class ByteUtil {
                return ByteSwapper.swap(buf.getShort());
        }
 
-       public static byte[] floatToBytes(float input) {
-               input = ByteSwapper.swap(input);
-               int size = Float.SIZE / Byte.SIZE;
-               return ByteBuffer.allocate(size).putFloat(input).array();
-       }
-
-       public static byte[] doubleToBytes(double input) {
+       public static byte[] toBytes(short input) {
                input = ByteSwapper.swap(input);
-               int size = Double.SIZE / Byte.SIZE;
-               return ByteBuffer.allocate(size).putDouble(input).array();
+               int size = Short.SIZE / Byte.SIZE;
+               return ByteBuffer.allocate(size).putShort(input).array();
        }
 
        public static float toFloat(byte[] input, int from) {
@@ -304,6 +188,12 @@ public class ByteUtil {
                return ByteSwapper.swap(buf.getFloat());
        }
 
+       public static byte[] toBytes(float input) {
+               input = ByteSwapper.swap(input);
+               int size = Float.SIZE / Byte.SIZE;
+               return ByteBuffer.allocate(size).putFloat(input).array();
+       }
+
        public static double toDouble(byte[] input, int from) {
                int size = Double.SIZE / Byte.SIZE;
                byte[] temp = new byte[size];
@@ -312,39 +202,69 @@ public class ByteUtil {
                return ByteSwapper.swap(buf.getDouble());
        }
 
-       public static byte[] concatByteArray(byte[] front, byte[] rear) {
-               if (null == front && null == rear) {
-                       return null;
-               } else if (null == front && null != rear) {
-                       return rear;
-               } else if (null != front && null == rear) {
-                       return front;
-               }
+       public static byte[] toBytes(double input) {
+               input = ByteSwapper.swap(input);
+               int size = Double.SIZE / Byte.SIZE;
+               return ByteBuffer.allocate(size).putDouble(input).array();
+       }
 
-               byte[] result = new byte[front.length + rear.length];
-               System.arraycopy(front, 0, result, 0, front.length);
-               System.arraycopy(rear, 0, result, front.length, rear.length);
+       // TODO: make getString caller to know length or remained byte position => make toString method
+       public static String getString(byte[] data, int start) {
+               int length = getStringLength(data, start);
+               byte[] temp = new byte[length];
+               System.arraycopy(data, start, temp, 0, length);
+               String str = new String(temp);
+               str = str.substring(0, str.length() - 1);
+               return str;
+       }
 
-               return result;
+       public static int getStringLength(byte[] data, int start) {
+               int length = 0;
+               for (int i = start; i < data.length; i++, length++) {
+                       if (data[i] == 0) {
+                               return length + 1;
+                       }
+               }
+               return data.length;
        }
 
-       public static byte[] concatByteArray(final Object... byteArrays) {
-               if (null == byteArrays) {
-                       return null;
+       public static String toString(byte[] data, int start, byte[] remained) {
+               int length = getStringLength(data, start);
+               int byte_length = data.length;
+               byte[] temp = new byte[length];
+               System.arraycopy(data, start, temp, 0, length);
+               String str = new String(temp);
+               remained = new byte[byte_length - length];
+               System.arraycopy(data, start + length, remained, 0, byte_length - length);
+               return str;
+       }
+
+       public static byte[] toByte(String str) {
+               if (str.isEmpty()) {
+                       byte temp[] = new byte[1];
+                       temp[0] = EOS;
+                       return temp;
                }
-               byte[] temp = new byte[0];
-               for (Object obj : byteArrays) {
-                       if (null == obj) {
-                               continue;
-                       }
-                       if (obj instanceof byte[]) {
-                               temp = concatByteArray(temp, (byte[]) obj);
-                       }
+
+               int size = str.length();
+               byte[] temp = null;
+               if (EOS != str.charAt(size - 1)) {
+                       temp = new byte[size + 1];
+                       System.arraycopy(str.getBytes(), 0, temp, 0, size);
+                       temp[temp.length - 1] = EOS;
+               } else {
+                       temp = new byte[size];
+                       System.arraycopy(str.getBytes(), 0, temp, 0, size);
                }
                return temp;
        }
 
        @SuppressWarnings({ "unused", "unchecked" })
+       /*
+        * TODO: if this method is called frequently, check to improve
+        * make new byte array for every concatenation of each object
+        * 
+        */
        public static byte[] getByte(final Object... objects) {
                if (null == objects) {
                        return null;
@@ -366,15 +286,15 @@ public class ByteUtil {
                        } else if (obj instanceof Float) {
                                float f = (Float) obj;
                                temp = ByteUtil.concatByteArray(temp,
-                                               ByteUtil.floatToBytes(f));
+                                               ByteUtil.toBytes(f));
                        } else if (obj instanceof Double) {
                                double d = (Double) obj;
                                temp = ByteUtil.concatByteArray(temp,
-                                               ByteUtil.doubleToBytes(d));
+                                               ByteUtil.toBytes(d));
                        } else if (obj instanceof String) {
                                String value = (String) obj;
                                temp = ByteUtil.concatByteArray(temp,
-                                               ByteUtil.stringToByte(value));
+                                               ByteUtil.toByte(value));
                        } else if (obj instanceof byte[]) {
                                temp = ByteUtil.concatByteArray(temp, (byte[]) obj);
                        } else if (obj instanceof List<?>) {
@@ -405,25 +325,44 @@ public class ByteUtil {
                return temp;
        }
 
-       public static String getString(byte[] data, int start) {
-               int length = getStringLength(data, start);
-               byte[] temp = new byte[length];
-               System.arraycopy(data, start, temp, 0, length);
-               String str = new String(temp);
-               str = str.substring(0, str.length() - 1);
-               return str;
+       public static byte[] concatByteArray(byte[] front, byte[] rear) {
+               if (null == front && null == rear) {
+                       return null;
+               } else if (null == front && null != rear) {
+                       return rear;
+               } else if (null != front && null == rear) {
+                       return front;
+               }
+
+               byte[] result = new byte[front.length + rear.length];
+               System.arraycopy(front, 0, result, 0, front.length);
+               System.arraycopy(rear, 0, result, front.length, rear.length);
+
+               return result;
        }
 
-       public static int getStringLength(byte[] data, int start) {
-               int length = 0;
-               for (int i = start; i < data.length; i++, length++) {
-                       if (data[i] == 0) {
-                               return length + 1;
+       public static byte[] concatByteArray(final Object... byteArrays) {
+               if (null == byteArrays) {
+                       return null;
+               }
+               byte[] temp = new byte[0];
+               for (Object obj : byteArrays) {
+                       if (null == obj) {
+                               continue;
+                       }
+                       else {
+                               if (recursive_checker == 10) {
+                                       DA_LOG.error("There possibly recursive call is occurred.");
+                               }
+                               recursive_checker++;
+                               temp = concatByteArray(temp, getByte(obj));
+                               recursive_checker--;
                        }
                }
-               return data.length;
+               return temp;
        }
 
+       // TODO: move to DALogger
        public static void printByteArray(byte[] input) {
                // System.out.println(" ====---- print byte array start----====");
                int line = 0;
@@ -441,6 +380,7 @@ public class ByteUtil {
                // System.out.println(" ====---- print byte array end----====");
        }
 
+       // TODO: move to DALogger
        public static void printByteArrayForStart(byte[] input) {
                Logger.debug(" ====---- print byte array start----====");
                int line = 0;
@@ -457,4 +397,97 @@ public class ByteUtil {
                }
                Logger.debug(" ====---- print byte array end----====");
        }
+
+       /**
+        * Convert string to byte
+        * 
+        * ByteUtils.toByte("1", *) = 0x01 ByteUtils.toByte("-1", *) = 0xff
+        * 
+        * @param value
+        * @param defaultValue
+        * @return
+        */
+       @Deprecated
+       public static byte toByte(String value, byte defaultValue) {
+               try {
+                       return Byte.parseByte(value);
+               } catch (Exception e) {
+                       return defaultValue;
+               }
+       }
+
+       /**
+        * Convert the string that formatted Octal or decimal or hexadecimal to byte array.
+        * Case of octal and Decimal, that change to three-digit string.
+        * Case of hexadecimal, that change to one byte. 
+        *       * 
+        * ByteUtils.toBytes(null) = null ByteUtils.toBytes("0E1F4E", 16) = [0x0e,
+        * 0xf4, 0x4e] ByteUtils.toBytes("48414e", 16) = [0x48, 0x41, 0x4e]
+        * 
+        * @param digits
+        *            string of number
+        * @param radix
+        *            base(can be 8, 10, 16)
+        * @return
+        * @throws NumberFormatException
+        */
+       @Deprecated
+       public static byte[] toBytes(String digits, int radix)
+                       throws IllegalArgumentException, NumberFormatException {
+               if (digits == null) {
+                       return null;
+               }
+               if (radix != 16 && radix != 10 && radix != 8) {
+                       throw new IllegalArgumentException("For input radix: \"" + radix
+                                       + "\"");
+               }
+               int divLen = (radix == 16) ? 2 : 3;
+               int length = digits.length();
+               if (length % divLen == 1) {
+                       throw new IllegalArgumentException("For input string: \"" + digits
+                                       + "\"");
+               }
+               length = length / divLen;
+               byte[] bytes = new byte[length];
+               for (int i = 0; i < length; i++) {
+                       int index = i * divLen;
+                       bytes[i] = (byte) (Short.parseShort(
+                                       digits.substring(index, index + divLen), radix));
+               }
+               return bytes;
+       }
+
+       /**
+        * Convert hexadecimal formatted string to byte array/
+        * The two-digit string change to one byte. 
+        * 
+        * ByteUtils.hexStringToBytes(null) = null
+        * ByteUtils.hexStringToBytes("0E1F4E") = [0x0e, 0xf4, 0x4e]
+        * ByteUtils.hexStringToBytes("48414e") = [0x48, 0x41, 0x4e]
+        * 
+        * @param digits
+        *            string of base-16 number
+        * @return
+        * @throws NumberFormatException
+        */
+       @Deprecated
+       public static byte[] hexStringToBytes(String digits)
+                       throws IllegalArgumentException, NumberFormatException {
+               if (digits == null) {
+                       return null;
+               }
+               int length = digits.length();
+               if (length % 2 == 1) {
+                       throw new IllegalArgumentException("For input string: \"" + digits
+                                       + "\"");
+               }
+               length = length / 2;
+               byte[] bytes = new byte[length];
+               for (int i = 0; i < length; i++) {
+                       int index = i * 2;
+                       bytes[i] = (byte) (Short.parseShort(
+                                       digits.substring(index, index + 2), 16));
+               }
+               return bytes;
+       }
 }