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 import java.util.function.Consumer; 22 23 /** 24 * @hide 25 */ 26 public abstract class ArrayUtils { ArrayUtils()27 private ArrayUtils() {} 28 nullToEmpty(String[] array)29 public static String[] nullToEmpty(String[] array) { 30 return (array != null) ? array : EmptyArray.STRING; 31 } 32 cloneIfNotEmpty(String[] array)33 public static String[] cloneIfNotEmpty(String[] array) { 34 return ((array != null) && (array.length > 0)) ? array.clone() : array; 35 } 36 37 /** 38 * Clones an array if it is not null and has a length greater than 0. Otherwise, returns the 39 * array. 40 */ cloneIfNotEmpty(int[] array)41 public static int[] cloneIfNotEmpty(int[] array) { 42 return ((array != null) && (array.length > 0)) ? array.clone() : array; 43 } 44 cloneIfNotEmpty(byte[] array)45 public static byte[] cloneIfNotEmpty(byte[] array) { 46 return ((array != null) && (array.length > 0)) ? array.clone() : array; 47 } 48 concat(byte[] arr1, byte[] arr2)49 public static byte[] concat(byte[] arr1, byte[] arr2) { 50 return concat(arr1, 0, (arr1 != null) ? arr1.length : 0, 51 arr2, 0, (arr2 != null) ? arr2.length : 0); 52 } 53 concat(byte[] arr1, int offset1, int len1, byte[] arr2, int offset2, int len2)54 public static byte[] concat(byte[] arr1, int offset1, int len1, byte[] arr2, int offset2, 55 int len2) { 56 if (len1 == 0) { 57 return subarray(arr2, offset2, len2); 58 } else if (len2 == 0) { 59 return subarray(arr1, offset1, len1); 60 } else { 61 byte[] result = new byte[len1 + len2]; 62 System.arraycopy(arr1, offset1, result, 0, len1); 63 System.arraycopy(arr2, offset2, result, len1, len2); 64 return result; 65 } 66 } 67 68 /** 69 * Copies a subset of the source array to the destination array. 70 * Length will be limited to the bounds of source and destination arrays. 71 * The length actually copied is returned, which will be <= length argument. 72 * @param src is the source array 73 * @param srcOffset is the offset in the source array. 74 * @param dst is the destination array. 75 * @param dstOffset is the offset in the destination array. 76 * @param length is the length to be copied from source to destination array. 77 * @return The length actually copied from source array. 78 */ copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length)79 public static int copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length) { 80 if (dst == null || src == null) { 81 return 0; 82 } 83 if (length > dst.length - dstOffset) { 84 length = dst.length - dstOffset; 85 } 86 if (length > src.length - srcOffset) { 87 length = src.length - srcOffset; 88 } 89 if (length <= 0) { 90 return 0; 91 } 92 System.arraycopy(src, srcOffset, dst, dstOffset, length); 93 return length; 94 } 95 subarray(byte[] arr, int offset, int len)96 public static byte[] subarray(byte[] arr, int offset, int len) { 97 if (len == 0) { 98 return EmptyArray.BYTE; 99 } 100 if ((offset == 0) && (len == arr.length)) { 101 return arr; 102 } 103 byte[] result = new byte[len]; 104 System.arraycopy(arr, offset, result, 0, len); 105 return result; 106 } 107 concat(int[] arr1, int[] arr2)108 public static int[] concat(int[] arr1, int[] arr2) { 109 if ((arr1 == null) || (arr1.length == 0)) { 110 return arr2; 111 } else if ((arr2 == null) || (arr2.length == 0)) { 112 return arr1; 113 } else { 114 int[] result = new int[arr1.length + arr2.length]; 115 System.arraycopy(arr1, 0, result, 0, arr1.length); 116 System.arraycopy(arr2, 0, result, arr1.length, arr2.length); 117 return result; 118 } 119 } 120 121 /** 122 * Runs {@code consumer.accept()} for each element of {@code array}. 123 * @param array 124 * @param consumer 125 * @hide 126 */ forEach(int[] array, Consumer<Integer> consumer)127 public static void forEach(int[] array, Consumer<Integer> consumer) { 128 for (int i : array) { 129 consumer.accept(i); 130 } 131 } 132 } 133