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 
58     /**
59      * Copies a subset of the source array to the destination array.
60      * Length will be limited to the bounds of source and destination arrays.
61      * The length actually copied is returned, which will be <= length argument.
62      * @param src is the source array
63      * @param srcOffset is the offset in the source array.
64      * @param dst is the destination array.
65      * @param dstOffset is the offset in the destination array.
66      * @param length is the length to be copied from source to destination array.
67      * @return The length actually copied from source array.
68      */
copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length)69     public static int copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length) {
70         if (dst == null || src == null) {
71             return 0;
72         }
73         if (length > dst.length - dstOffset) {
74             length = dst.length - dstOffset;
75         }
76         if (length > src.length - srcOffset) {
77             length = src.length - srcOffset;
78         }
79         if (length <= 0) {
80             return 0;
81         }
82         System.arraycopy(src, srcOffset, dst, dstOffset, length);
83         return length;
84     }
85 
subarray(byte[] arr, int offset, int len)86     public static byte[] subarray(byte[] arr, int offset, int len) {
87         if (len == 0) {
88             return EmptyArray.BYTE;
89         }
90         if ((offset == 0) && (len == arr.length)) {
91             return arr;
92         }
93         byte[] result = new byte[len];
94         System.arraycopy(arr, offset, result, 0, len);
95         return result;
96     }
97 
concat(int[] arr1, int[] arr2)98     public static int[] concat(int[] arr1, int[] arr2) {
99         if ((arr1 == null) || (arr1.length == 0)) {
100             return arr2;
101         } else if ((arr2 == null) || (arr2.length == 0)) {
102             return arr1;
103         } else {
104             int[] result = new int[arr1.length + arr2.length];
105             System.arraycopy(arr1, 0, result, 0, arr1.length);
106             System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
107             return result;
108         }
109     }
110 }
111