1/* GENERATED SOURCE. DO NOT MODIFY. */
2// © 2016 and later: Unicode, Inc. and others.
3// License & terms of use: http://www.unicode.org/copyright.html#License
4/*
5 *******************************************************************************
6 * Copyright (C) 2012-2016, International Business Machines Corporation and
7 * others. All Rights Reserved.
8 *******************************************************************************
9 */
10
11package android.icu.text;
12
13import java.io.IOException;
14import java.nio.ByteBuffer;
15
16import android.icu.impl.Assert;
17import android.icu.impl.ICUBinary;
18import android.icu.impl.ICUData;
19import android.icu.impl.ICUResourceBundle;
20import android.icu.util.UResourceBundle;
21
22final class DictionaryData {
23    // disallow instantiation
24    private DictionaryData() { }
25
26    public static final int TRIE_TYPE_BYTES = 0;
27    public static final int TRIE_TYPE_UCHARS = 1;
28    public static final int TRIE_TYPE_MASK = 7;
29    public static final int TRIE_HAS_VALUES = 8;
30    public static final int TRANSFORM_NONE = 0;
31    public static final int TRANSFORM_TYPE_OFFSET = 0x1000000;
32    public static final int TRANSFORM_TYPE_MASK = 0x7f000000;
33    public static final int TRANSFORM_OFFSET_MASK = 0x1fffff;
34
35    public static final int IX_STRING_TRIE_OFFSET = 0;
36    public static final int IX_RESERVED1_OFFSET = 1;
37    public static final int IX_RESERVED2_OFFSET = 2;
38    public static final int IX_TOTAL_SIZE = 3;
39    public static final int IX_TRIE_TYPE = 4;
40    public static final int IX_TRANSFORM = 5;
41    public static final int IX_RESERVED6 = 6;
42    public static final int IX_RESERVED7 = 7;
43    public static final int IX_COUNT = 8;
44
45    private static final int DATA_FORMAT_ID = 0x44696374;
46
47    public static DictionaryMatcher loadDictionaryFor(String dictType) throws IOException {
48        ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUData.ICU_BRKITR_BASE_NAME);
49        String dictFileName = rb.getStringWithFallback("dictionaries/" + dictType);
50        dictFileName = ICUData.ICU_BRKITR_NAME + '/' + dictFileName;
51        ByteBuffer bytes = ICUBinary.getRequiredData(dictFileName);
52        ICUBinary.readHeader(bytes, DATA_FORMAT_ID, null);
53        int[] indexes = new int[IX_COUNT];
54        // TODO: read indexes[IX_STRING_TRIE_OFFSET] first, then read a variable-length indexes[]
55        for (int i = 0; i < IX_COUNT; i++) {
56            indexes[i] = bytes.getInt();
57        }
58        int offset = indexes[IX_STRING_TRIE_OFFSET];
59        Assert.assrt(offset >= (4 * IX_COUNT));
60        if (offset > (4 * IX_COUNT)) {
61            int diff = offset - (4 * IX_COUNT);
62            ICUBinary.skipBytes(bytes, diff);
63        }
64        int trieType = indexes[IX_TRIE_TYPE] & TRIE_TYPE_MASK;
65        int totalSize = indexes[IX_TOTAL_SIZE] - offset;
66        DictionaryMatcher m = null;
67        if (trieType == TRIE_TYPE_BYTES) {
68            int transform = indexes[IX_TRANSFORM];
69            byte[] data = new byte[totalSize];
70            bytes.get(data);
71            m = new BytesDictionaryMatcher(data, transform);
72        } else if (trieType == TRIE_TYPE_UCHARS) {
73            Assert.assrt(totalSize % 2 == 0);
74            String data = ICUBinary.getString(bytes, totalSize / 2, totalSize & 1);
75            m = new CharsDictionaryMatcher(data);
76        } else {
77            m = null;
78        }
79        return m;
80    }
81}
82