1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.security.keystore;
18 
19 import libcore.util.EmptyArray;
20 
21 /**
22  * @hide
23  */
24 public abstract class ArrayUtils {
ArrayUtils()25     private ArrayUtils() {}
26 
nullToEmpty(String[] array)27     public static String[] nullToEmpty(String[] array) {
28         return (array != null) ? array : EmptyArray.STRING;
29     }
30 
cloneIfNotEmpty(String[] array)31     public static String[] cloneIfNotEmpty(String[] array) {
32         return ((array != null) && (array.length > 0)) ? array.clone() : array;
33     }
34 
cloneIfNotEmpty(byte[] array)35     public static byte[] cloneIfNotEmpty(byte[] array) {
36         return ((array != null) && (array.length > 0)) ? array.clone() : array;
37     }
38 
concat(byte[] arr1, byte[] arr2)39     public static byte[] concat(byte[] arr1, byte[] arr2) {
40         return concat(arr1, 0, (arr1 != null) ? arr1.length : 0,
41                 arr2, 0, (arr2 != null) ? arr2.length : 0);
42     }
43 
concat(byte[] arr1, int offset1, int len1, byte[] arr2, int offset2, int len2)44     public static byte[] concat(byte[] arr1, int offset1, int len1, byte[] arr2, int offset2,
45             int len2) {
46         if (len1 == 0) {
47             return subarray(arr2, offset2, len2);
48         } else if (len2 == 0) {
49             return subarray(arr1, offset1, len1);
50         } else {
51             byte[] result = new byte[len1 + len2];
52             System.arraycopy(arr1, offset1, result, 0, len1);
53             System.arraycopy(arr2, offset2, result, len1, len2);
54             return result;
55         }
56     }
57 
subarray(byte[] arr, int offset, int len)58     public static byte[] subarray(byte[] arr, int offset, int len) {
59         if (len == 0) {
60             return EmptyArray.BYTE;
61         }
62         if ((offset == 0) && (len == arr.length)) {
63             return arr;
64         }
65         byte[] result = new byte[len];
66         System.arraycopy(arr, offset, result, 0, len);
67         return result;
68     }
69 
concat(int[] arr1, int[] arr2)70     public static int[] concat(int[] arr1, int[] arr2) {
71         if ((arr1 == null) || (arr1.length == 0)) {
72             return arr2;
73         } else if ((arr2 == null) || (arr2.length == 0)) {
74             return arr1;
75         } else {
76             int[] result = new int[arr1.length + arr2.length];
77             System.arraycopy(arr1, 0, result, 0, arr1.length);
78             System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
79             return result;
80         }
81     }
82 }
83