]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/UCharacterNameReader.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / UCharacterNameReader.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 import java.util.Arrays;\r
13 \r
14 /**\r
15 * <p>Internal reader class for ICU data file uname.dat containing \r
16 * Unicode codepoint name data.</p> \r
17 * <p>This class simply reads unames.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=UCharacterName.html>com.ibm.icu.impl.UCharacterName</a>.\r
20 * </p> \r
21 * <p>unames.icu which is in big-endian format is jared together with this \r
22 * package.</p>\r
23 * @author Syn Wee Quek\r
24 * @since release 2.1, February 1st 2002\r
25 */\r
26 \r
27 final class UCharacterNameReader implements ICUBinary.Authenticate\r
28 {      \r
29     // public methods ----------------------------------------------------\r
30     \r
31     public boolean isDataVersionAcceptable(byte version[])\r
32     {\r
33         return version[0] == DATA_FORMAT_VERSION_[0];\r
34     }\r
35     \r
36     // protected constructor ---------------------------------------------\r
37     \r
38     /**\r
39     * <p>Protected constructor.</p>\r
40     * @param inputStream ICU uprop.dat file input stream\r
41     * @exception IOException throw if data file fails authentication \r
42     */\r
43     protected UCharacterNameReader(InputStream inputStream) \r
44                                                         throws IOException\r
45     {\r
46         ICUBinary.readHeader(inputStream, DATA_FORMAT_ID_, this);\r
47         m_dataInputStream_ = new DataInputStream(inputStream);\r
48     }\r
49   \r
50     // protected methods -------------------------------------------------\r
51       \r
52     /**\r
53     * Read and break up the stream of data passed in as arguments\r
54     * and fills up UCharacterName.\r
55     * If unsuccessful false will be returned.\r
56     * @param data instance of datablock\r
57     * @exception IOException thrown when there's a data error.\r
58     */\r
59     protected void read(UCharacterName data) throws IOException\r
60     {\r
61         // reading index\r
62         m_tokenstringindex_ = m_dataInputStream_.readInt();\r
63         m_groupindex_       = m_dataInputStream_.readInt();\r
64         m_groupstringindex_ = m_dataInputStream_.readInt();\r
65         m_algnamesindex_    = m_dataInputStream_.readInt();\r
66         \r
67         // reading tokens\r
68         int count = m_dataInputStream_.readChar();\r
69         char token[] = new char[count];\r
70         for (char i = 0; i < count; i ++) {\r
71             token[i] = m_dataInputStream_.readChar();\r
72         }\r
73         int size = m_groupindex_ - m_tokenstringindex_;\r
74         byte tokenstr[] = new byte[size];\r
75         m_dataInputStream_.readFully(tokenstr);\r
76         data.setToken(token, tokenstr);\r
77         \r
78         // reading the group information records\r
79         count = m_dataInputStream_.readChar();\r
80         data.setGroupCountSize(count, GROUP_INFO_SIZE_);\r
81         count *= GROUP_INFO_SIZE_;\r
82         char group[] = new char[count];\r
83         for (int i = 0; i < count; i ++) {\r
84             group[i] = m_dataInputStream_.readChar();\r
85         }\r
86         \r
87         size = m_algnamesindex_ - m_groupstringindex_;\r
88         byte groupstring[] = new byte[size];\r
89         m_dataInputStream_.readFully(groupstring);\r
90     \r
91         data.setGroup(group, groupstring);\r
92         \r
93         count = m_dataInputStream_.readInt();\r
94         UCharacterName.AlgorithmName alg[] = \r
95                                  new UCharacterName.AlgorithmName[count];\r
96      \r
97         for (int i = 0; i < count; i ++)\r
98         {\r
99             UCharacterName.AlgorithmName an = readAlg();\r
100             if (an == null) {\r
101                 throw new IOException("unames.icu read error: Algorithmic names creation error");\r
102             }\r
103             alg[i] = an;\r
104         }\r
105         data.setAlgorithm(alg);\r
106     }\r
107     \r
108     /**\r
109     * <p>Checking the file for the correct format.</p>\r
110     * @param dataformatid\r
111     * @param dataformatversion\r
112     * @return true if the file format version is correct\r
113     */\r
114     ///CLOVER:OFF\r
115     protected boolean authenticate(byte dataformatid[],\r
116                                    byte dataformatversion[])\r
117     {\r
118         return Arrays.equals(DATA_FORMAT_ID_, dataformatid) &&\r
119                Arrays.equals(DATA_FORMAT_VERSION_, dataformatversion);\r
120     }\r
121     ///CLOVER:ON\r
122     \r
123     // private variables -------------------------------------------------\r
124   \r
125     /**\r
126     * Data input stream for names \r
127     */\r
128     private DataInputStream m_dataInputStream_;\r
129     /**\r
130     * Size of the group information block in number of char\r
131     */\r
132     private static final int GROUP_INFO_SIZE_ = 3;\r
133 \r
134     /**\r
135     * Index of the offset information\r
136     */\r
137     private int m_tokenstringindex_;\r
138     private int m_groupindex_;\r
139     private int m_groupstringindex_;\r
140     private int m_algnamesindex_;\r
141       \r
142     /**\r
143     * Size of an algorithmic name information group\r
144     * start code point size + end code point size + type size + variant size + \r
145     * size of data size\r
146     */\r
147     private static final int ALG_INFO_SIZE_ = 12;\r
148       \r
149     /**\r
150     * File format version and id that this class understands.\r
151     * No guarantees are made if a older version is used\r
152     */\r
153     private static final byte DATA_FORMAT_VERSION_[] = \r
154                                     {(byte)0x1, (byte)0x0, (byte)0x0, (byte)0x0};\r
155     private static final byte DATA_FORMAT_ID_[] = {(byte)0x75, (byte)0x6E, \r
156                                                     (byte)0x61, (byte)0x6D};                                                 \r
157       \r
158     // private methods ---------------------------------------------------\r
159       \r
160     /**\r
161     * Reads an individual record of AlgorithmNames\r
162     * @return an instance of AlgorithNames if read is successful otherwise null\r
163     * @exception IOException thrown when file read error occurs or data is corrupted\r
164     */\r
165     private UCharacterName.AlgorithmName readAlg() throws IOException\r
166     {\r
167         UCharacterName.AlgorithmName result = \r
168                                        new UCharacterName.AlgorithmName();\r
169         int rangestart = m_dataInputStream_.readInt();\r
170         int rangeend   = m_dataInputStream_.readInt();\r
171         byte type      = m_dataInputStream_.readByte();\r
172         byte variant   = m_dataInputStream_.readByte();\r
173         if (!result.setInfo(rangestart, rangeend, type, variant)) {\r
174             return null;\r
175         }\r
176                          \r
177         int size = m_dataInputStream_.readChar();\r
178         if (type == UCharacterName.AlgorithmName.TYPE_1_)\r
179         {\r
180             char factor[] = new char[variant];\r
181             for (int j = 0; j < variant; j ++) {\r
182                 factor[j] = m_dataInputStream_.readChar();\r
183             }\r
184                   \r
185             result.setFactor(factor);\r
186             size -= (variant << 1);\r
187         }\r
188           \r
189         StringBuilder prefix = new StringBuilder();\r
190         char c = (char)(m_dataInputStream_.readByte() & 0x00FF);\r
191         while (c != 0)\r
192         {\r
193             prefix.append(c);\r
194             c = (char)(m_dataInputStream_.readByte() & 0x00FF);\r
195         }\r
196         \r
197         result.setPrefix(prefix.toString());\r
198         \r
199         size -= (ALG_INFO_SIZE_ + prefix.length() + 1);\r
200         \r
201         if (size > 0)\r
202         {\r
203             byte string[] = new byte[size];\r
204             m_dataInputStream_.readFully(string);\r
205             result.setFactorString(string);\r
206         }\r
207         return result;\r
208     }\r
209 }\r
210 \r