1 /* 2 * Copyright (c) 2021 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.global.i18n; 17 18 import java.io.DataOutputStream; 19 import java.io.IOException; 20 import java.util.ArrayList; 21 import java.util.HashMap; 22 import java.util.Map; 23 24 /** 25 * Represents a locale in dat file 26 */ 27 public class LocaleList { 28 private ArrayList<LocaleMaskItem> list = new ArrayList<>(70); 29 private StringPool pool; 30 private int size; 31 private int startOffset; 32 LocaleList(int startOffset, HashMap<String, Integer> locales, Map<Integer, ArrayList<LocaleConfig>> localeConfigs, StringPool stringPool)33 public LocaleList(int startOffset, HashMap<String, Integer> locales, Map<Integer, 34 ArrayList<LocaleConfig>> localeConfigs, StringPool stringPool) { 35 this.startOffset = startOffset; 36 this.size = locales.size(); 37 this.pool = stringPool; 38 for (Map.Entry<String, Integer> next : locales.entrySet()) { 39 this.list.add(new LocaleMaskItem(next.getKey(), localeConfigs.get(next.getValue()))); 40 } 41 list.sort(null); 42 for (int i = 0; i < list.size(); ++i) { 43 LocaleMaskItem item = list.get(i); 44 item.setRank(i); 45 item.setOffset(startOffset + i * FileConfig.LOCALE_MASK_ITEM_SIZE); 46 } 47 } 48 49 /** 50 * Methods to write a localeList object to dat file 51 * 52 * @param dataOutputStream The destination of the output 53 * @throws IOException throws IOException if an error occurs 54 */ write(DataOutputStream dataOutputStream)55 public void write(DataOutputStream dataOutputStream) throws IOException { 56 int acc = 0; 57 for (int i = 0; i < list.size(); i++) { 58 LocaleMaskItem localeMaskItem = list.get(i); 59 // write Locale part 60 dataOutputStream.writeInt((int) localeMaskItem.getMask()); 61 dataOutputStream.writeChar(startOffset + (this.size * FileConfig.LOCALE_MASK_ITEM_SIZE) + 62 (acc * FileConfig.CONFIG_SIZE)); 63 dataOutputStream.writeChar(localeMaskItem.getConfigs().size()); 64 acc += localeMaskItem.getConfigs().size(); 65 dataOutputStream.flush(); 66 } 67 // Write configs part 68 for (int i = 0; i < this.list.size(); i++) { 69 LocaleMaskItem localeMaskItem2 = this.list.get(i); 70 ArrayList<LocaleConfig> configs = localeMaskItem2.getConfigs(); 71 // All configs are sorted already 72 for (int j = 0; j < configs.size(); ++j) { 73 LocaleConfig localeConfig = configs.get(j); 74 int nameId = localeConfig.nameId; 75 int stringId = localeConfig.stringId; 76 dataOutputStream.writeChar(nameId); 77 StringPool.StringItem stringItem = pool.getStringItem(stringId); 78 dataOutputStream.writeChar(stringItem.offset); 79 dataOutputStream.writeChar(stringItem.length); 80 } 81 dataOutputStream.flush(); 82 } 83 pool.write(dataOutputStream); 84 dataOutputStream.flush(); 85 } 86 } 87