Bug-731: Fixed few major Sonar warnings
[bgpcep.git] / util / src / main / java / org / opendaylight / protocol / util / ByteArray.java
index bd58afae1eb59e88b7c4df7951fb7370a0b44d84..83713c3420512a922773117f6954c32b0be81d65 100644 (file)
@@ -91,8 +91,7 @@ public final class ByteArray {
      * @return a new byte array that is a sub-array of the original
      */
     public static byte[] subByte(final byte[] bytes, final int startIndex, final int length) {
-        if (bytes.length == 0 || length < 0 || length > bytes.length || startIndex < 0 || startIndex > bytes.length
-            || startIndex + length > bytes.length) {
+        if (!checkLength(bytes, length) || !checkStartIndex(bytes, startIndex, length)) {
             throw new IllegalArgumentException("Cannot create subByte, invalid arguments: Length: " + length + " startIndex: " + startIndex);
         }
         final byte[] res = new byte[length];
@@ -100,6 +99,14 @@ public final class ByteArray {
         return res;
     }
 
+    private static boolean checkLength(final byte[] bytes, final int length) {
+        return length > 0 && bytes.length > 0 && length <= bytes.length;
+    }
+
+    private static boolean checkStartIndex(final byte[] bytes, final int startIndex, final int length) {
+        return startIndex >= 0 && startIndex < bytes.length && (startIndex + length <= bytes.length);
+    }
+
     /**
      * Converts byte array to Integer. If there are less bytes in the array as required (4), the method will push
      * adequate number of zero bytes prepending given byte array.