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