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