1 /*
2  * Copyright (C) 2017 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 com.android.car.obd2.test;
18 
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 
22 public class Utils {
Utils()23     private Utils() {}
24 
stringsToIntArray(String... strings)25     static int[] stringsToIntArray(String... strings) {
26         ArrayList<Integer> arrayList = new ArrayList<>();
27         for (String string : strings) {
28             string.chars().forEach(arrayList::add);
29         }
30         return arrayList.stream().mapToInt(Integer::intValue).toArray();
31     }
32 
concatIntArrays(int[]... arrays)33     static int[] concatIntArrays(int[]... arrays) {
34         // corner and trivial cases
35         if (0 == arrays.length) return new int[] {};
36         if (1 == arrays.length) return arrays[0];
37 
38         // the general case in its full glory
39         int totalSize = Arrays.stream(arrays).mapToInt((int[] array) -> array.length).sum();
40         int[] newArray = Arrays.copyOf(arrays[0], totalSize);
41         int usedSize = arrays[0].length;
42         for (int i = 1; i < arrays.length; ++i) {
43             int[] array = arrays[i];
44             int length = array.length;
45             System.arraycopy(array, 0, newArray, usedSize, length);
46             usedSize += length;
47         }
48         return newArray;
49     }
50 }
51