1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.ohos.hapsigntool.utils; 17 18 /** 19 * utils functions about byte arrays 20 * 21 * @since 2021-12-13 22 */ 23 public class ByteArrayUtils { 24 private static final int BIT_SIZE = 8; 25 26 private static final int DOUBLE_BIT_SIZE = 16; 27 28 private static final int TRIPLE_BIT_SIZE = 24; 29 30 private static final int HALF_INTEGER_BYTES = 2; 31 ByteArrayUtils()32 private ByteArrayUtils() { 33 } 34 35 /** 36 * Insert int value to byte array 37 * 38 * @param desByte destination byte array 39 * @param index position of inserting 40 * @param num value is inserted 41 * @return end of position of inserting, if successfully 42 */ insertIntToByteArray(byte[] desByte, int index, int num)43 public static int insertIntToByteArray(byte[] desByte, int index, int num) { 44 if (desByte == null) { 45 return -1; 46 } 47 if (index + Integer.BYTES > desByte.length) { 48 return -1; 49 } 50 int pos = index; 51 desByte[pos] = (byte) ((num >> TRIPLE_BIT_SIZE) & 0xff); 52 pos++; 53 desByte[pos] = (byte) ((num >> DOUBLE_BIT_SIZE) & 0xff); 54 pos++; 55 desByte[pos] = (byte) ((num >> BIT_SIZE) & 0xff); 56 pos++; 57 desByte[pos] = (byte) (num & 0xff); 58 pos++; 59 return pos; 60 } 61 62 /** 63 * Insert short value to byte array 64 * 65 * @param desByte destination byte array 66 * @param desByteLen length of destination byte array 67 * @param index position of inserting 68 * @param num value is inserted 69 * @return end of position of inserting, if successfully 70 */ insertShortToByteArray(byte[] desByte, int desByteLen, int index, short num)71 public static int insertShortToByteArray(byte[] desByte, int desByteLen, int index, short num) { 72 if (desByte == null) { 73 return -1; 74 } 75 if (index + HALF_INTEGER_BYTES > desByteLen) { 76 return -1; 77 } 78 int pos = index; 79 desByte[pos] = (byte) ((num >> BIT_SIZE) & 0xff); 80 pos++; 81 desByte[pos] = (byte) (num & 0xff); 82 pos++; 83 return pos; 84 } 85 86 /** 87 * Insert byte array to byte array 88 * 89 * @param des destination byte array 90 * @param start position of inserting 91 * @param src byte array is inserted 92 * @param srcLen length of byte array is inserted 93 * @return end of position of inserting, if successfully 94 */ insertByteToByteArray(byte[] des, int start, byte[] src, int srcLen)95 public static int insertByteToByteArray(byte[] des, int start, byte[] src, int srcLen) { 96 if (src.length < srcLen) { 97 return -1; 98 } 99 System.arraycopy(src, 0, des, start, srcLen); 100 return start + srcLen; 101 } 102 103 /** 104 * Insert char array to byte array 105 * 106 * @param des destination byte array 107 * @param start position of inserting 108 * @param src char array is inserted 109 * @return end of position of inserting, if successfully 110 */ insertCharToByteArray(byte[] des, int start, char[] src)111 public static int insertCharToByteArray(byte[] des, int start, char[] src) { 112 if (des == null) { 113 return -1; 114 } 115 if (start > des.length - src.length) { 116 return -1; 117 } 118 for (int i = 0; i < src.length; i++) { 119 des[i + start] = (byte) src[i]; 120 } 121 return start + src.length; 122 } 123 }