[Title] sdblib refactoring
authorBonyong.lee <bonyong.lee@samsung.com>
Wed, 21 Aug 2013 02:58:33 +0000 (11:58 +0900)
committerBonyong.lee <bonyong.lee@samsung.com>
Wed, 21 Aug 2013 02:58:33 +0000 (11:58 +0900)
[Desc.] ByteUtil modification. Add testcase, change signature and logic,
add comment, and fix potential warning
[Issue]

org.tizen.common.sdblib/build.properties
org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryInputStream.java
org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryOutputStream.java
org.tizen.common.sdblib/src/org/tizen/sdblib/util/ByteUtil.java
org.tizen.common.sdblib/test/src/org/tizen/sdblib/DeviceTest.java
org.tizen.common.sdblib/test/src/org/tizen/sdblib/SdbHelperTest.java
org.tizen.common.sdblib/test/src/org/tizen/sdblib/util/ByteUtilTest.java [new file with mode: 0644]

index c58ea21..b04195a 100644 (file)
@@ -3,3 +3,4 @@ output.. = bin/
 bin.includes = META-INF/,\
                .,\
                OSGI-INF/
+src.excludes = test/src/
index 638fb47..ae9c2cc 100644 (file)
@@ -42,6 +42,8 @@ import static org.tizen.sdblib.service.SyncServiceReturnCode.RESULT_CONNECTION_T
 import static org.tizen.sdblib.service.SyncServiceReturnCode.RESULT_REMOTE_PATH_ENCODING;\r
 import static org.tizen.sdblib.service.SyncServiceReturnCode.RESULT_REMOTE_PATH_LENGTH;\r
 import static org.tizen.sdblib.util.Assert.notNull;\r
+import static org.tizen.sdblib.util.ByteUtil.fromBytes;\r
+import static org.tizen.sdblib.util.ByteUtil.toBytes;\r
 \r
 import java.io.IOException;\r
 import java.io.InputStream;\r
@@ -53,7 +55,6 @@ import org.tizen.sdblib.SdbResponse;
 import org.tizen.sdblib.SmartDevelopmentBridge;\r
 import org.tizen.sdblib.exception.SyncException;\r
 import org.tizen.sdblib.exception.TimeoutException;\r
-import org.tizen.sdblib.util.ByteUtil;\r
 import org.tizen.sdblib.util.Log;\r
 \r
 /**\r
@@ -121,7 +122,7 @@ extends InputStream
         final byte[] array = new byte[8 + path.length];\r
 \r
         System.arraycopy( command, 0, array, 0, 4 );\r
-        ByteUtil.swap32bitsToArray( path.length, array, 4 );\r
+        toBytes( path.length, array, 4 );\r
         System.arraycopy( path, 0, array, 8, path.length );\r
 \r
         return array;\r
@@ -215,7 +216,7 @@ extends InputStream
                                // if we're done, we stop\r
                                return -1;\r
                        }\r
-               length = ByteUtil.swap32bitFromArray( header, 4 );\r
+               length = fromBytes( header, 4 );\r
                if ( length > SYNC_DATA_MAX )\r
                {\r
                    // buffer overrun!\r
index 61a96b0..bbe5553 100644 (file)
@@ -46,6 +46,8 @@ import static org.tizen.sdblib.service.SyncServiceReturnCode.RESULT_REMOTE_PATH_
 import static org.tizen.sdblib.service.SyncServiceReturnCode.RESULT_REMOTE_PATH_LENGTH;\r
 import static org.tizen.sdblib.service.SyncServiceReturnCode.RESULT_UNKNOWN_ERROR;\r
 import static org.tizen.sdblib.util.Assert.notNull;\r
+import static org.tizen.sdblib.util.ByteUtil.fromBytes;\r
+import static org.tizen.sdblib.util.ByteUtil.toBytes;\r
 \r
 import java.io.IOException;\r
 import java.io.OutputStream;\r
@@ -57,7 +59,6 @@ import org.tizen.sdblib.SdbResponse;
 import org.tizen.sdblib.SmartDevelopmentBridge;\r
 import org.tizen.sdblib.exception.SyncException;\r
 import org.tizen.sdblib.exception.TimeoutException;\r
-import org.tizen.sdblib.util.ByteUtil;\r
 import org.tizen.sdblib.util.Log;\r
 \r
 /**\r
@@ -199,7 +200,7 @@ extends OutputStream
                }\r
         // now send the data to the device\r
         // first write the amount read\r
-        ByteUtil.swap32bitsToArray( index - 8, buffer, 4 );\r
+        toBytes( index - 8, buffer, 4 );\r
 \r
         // now write it\r
         try\r
@@ -245,7 +246,7 @@ extends OutputStream
                final byte[] array = new byte[8 + path.length + modeContent.length];\r
                \r
                System.arraycopy( command, 0, array, 0, 4 );\r
-               ByteUtil.swap32bitsToArray( path.length + modeContent.length, array, 4 );\r
+               toBytes( path.length + modeContent.length, array, 4 );\r
                System.arraycopy( path, 0, array, 8, path.length );\r
                System.arraycopy( modeContent, 0, array, 8 + path.length, modeContent.length );\r
                \r
@@ -274,7 +275,7 @@ extends OutputStream
         final byte[] array = new byte[8];\r
 \r
         System.arraycopy(command, 0, array, 0, 4);\r
-        ByteUtil.swap32bitsToArray(value, array, 4);\r
+        toBytes(value, array, 4);\r
 \r
         return array;\r
     }\r
@@ -303,7 +304,7 @@ extends OutputStream
             if ( check( result, 0, ID_FAIL, 0, 4 ) )\r
             {\r
                 // read some error message...\r
-                int len = ByteUtil.swap32bitFromArray(result, 4);\r
+                int len = fromBytes( result, 4 );\r
 \r
                 read(channel, buffer, len, timeOut);\r
 \r
index 7fdcd05..7cdbf9a 100755 (executable)
@@ -27,7 +27,8 @@ package org.tizen.sdblib.util;
 
 /**
  * ByteUtil
- * Helper for byte using communication
+ * 
+ * Helper for handling bytes
  *  
  * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
  */
@@ -38,85 +39,92 @@ ByteUtil
        protected ByteUtil() {}
 
     /**
-     * Swaps an unsigned value around, and puts the result in an array that can be sent to a device.
-     * @param value The value to swap.
-     * @param dest the destination array
-     * @param offset the offset in the array where to put the swapped value.
-     *      Array length must be at least offset + 4
+     * write {@code src} from {@code offset} in {@code dest} as byte series
+     * 
+     * @param src data to write
+     * @param dest byte array to write to
+     * @param offset start index to write to
+     * @param length byte's length to write
      */
     public static
     void
-    swap32bitsToArray(
-       final int value,
+    toBytes(
+       final int src,
        final byte[] dest,
-       final int offset
+       final int offset,
+       final int length
     )
     {
-        dest[offset] = (byte)(value & 0x000000FF);
-        dest[offset + 1] = (byte)((value & 0x0000FF00) >> 8);
-        dest[offset + 2] = (byte)((value & 0x00FF0000) >> 16);
-        dest[offset + 3] = (byte)((value & 0xFF000000) >> 24);
+        for ( int i = 0 ; i < length ; ++i )
+        {
+            dest[offset + i] = (byte) (( src >> ( 8 * i ) ) & 0x000000FF );
+        }
     }
-
+    
     /**
-     * Reads a signed 32 bit integer from an array coming from a device.
-     * @param value the array containing the int
-     * @param offset the offset in the array at which the int starts
-     * @return the integer read from the array
+     * write {@code src}, int type from {@code offset} in {@code dest} as byte series
+     * 
+     * @param src data to write
+     * @param dest byte array to write to
+     * @param offset start index to write to
+     * 
+     * @see #toBytes(int, byte[], int, int)
      */
     public static
-    int
-    swap32bitFromArray(
-       final byte[] value,
-       final int offset
+    void
+    toBytes(
+        final int src,
+        final byte[] dest,
+        final int offset
     )
     {
-        return (((int)value[offset]) & 0x000000FF)
-        | ((((int)value[offset + 1]) & 0x000000FF) << 8)
-        | ((((int)value[offset + 2]) & 0x000000FF) << 16)
-        | ((((int)value[offset + 3]) & 0x000000FF) << 24);
+        toBytes( src, dest, offset, 4 );
     }
     
     /**
-     * Reads an unsigned 16 bit integer from an array coming from a device,
-     * and returns it as an 'int'
-     * @param value the array containing the 16 bit int (2 byte).
-     * @param offset the offset in the array at which the int starts
-     *      Array length must be at least offset + 2
-     * @return the integer read from the array.
+     * read from byte series and return long type value
+     * 
+     * @param value byte array to read from
+     * @param offset start index of byte array
+     * @param length length to read
+     * @return value to read
      */
     public static
-    int
-    swapU16bitFromArray(
-       final byte[] value,
-       final int offset
+    long
+    fromBytes(
+        final byte[] value,
+        final int offset,
+        final int length
     )
     {
-        return (((int)value[offset]) & 0x000000FF )
-        | ((((int)value[offset + 1]) & 0x000000FF) << 8);
+        long result = 0;
+        for ( int i = 0, n = length ; i < n ; ++i )
+        {
+            result |= ((int) value[offset+i] & 0x000000FF ) << ( 8 * i );
+        }
+        
+        return result;
     }
     
     /**
-     * Reads a signed 64 bit integer from an array coming from a device.
-     * @param value the array containing the int
-     * @param offset the offset in the array at which the int starts
-     *      Array length must be at least offset + 8
-     * @return the integer read from the array
+     * read 4 bytes from byte series and return int type value
+     * 
+     * @param value byte array to read from
+     * @param offset start index of byte array
+     * 
+     * @return value to read
+     * 
+     * @see #fromBytes(byte[], int, int)
      */
     public static
-    long
-    swap64bitFromArray(
-       final byte[] value,
-       final int offset
+    int
+    fromBytes(
+        final byte[] value,
+        final int offset
     )
     {
-        return (((long)value[offset]) & 0x00000000000000FFL)
-        | ((((long)value[offset + 1]) & 0x00000000000000FFL) << 8)
-        | ((((long)value[offset + 2]) & 0x00000000000000FFL) << 16)
-        | ((((long)value[offset + 3]) & 0x00000000000000FFL) << 24)
-        | ((((long)value[offset + 4]) & 0x00000000000000FFL) << 32)
-        | ((((long)value[offset + 5]) & 0x00000000000000FFL) << 40)
-        | ((((long)value[offset + 6]) & 0x00000000000000FFL) << 48)
-        | ((((long)value[offset + 7]) & 0x00000000000000FFL) << 56);
+        return (int) fromBytes( value, offset, 4 );
     }
+    
+
 }
index 0ebb805..f50b410 100644 (file)
@@ -51,11 +51,23 @@ import org.tizen.sdblib.command.CommandFactory;
 import org.tizen.sdblib.command.Matcher;\r
 import org.tizen.sdblib.util.IOUtil;\r
 \r
+/**\r
+ * DeviceTest\r
+ *\r
+ * Testcase for {@link Device}\r
+ * \r
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)\r
+ */\r
 @RunWith( PowerMockRunner.class )\r
 public class\r
 DeviceTest\r
 {\r
 \r
+    /**\r
+     * test for {@link Device#getAppInstallPath()}\r
+     * \r
+     * @throws Exception if test fails\r
+     */\r
        @Test\r
        @PrepareForTest( { SmartDevelopmentBridge.class, CommandFactory.class, IOUtil.class } )\r
        public\r
index 2da7e48..e444e7c 100644 (file)
  */\r
 package org.tizen.sdblib;\r
 \r
-import static org.junit.Assert.*;\r
+import static org.junit.Assert.assertTrue;\r
 \r
 import org.junit.Test;\r
 \r
-public class SdbHelperTest\r
+/**\r
+ * SdbHelperTest\r
+ *\r
+ * Testcase for {@link SdbHelper}\r
+ * \r
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)\r
+ */\r
+public class\r
+SdbHelperTest\r
 {\r
 \r
+    /**\r
+     * test for {@link SdbHelper#check(byte[], int, byte[], int, int)}\r
+     * \r
+     * @throws Exception if test fails\r
+     */\r
        @Test\r
        public\r
        void\r
diff --git a/org.tizen.common.sdblib/test/src/org/tizen/sdblib/util/ByteUtilTest.java b/org.tizen.common.sdblib/test/src/org/tizen/sdblib/util/ByteUtilTest.java
new file mode 100644 (file)
index 0000000..109ca65
--- /dev/null
@@ -0,0 +1,74 @@
+/*\r
+ *  sdblib\r
+ *\r
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
+ *\r
+ * Contact: \r
+ * Kangho Kim <kh5325.kim@samsung.com>\r
+ * BonYong Lee <bonyong.lee@samsung.com>\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * Contributors:\r
+ * - S-Core Co., Ltd\r
+ *\r
+ */\r
+package org.tizen.sdblib.util;\r
+\r
+import static java.lang.System.currentTimeMillis;\r
+import static org.junit.Assert.assertEquals;\r
+import static org.tizen.sdblib.util.ByteUtil.fromBytes;\r
+import static org.tizen.sdblib.util.ByteUtil.toBytes;\r
+\r
+import java.util.Random;\r
+\r
+import org.junit.Test;\r
+\r
+/**\r
+ * ByteUtilTest\r
+ *\r
+ * Testcase for {@link ByteUtil}\r
+ * \r
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)\r
+ */\r
+public class\r
+ByteUtilTest\r
+{\r
+\r
+    /**\r
+     * test for {@link ByteUtil#fromBytes(byte[], int, int)}\r
+     * and {@link ByteUtil#toBytes(int, byte[], int, int)}\r
+     * \r
+     * @throws Exception if test fails\r
+     */\r
+    @Test\r
+    public\r
+    void\r
+    test_toBytes_fromBytes()\r
+    {\r
+        Random r = new Random( currentTimeMillis() );\r
+        for ( int i = 0, n = 10000 ; i<n ; ++i )\r
+        {\r
+            final int input = r.nextInt();\r
+            \r
+            final byte[] result = new byte[8];\r
+            for ( int offset = 0 ; offset < result.length - 4 ; ++offset )\r
+            {\r
+                toBytes( input, result, offset, 4 );\r
+                assertEquals( input, (int) fromBytes( result, offset, 4 ) );\r
+                \r
+            }\r
+        }\r
+    }\r
+\r
+}\r