]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/UCharacterPropertyReader.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / UCharacterPropertyReader.java
1 /**\r
2 *******************************************************************************\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and    *\r
4 * others. All Rights Reserved.                                                *\r
5 *******************************************************************************\r
6 */\r
7 package com.ibm.icu.impl;\r
8 \r
9 import java.io.DataInputStream;\r
10 import java.io.IOException;\r
11 import java.io.InputStream;\r
12 \r
13 import com.ibm.icu.util.VersionInfo;\r
14 \r
15 /**\r
16 * <p>Internal reader class for ICU data file uprops.icu containing \r
17 * Unicode codepoint data.</p> \r
18 * <p>This class simply reads uprops.icu, authenticates that it is a valid\r
19 * ICU data file and split its contents up into blocks of data for use in\r
20 * <a href=UCharacterProperty.html>com.ibm.icu.impl.UCharacterProperty</a>.\r
21 * </p> \r
22 * <p>uprops.icu which is in big-endian format is jared together with this \r
23 * package.</p>\r
24 *\r
25 * Unicode character properties file format see\r
26 * (ICU4C)/source/tools/genprops/store.c\r
27 *\r
28 * @author Syn Wee Quek\r
29 * @since release 2.1, February 1st 2002\r
30 */\r
31 final class UCharacterPropertyReader implements ICUBinary.Authenticate\r
32 {\r
33     // public methods ----------------------------------------------------\r
34     \r
35     public boolean isDataVersionAcceptable(byte version[])\r
36     {\r
37         return version[0] == DATA_FORMAT_VERSION_[0] \r
38                && version[2] == DATA_FORMAT_VERSION_[2] \r
39                && version[3] == DATA_FORMAT_VERSION_[3];\r
40     }\r
41     \r
42     // protected constructor ---------------------------------------------\r
43     \r
44     /**\r
45     * <p>Protected constructor.</p>\r
46     * @param inputStream ICU uprop.dat file input stream\r
47     * @exception IOException throw if data file fails authentication \r
48     */\r
49     protected UCharacterPropertyReader(InputStream inputStream) \r
50                                                         throws IOException\r
51     {\r
52         m_unicodeVersion_ = ICUBinary.readHeader(inputStream, DATA_FORMAT_ID_, \r
53                                                  this);\r
54         m_dataInputStream_ = new DataInputStream(inputStream);\r
55     }\r
56     \r
57     // protected methods -------------------------------------------------\r
58       \r
59     /**\r
60     * <p>Reads uprops.icu, parse it into blocks of data to be stored in\r
61     * UCharacterProperty.</P\r
62     * @param ucharppty UCharacterProperty instance\r
63     * @exception IOException thrown when data reading fails\r
64     */\r
65     protected void read(UCharacterProperty ucharppty) throws IOException\r
66     {\r
67         // read the indexes\r
68         int count = INDEX_SIZE_;\r
69         m_propertyOffset_          = m_dataInputStream_.readInt();\r
70         count --;\r
71         m_exceptionOffset_         = m_dataInputStream_.readInt();\r
72         count --;\r
73         m_caseOffset_              = m_dataInputStream_.readInt();\r
74         count --;\r
75         m_additionalOffset_        = m_dataInputStream_.readInt();\r
76         count --;\r
77         m_additionalVectorsOffset_ = m_dataInputStream_.readInt();\r
78         count --;\r
79         m_additionalColumnsCount_  = m_dataInputStream_.readInt();\r
80         count --;\r
81         m_reservedOffset_          = m_dataInputStream_.readInt();\r
82         count --;\r
83         m_dataInputStream_.skipBytes(3 << 2);\r
84         count -= 3;\r
85         ucharppty.m_maxBlockScriptValue_ = m_dataInputStream_.readInt();\r
86         count --; // 10\r
87         ucharppty.m_maxJTGValue_ = m_dataInputStream_.readInt();\r
88         count --; // 11\r
89         m_dataInputStream_.skipBytes(count << 2);\r
90 \r
91         // read the trie index block\r
92         // m_props_index_ in terms of ints\r
93         ucharppty.m_trie_ = new CharTrie(m_dataInputStream_, null);\r
94 \r
95         // skip the 32 bit properties block\r
96         int size = m_exceptionOffset_ - m_propertyOffset_;\r
97         m_dataInputStream_.skipBytes(size * 4);\r
98 \r
99         // reads the 32 bit exceptions block\r
100         size = m_caseOffset_ - m_exceptionOffset_;\r
101         m_dataInputStream_.skipBytes(size * 4);\r
102 \r
103         // reads the 32 bit case block\r
104         size = (m_additionalOffset_ - m_caseOffset_) << 1;\r
105         m_dataInputStream_.skipBytes(size * 2);\r
106 \r
107         if(m_additionalColumnsCount_ > 0) {\r
108             // reads the additional property block\r
109             ucharppty.m_additionalTrie_ = new CharTrie(m_dataInputStream_, null);\r
110                                                                \r
111             // additional properties\r
112             size = m_reservedOffset_ - m_additionalVectorsOffset_;\r
113             ucharppty.m_additionalVectors_ = new int[size];\r
114             for (int i = 0; i < size; i ++) {\r
115                 ucharppty.m_additionalVectors_[i] = m_dataInputStream_.readInt();\r
116             }\r
117         }\r
118 \r
119         m_dataInputStream_.close();\r
120         ucharppty.m_additionalColumnsCount_ = m_additionalColumnsCount_;\r
121         ucharppty.m_unicodeVersion_ = VersionInfo.getInstance(\r
122                          (int)m_unicodeVersion_[0], (int)m_unicodeVersion_[1],\r
123                          (int)m_unicodeVersion_[2], (int)m_unicodeVersion_[3]);\r
124     }\r
125     \r
126     // private variables -------------------------------------------------\r
127       \r
128     /**\r
129     * Index size\r
130     */\r
131     private static final int INDEX_SIZE_ = 16;\r
132     \r
133     /**\r
134     * ICU data file input stream\r
135     */\r
136     private DataInputStream m_dataInputStream_;\r
137       \r
138     /**\r
139     * Offset information in the indexes.\r
140     */\r
141     private int m_propertyOffset_;\r
142     private int m_exceptionOffset_;\r
143     private int m_caseOffset_;\r
144     private int m_additionalOffset_;\r
145     private int m_additionalVectorsOffset_;\r
146     private int m_additionalColumnsCount_;\r
147     private int m_reservedOffset_;\r
148     private byte m_unicodeVersion_[];  \r
149                                       \r
150     /**\r
151     * Data format "UPro".\r
152     */\r
153     private static final byte DATA_FORMAT_ID_[] = {(byte)0x55, (byte)0x50, \r
154                                                     (byte)0x72, (byte)0x6F};\r
155     /**\r
156      * Format version; this code works with all versions with the same major\r
157      * version number and the same Trie bit distribution.\r
158      */\r
159     private static final byte DATA_FORMAT_VERSION_[] = {(byte)6, (byte)0, \r
160                                              (byte)Trie.INDEX_STAGE_1_SHIFT_, \r
161                                              (byte)Trie.INDEX_STAGE_2_SHIFT_};\r
162 }\r