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 (index + Integer.BYTES > desByte.length) { 45 return -1; 46 } 47 int pos = index; 48 desByte[pos] = (byte) ((num >> TRIPLE_BIT_SIZE) & 0xff); 49 pos++; 50 desByte[pos] = (byte) ((num >> DOUBLE_BIT_SIZE) & 0xff); 51 pos++; 52 desByte[pos] = (byte) ((num >> BIT_SIZE) & 0xff); 53 pos++; 54 desByte[pos] = (byte) (num & 0xff); 55 pos++; 56 return pos; 57 } 58 59 /** 60 * Insert short value to byte array 61 * 62 * @param desByte destination byte array 63 * @param desByteLen length of destination byte array 64 * @param index position of inserting 65 * @param num value is inserted 66 * @return end of position of inserting, if successfully 67 */ insertShortToByteArray(byte[] desByte, int desByteLen, int index, short num)68 public static int insertShortToByteArray(byte[] desByte, int desByteLen, int index, short num) { 69 if (index + HALF_INTEGER_BYTES > desByteLen) { 70 return -1; 71 } 72 int pos = index; 73 desByte[pos] = (byte) ((num >> BIT_SIZE) & 0xff); 74 pos++; 75 desByte[pos] = (byte) (num & 0xff); 76 pos++; 77 return pos; 78 } 79 80 /** 81 * Insert byte array to byte array 82 * 83 * @param des destination byte array 84 * @param start position of inserting 85 * @param src byte array is inserted 86 * @param srcLen length of byte array is inserted 87 * @return end of position of inserting, if successfully 88 */ insertByteToByteArray(byte[] des, int start, byte[] src, int srcLen)89 public static int insertByteToByteArray(byte[] des, int start, byte[] src, int srcLen) { 90 if (src.length < srcLen) { 91 return -1; 92 } 93 System.arraycopy(src, 0, des, start, srcLen); 94 return start + srcLen; 95 } 96 97 /** 98 * Insert char array to byte array 99 * 100 * @param des destination byte array 101 * @param start position of inserting 102 * @param src char array is inserted 103 * @return end of position of inserting, if successfully 104 */ insertCharToByteArray(byte[] des, int start, char[] src)105 public static int insertCharToByteArray(byte[] des, int start, char[] src) { 106 if (start > des.length - src.length) { 107 return -1; 108 } 109 for (int i = 0; i < src.length; i++) { 110 des[i + start] = (byte) src[i]; 111 } 112 return start + src.length; 113 } 114 }