blob: 81be384857226f594c57ddb8b9db97461fda4b1e [file] [log] [blame]
Alex Klyubindcdaf872015-05-13 15:57:09 -07001/*
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
17package android.security.keystore;
Alex Klyubin5927c9f2015-04-10 13:28:03 -070018
19import libcore.util.EmptyArray;
20
21/**
22 * @hide
23 */
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070024public abstract class ArrayUtils {
Alex Klyubin5927c9f2015-04-10 13:28:03 -070025 private ArrayUtils() {}
26
27 public static String[] nullToEmpty(String[] array) {
28 return (array != null) ? array : EmptyArray.STRING;
29 }
30
31 public static String[] cloneIfNotEmpty(String[] array) {
32 return ((array != null) && (array.length > 0)) ? array.clone() : array;
33 }
34
35 public static byte[] concat(byte[] arr1, byte[] arr2) {
36 return concat(arr1, 0, (arr1 != null) ? arr1.length : 0,
37 arr2, 0, (arr2 != null) ? arr2.length : 0);
38 }
39
40 public static byte[] concat(byte[] arr1, int offset1, int len1, byte[] arr2, int offset2,
41 int len2) {
42 if (len1 == 0) {
43 return subarray(arr2, offset2, len2);
44 } else if (len2 == 0) {
45 return subarray(arr1, offset1, len1);
46 } else {
47 byte[] result = new byte[len1 + len2];
48 System.arraycopy(arr1, offset1, result, 0, len1);
49 System.arraycopy(arr2, offset2, result, len1, len2);
50 return result;
51 }
52 }
53
54 public static byte[] subarray(byte[] arr, int offset, int len) {
55 if (len == 0) {
56 return EmptyArray.BYTE;
57 }
58 if ((offset == 0) && (len == arr.length)) {
59 return arr;
60 }
61 byte[] result = new byte[len];
62 System.arraycopy(arr, offset, result, 0, len);
63 return result;
64 }
65
66 public static int[] concat(int[] arr1, int[] arr2) {
67 if ((arr1 == null) || (arr1.length == 0)) {
68 return arr2;
69 } else if ((arr2 == null) || (arr2.length == 0)) {
70 return arr1;
71 } else {
72 int[] result = new int[arr1.length + arr2.length];
73 System.arraycopy(arr1, 0, result, 0, arr1.length);
74 System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
75 return result;
76 }
77 }
78}