summaryrefslogtreecommitdiff
path: root/org.tizen.common.sdblib
diff options
context:
space:
mode:
authorBonyong.lee <bonyong.lee@samsung.com>2013-08-21 11:58:33 +0900
committerBonyong.lee <bonyong.lee@samsung.com>2013-08-21 11:58:33 +0900
commit54f206ff08b7f1d6dded17f24b67f73be0f8d199 (patch)
tree40788cb26f0d54a5e4b5e53dba4770972f168c93 /org.tizen.common.sdblib
parent74e746f31d945aa01b5450f8ebc6fe5a851be41a (diff)
downloadcommon-eplugin-54f206ff08b7f1d6dded17f24b67f73be0f8d199.tar.gz
common-eplugin-54f206ff08b7f1d6dded17f24b67f73be0f8d199.tar.bz2
common-eplugin-54f206ff08b7f1d6dded17f24b67f73be0f8d199.zip
[Title] sdblib refactoring
[Desc.] ByteUtil modification. Add testcase, change signature and logic, add comment, and fix potential warning [Issue]
Diffstat (limited to 'org.tizen.common.sdblib')
-rw-r--r--org.tizen.common.sdblib/build.properties1
-rw-r--r--org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryInputStream.java7
-rw-r--r--org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryOutputStream.java11
-rwxr-xr-xorg.tizen.common.sdblib/src/org/tizen/sdblib/util/ByteUtil.java118
-rw-r--r--org.tizen.common.sdblib/test/src/org/tizen/sdblib/DeviceTest.java12
-rw-r--r--org.tizen.common.sdblib/test/src/org/tizen/sdblib/SdbHelperTest.java17
-rw-r--r--org.tizen.common.sdblib/test/src/org/tizen/sdblib/util/ByteUtilTest.java74
7 files changed, 175 insertions, 65 deletions
diff --git a/org.tizen.common.sdblib/build.properties b/org.tizen.common.sdblib/build.properties
index c58ea2178..b04195ad0 100644
--- a/org.tizen.common.sdblib/build.properties
+++ b/org.tizen.common.sdblib/build.properties
@@ -3,3 +3,4 @@ output.. = bin/
bin.includes = META-INF/,\
.,\
OSGI-INF/
+src.excludes = test/src/
diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryInputStream.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryInputStream.java
index 638fb4713..ae9c2cc4f 100644
--- a/org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryInputStream.java
+++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryInputStream.java
@@ -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;
import static org.tizen.sdblib.service.SyncServiceReturnCode.RESULT_REMOTE_PATH_LENGTH;
import static org.tizen.sdblib.util.Assert.notNull;
+import static org.tizen.sdblib.util.ByteUtil.fromBytes;
+import static org.tizen.sdblib.util.ByteUtil.toBytes;
import java.io.IOException;
import java.io.InputStream;
@@ -53,7 +55,6 @@ import org.tizen.sdblib.SdbResponse;
import org.tizen.sdblib.SmartDevelopmentBridge;
import org.tizen.sdblib.exception.SyncException;
import org.tizen.sdblib.exception.TimeoutException;
-import org.tizen.sdblib.util.ByteUtil;
import org.tizen.sdblib.util.Log;
/**
@@ -121,7 +122,7 @@ extends InputStream
final byte[] array = new byte[8 + path.length];
System.arraycopy( command, 0, array, 0, 4 );
- ByteUtil.swap32bitsToArray( path.length, array, 4 );
+ toBytes( path.length, array, 4 );
System.arraycopy( path, 0, array, 8, path.length );
return array;
@@ -215,7 +216,7 @@ extends InputStream
// if we're done, we stop
return -1;
}
- length = ByteUtil.swap32bitFromArray( header, 4 );
+ length = fromBytes( header, 4 );
if ( length > SYNC_DATA_MAX )
{
// buffer overrun!
diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryOutputStream.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryOutputStream.java
index 61a96b018..bbe555395 100644
--- a/org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryOutputStream.java
+++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/service/FileEntryOutputStream.java
@@ -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;
import static org.tizen.sdblib.service.SyncServiceReturnCode.RESULT_UNKNOWN_ERROR;
import static org.tizen.sdblib.util.Assert.notNull;
+import static org.tizen.sdblib.util.ByteUtil.fromBytes;
+import static org.tizen.sdblib.util.ByteUtil.toBytes;
import java.io.IOException;
import java.io.OutputStream;
@@ -57,7 +59,6 @@ import org.tizen.sdblib.SdbResponse;
import org.tizen.sdblib.SmartDevelopmentBridge;
import org.tizen.sdblib.exception.SyncException;
import org.tizen.sdblib.exception.TimeoutException;
-import org.tizen.sdblib.util.ByteUtil;
import org.tizen.sdblib.util.Log;
/**
@@ -199,7 +200,7 @@ extends OutputStream
}
// now send the data to the device
// first write the amount read
- ByteUtil.swap32bitsToArray( index - 8, buffer, 4 );
+ toBytes( index - 8, buffer, 4 );
// now write it
try
@@ -245,7 +246,7 @@ extends OutputStream
final byte[] array = new byte[8 + path.length + modeContent.length];
System.arraycopy( command, 0, array, 0, 4 );
- ByteUtil.swap32bitsToArray( path.length + modeContent.length, array, 4 );
+ toBytes( path.length + modeContent.length, array, 4 );
System.arraycopy( path, 0, array, 8, path.length );
System.arraycopy( modeContent, 0, array, 8 + path.length, modeContent.length );
@@ -274,7 +275,7 @@ extends OutputStream
final byte[] array = new byte[8];
System.arraycopy(command, 0, array, 0, 4);
- ByteUtil.swap32bitsToArray(value, array, 4);
+ toBytes(value, array, 4);
return array;
}
@@ -303,7 +304,7 @@ extends OutputStream
if ( check( result, 0, ID_FAIL, 0, 4 ) )
{
// read some error message...
- int len = ByteUtil.swap32bitFromArray(result, 4);
+ int len = fromBytes( result, 4 );
read(channel, buffer, len, timeOut);
diff --git a/org.tizen.common.sdblib/src/org/tizen/sdblib/util/ByteUtil.java b/org.tizen.common.sdblib/src/org/tizen/sdblib/util/ByteUtil.java
index 7fdcd05ee..7cdbf9a8e 100755
--- a/org.tizen.common.sdblib/src/org/tizen/sdblib/util/ByteUtil.java
+++ b/org.tizen.common.sdblib/src/org/tizen/sdblib/util/ByteUtil.java
@@ -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 );
}
+
+
}
diff --git a/org.tizen.common.sdblib/test/src/org/tizen/sdblib/DeviceTest.java b/org.tizen.common.sdblib/test/src/org/tizen/sdblib/DeviceTest.java
index 0ebb805ec..f50b410ca 100644
--- a/org.tizen.common.sdblib/test/src/org/tizen/sdblib/DeviceTest.java
+++ b/org.tizen.common.sdblib/test/src/org/tizen/sdblib/DeviceTest.java
@@ -51,11 +51,23 @@ import org.tizen.sdblib.command.CommandFactory;
import org.tizen.sdblib.command.Matcher;
import org.tizen.sdblib.util.IOUtil;
+/**
+ * DeviceTest
+ *
+ * Testcase for {@link Device}
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
@RunWith( PowerMockRunner.class )
public class
DeviceTest
{
+ /**
+ * test for {@link Device#getAppInstallPath()}
+ *
+ * @throws Exception if test fails
+ */
@Test
@PrepareForTest( { SmartDevelopmentBridge.class, CommandFactory.class, IOUtil.class } )
public
diff --git a/org.tizen.common.sdblib/test/src/org/tizen/sdblib/SdbHelperTest.java b/org.tizen.common.sdblib/test/src/org/tizen/sdblib/SdbHelperTest.java
index 2da7e486f..e444e7ce3 100644
--- a/org.tizen.common.sdblib/test/src/org/tizen/sdblib/SdbHelperTest.java
+++ b/org.tizen.common.sdblib/test/src/org/tizen/sdblib/SdbHelperTest.java
@@ -25,13 +25,26 @@
*/
package org.tizen.sdblib;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertTrue;
import org.junit.Test;
-public class SdbHelperTest
+/**
+ * SdbHelperTest
+ *
+ * Testcase for {@link SdbHelper}
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
+public class
+SdbHelperTest
{
+ /**
+ * test for {@link SdbHelper#check(byte[], int, byte[], int, int)}
+ *
+ * @throws Exception if test fails
+ */
@Test
public
void
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
index 000000000..109ca6554
--- /dev/null
+++ b/org.tizen.common.sdblib/test/src/org/tizen/sdblib/util/ByteUtilTest.java
@@ -0,0 +1,74 @@
+/*
+ * sdblib
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Kangho Kim <kh5325.kim@samsung.com>
+ * BonYong Lee <bonyong.lee@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+package org.tizen.sdblib.util;
+
+import static java.lang.System.currentTimeMillis;
+import static org.junit.Assert.assertEquals;
+import static org.tizen.sdblib.util.ByteUtil.fromBytes;
+import static org.tizen.sdblib.util.ByteUtil.toBytes;
+
+import java.util.Random;
+
+import org.junit.Test;
+
+/**
+ * ByteUtilTest
+ *
+ * Testcase for {@link ByteUtil}
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
+public class
+ByteUtilTest
+{
+
+ /**
+ * test for {@link ByteUtil#fromBytes(byte[], int, int)}
+ * and {@link ByteUtil#toBytes(int, byte[], int, int)}
+ *
+ * @throws Exception if test fails
+ */
+ @Test
+ public
+ void
+ test_toBytes_fromBytes()
+ {
+ Random r = new Random( currentTimeMillis() );
+ for ( int i = 0, n = 10000 ; i<n ; ++i )
+ {
+ final int input = r.nextInt();
+
+ final byte[] result = new byte[8];
+ for ( int offset = 0 ; offset < result.length - 4 ; ++offset )
+ {
+ toBytes( input, result, offset, 4 );
+ assertEquals( input, (int) fromBytes( result, offset, 4 ) );
+
+ }
+ }
+ }
+
+}