1 /* 2 * Copyright (c) 2023-2023 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.zip; 17 18 import java.nio.ByteBuffer; 19 20 /** 21 * Unsigned Decimal Util 22 * 23 * @since 2023/12/09 24 */ 25 public class UnsignedDecimalUtil { 26 /** 27 * max unsigned int value 28 */ 29 public static final long MAX_UNSIGNED_INT_VALUE = 0xFFFFFFFFL; 30 31 /** 32 * max unsigned int value 33 */ 34 public static final int MAX_UNSIGNED_SHORT_VALUE = 0xFFFF; 35 36 private static final int BIT_SIZE = 8; 37 38 private static final int DOUBLE_BIT_SIZE = 16; 39 40 private static final int TRIPLE_BIT_SIZE = 24; 41 42 /** 43 * get unsigned int to long 44 * 45 * @param bf byteBuffer 46 * @return long 47 */ getUnsignedInt(ByteBuffer bf)48 public static long getUnsignedInt(ByteBuffer bf) { 49 return bf.getInt() & MAX_UNSIGNED_INT_VALUE; 50 } 51 52 /** 53 * get unsigned short to int 54 * 55 * @param bf byteBuffer 56 * @return int 57 */ getUnsignedShort(ByteBuffer bf)58 public static int getUnsignedShort(ByteBuffer bf) { 59 return bf.getShort() & MAX_UNSIGNED_SHORT_VALUE; 60 } 61 62 /** 63 * set long to unsigned int 64 * 65 * @param bf byteBuffer 66 * @param value long 67 */ setUnsignedInt(ByteBuffer bf, long value)68 public static void setUnsignedInt(ByteBuffer bf, long value) { 69 byte[] bytes = new byte[] { 70 (byte) (value & 0xFF), 71 (byte) ((value >> BIT_SIZE) & 0xFF), 72 (byte) ((value >> DOUBLE_BIT_SIZE) & 0xFF), 73 (byte) ((value >> TRIPLE_BIT_SIZE) & 0xFF) 74 }; 75 bf.put(bytes); 76 } 77 78 /** 79 * set int to unsigned short 80 * 81 * @param bf byteBuffer 82 * @param value int 83 */ setUnsignedShort(ByteBuffer bf, int value)84 public static void setUnsignedShort(ByteBuffer bf, int value) { 85 byte[] bytes = new byte[] { 86 (byte) (value & 0xFF), 87 (byte) ((value >> BIT_SIZE) & 0xFF) 88 }; 89 bf.put(bytes); 90 } 91 } 92