diff options
author | Austin Kirk <austin@livio.io> | 2017-05-16 14:22:26 -0400 |
---|---|---|
committer | Joey Grover <joeygrover@gmail.com> | 2017-05-16 19:22:26 +0100 |
commit | 3569adec11d2bf7a8cb6f7e8ffd010f44222bce5 (patch) | |
tree | c80ff59cf49003196d74e0e84bedce75e044dbcc /sdl_android | |
parent | 832471ccf723126732d4fd2f3c2db0eba9fce97c (diff) | |
download | sdl_android-3569adec11d2bf7a8cb6f7e8ffd010f44222bce5.tar.gz |
Adding null checks to BitConverter. Also included better java docs and tests.
* Adding null checks
Now returns -1 or null object upon null input array
* Fixing Javadocs
Diffstat (limited to 'sdl_android')
-rw-r--r-- | sdl_android/src/androidTest/java/com/smartdevicelink/test/util/BitConverterTests.java | 10 | ||||
-rw-r--r-- | sdl_android/src/main/java/com/smartdevicelink/util/BitConverter.java | 34 |
2 files changed, 35 insertions, 9 deletions
diff --git a/sdl_android/src/androidTest/java/com/smartdevicelink/test/util/BitConverterTests.java b/sdl_android/src/androidTest/java/com/smartdevicelink/test/util/BitConverterTests.java index a3f0964b4..fb6eed3f9 100644 --- a/sdl_android/src/androidTest/java/com/smartdevicelink/test/util/BitConverterTests.java +++ b/sdl_android/src/androidTest/java/com/smartdevicelink/test/util/BitConverterTests.java @@ -1,11 +1,11 @@ package com.smartdevicelink.test.util; -import java.util.Arrays; +import com.smartdevicelink.test.Test; +import com.smartdevicelink.util.BitConverter; import junit.framework.TestCase; -import com.smartdevicelink.test.Test; -import com.smartdevicelink.util.BitConverter; +import java.util.Arrays; /** * This is a unit test class for the SmartDeviceLink library project class : @@ -61,7 +61,7 @@ public class BitConverterTests extends TestCase { assertTrue(Test.ARRAY, Arrays.equals(expectedBytes, actualBytes)); // Invalid/Null Tests - assertEquals(Test.MATCH, (int) 0, actualNullBytes); + assertEquals(Test.MATCH, (int) -1, actualNullBytes); } /** @@ -83,6 +83,6 @@ public class BitConverterTests extends TestCase { assertTrue(Test.ARRAY, Arrays.equals(expectedBytes, actualBytes)); // Invalid/Null Tests - assertEquals(Test.MATCH, (short) 0, actualNullBytes); + assertEquals(Test.MATCH, (short) -1, actualNullBytes); } }
\ No newline at end of file diff --git a/sdl_android/src/main/java/com/smartdevicelink/util/BitConverter.java b/sdl_android/src/main/java/com/smartdevicelink/util/BitConverter.java index c844e49a4..a681a75a8 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/util/BitConverter.java +++ b/sdl_android/src/main/java/com/smartdevicelink/util/BitConverter.java @@ -1,10 +1,21 @@ package com.smartdevicelink.util;
public class BitConverter {
+ /**
+ * @param bytes byte array that will be converted to hex
+ * @return the String containing converted hex values or null if byte array is null
+ */
public static String bytesToHex(byte [] bytes) {
+ if (bytes == null) { return null; }
return bytesToHex(bytes, 0, bytes.length);
} // end-method
+ /**
+ * @param bytes byte array that will be converted to hex
+ * @param offset int representing the offset to begin conversion at
+ * @param length int representing number of bytes in array to convert
+ * @return the String containing converted hex values or null if byte array is null
+ */
public static String bytesToHex(byte[] bytes, int offset, int length) {
if (bytes == null) { return null; }
final char[] HexDigits = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
@@ -24,6 +35,10 @@ public class BitConverter { return new String(hexChars);
} // end-method
+ /**
+ * @param hexString the String containing converted hex values
+ * @return byte array converted from input String or null if String is null
+ */
public static byte [] hexToBytes(String hexString) {
if (hexString == null) { return null; }
if (hexString.length() % 2 != 0) {
@@ -45,8 +60,13 @@ public class BitConverter { (byte)(value >>> 8),
(byte)value};
}
-
+
+ /**
+ * @param sizeBuf byte array that will be converted to int
+ * @return int converted from byte array or -1 if byte array is null
+ */
public static int intFromByteArray(byte[] sizeBuf, int offset) {
+ if (sizeBuf == null) { return -1; }
int ret = 0;
for (int i = offset; i < offset + 4; i++) {
ret <<= 8;
@@ -60,8 +80,13 @@ public class BitConverter { (byte)(value >>> 8),
(byte)value};
}
-
+
+ /**
+ * @param sizeBuf byte array that will be converted to short
+ * @return short converted from byte array or -1 if byte array is null
+ */
public static short shortFromByteArray(byte[] sizeBuf, int offset) {
+ if (sizeBuf == null) { return -1; }
short ret = 0;
for (int i = offset; i < offset + 2; i++) {
ret <<= 8;
@@ -72,11 +97,12 @@ public class BitConverter { /**
* Converts the byte array into a string of hex values.
- * @param bytes
+ * @param bytes byte array that will be converted to hex
* @param end EXCLUSIVE so if it it receives 10 it will print 0-9
- * @return
+ * @return the String containing converted hex values or null if byte array is null
*/
public static String bytesToHex(byte[] bytes,int end){
+ if (bytes == null) { return null; }
if(bytes.length<end){
end = bytes.length;
}
|