1 /* 2 * Copyright (c) 2025 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 ohos.restool; 17 18 import java.nio.ByteBuffer; 19 import java.nio.ByteOrder; 20 import java.nio.charset.StandardCharsets; 21 22 /** 23 * ResourcesParserFactory 24 * 25 * @since 2025-06-06 26 */ 27 public class ResourcesParserFactory { 28 private static final int VERSION_BYTE_LENGTH = 128; 29 private static final String RESOURCE_PROTOCOL_VERSION_TAG = "RestoolV2"; 30 31 /** 32 * createParser. 33 * 34 * @param data resource index data 35 * @return ResourcesParser 36 */ createParser(byte[] data)37 public static ResourcesParser createParser(byte[] data) { 38 if (isV2Protocol(data)) { 39 return new ResourcesParserV2(data); 40 } 41 return new ResourcesParserV1(); 42 } 43 44 /** 45 * Is V2 protocol. 46 * 47 * @param data resource index data 48 * @return is V2 49 */ isV2Protocol(byte[] data)50 private static boolean isV2Protocol(byte[] data) { 51 ByteBuffer byteBuf = ByteBuffer.wrap(data); 52 byteBuf.order(ByteOrder.LITTLE_ENDIAN); 53 byte[] version = new byte[VERSION_BYTE_LENGTH]; 54 byteBuf.get(version); 55 String versionStr = new String(version, StandardCharsets.UTF_8); 56 return versionStr.contains(RESOURCE_PROTOCOL_VERSION_TAG); 57 } 58 } 59