KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > UCharacterNameReader


1 /**
2 *******************************************************************************
3 * Copyright (C) 1996-2006, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 */

7 package com.ibm.icu.impl;
8
9 import java.io.InputStream JavaDoc;
10 import java.io.DataInputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.Arrays JavaDoc;
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 * @draft 2.1
26 */

27
28 final class UCharacterNameReader implements ICUBinary.Authenticate
29 {
30     // public methods ----------------------------------------------------
31

32     public boolean isDataVersionAcceptable(byte version[])
33     {
34         return version[0] == DATA_FORMAT_VERSION_[0];
35     }
36     
37     // protected constructor ---------------------------------------------
38

39     /**
40     * <p>Protected constructor.</p>
41     * @param inputStream ICU uprop.dat file input stream
42     * @exception IOException throw if data file fails authentication
43     * @draft 2.1
44     */

45     protected UCharacterNameReader(InputStream JavaDoc inputStream)
46                                                         throws IOException JavaDoc
47     {
48         ICUBinary.readHeader(inputStream, DATA_FORMAT_ID_, this);
49         m_dataInputStream_ = new DataInputStream JavaDoc(inputStream);
50     }
51   
52     // protected methods -------------------------------------------------
53

54     /**
55     * Read and break up the stream of data passed in as arguments
56     * and fills up UCharacterName.
57     * If unsuccessful false will be returned.
58     * @param data instance of datablock
59     * @exception IOException thrown when there's a data error.
60     */

61     protected void read(UCharacterName data) throws IOException JavaDoc
62     {
63         // reading index
64
m_tokenstringindex_ = m_dataInputStream_.readInt();
65         m_groupindex_ = m_dataInputStream_.readInt();
66         m_groupstringindex_ = m_dataInputStream_.readInt();
67         m_algnamesindex_ = m_dataInputStream_.readInt();
68         
69         // reading tokens
70
int count = m_dataInputStream_.readChar();
71         char token[] = new char[count];
72         for (char i = 0; i < count; i ++) {
73             token[i] = m_dataInputStream_.readChar();
74         }
75         int size = m_groupindex_ - m_tokenstringindex_;
76         byte tokenstr[] = new byte[size];
77         m_dataInputStream_.readFully(tokenstr);
78         data.setToken(token, tokenstr);
79         
80         // reading the group information records
81
count = m_dataInputStream_.readChar();
82         data.setGroupCountSize(count, GROUP_INFO_SIZE_);
83         count *= GROUP_INFO_SIZE_;
84         char group[] = new char[count];
85         for (int i = 0; i < count; i ++) {
86             group[i] = m_dataInputStream_.readChar();
87         }
88         
89         size = m_algnamesindex_ - m_groupstringindex_;
90         byte groupstring[] = new byte[size];
91         m_dataInputStream_.readFully(groupstring);
92     
93         data.setGroup(group, groupstring);
94         
95         count = m_dataInputStream_.readInt();
96         UCharacterName.AlgorithmName alg[] =
97                                  new UCharacterName.AlgorithmName[count];
98      
99         for (int i = 0; i < count; i ++)
100         {
101             UCharacterName.AlgorithmName an = readAlg();
102             if (an == null) {
103                 throw new IOException JavaDoc("unames.icu read error: Algorithmic names creation error");
104             }
105             alg[i] = an;
106         }
107         data.setAlgorithm(alg);
108     }
109     
110     /**
111     * <p>Checking the file for the correct format.</p>
112     * @param dataformatid
113     * @param dataformatversion
114     * @return true if the file format version is correct
115     * @draft 2.1
116     */

117     ///CLOVER:OFF
118
protected boolean authenticate(byte dataformatid[],
119                                    byte dataformatversion[])
120     {
121         return Arrays.equals(DATA_FORMAT_ID_, dataformatid) &&
122                Arrays.equals(DATA_FORMAT_VERSION_, dataformatversion);
123     }
124     ///CLOVER:ON
125

126     // private variables -------------------------------------------------
127

128     /**
129     * Data input stream for names
130     */

131     private DataInputStream JavaDoc m_dataInputStream_;
132     /**
133     * Size of the group information block in number of char
134     */

135     private static final int GROUP_INFO_SIZE_ = 3;
136
137     /**
138     * Index of the offset information
139     */

140     private int m_tokenstringindex_;
141     private int m_groupindex_;
142     private int m_groupstringindex_;
143     private int m_algnamesindex_;
144       
145     /**
146     * Size of an algorithmic name information group
147     * start code point size + end code point size + type size + variant size +
148     * size of data size
149     */

150     private static final int ALG_INFO_SIZE_ = 12;
151       
152     /**
153     * File format version and id that this class understands.
154     * No guarantees are made if a older version is used
155     */

156     private static final byte DATA_FORMAT_VERSION_[] =
157                                     {(byte)0x1, (byte)0x0, (byte)0x0, (byte)0x0};
158     private static final byte DATA_FORMAT_ID_[] = {(byte)0x75, (byte)0x6E,
159                                                     (byte)0x61, (byte)0x6D};
160       
161     // private methods ---------------------------------------------------
162

163     /**
164     * Reads an individual record of AlgorithmNames
165     * @return an instance of AlgorithNames if read is successful otherwise null
166     * @exception IOException thrown when file read error occurs or data is corrupted
167     */

168     private UCharacterName.AlgorithmName readAlg() throws IOException JavaDoc
169     {
170         UCharacterName.AlgorithmName result =
171                                        new UCharacterName.AlgorithmName();
172         int rangestart = m_dataInputStream_.readInt();
173         int rangeend = m_dataInputStream_.readInt();
174         byte type = m_dataInputStream_.readByte();
175         byte variant = m_dataInputStream_.readByte();
176         if (!result.setInfo(rangestart, rangeend, type, variant)) {
177             return null;
178         }
179                          
180         int size = m_dataInputStream_.readChar();
181         if (type == UCharacterName.AlgorithmName.TYPE_1_)
182         {
183             char factor[] = new char[variant];
184             for (int j = 0; j < variant; j ++) {
185                 factor[j] = m_dataInputStream_.readChar();
186             }
187                   
188             result.setFactor(factor);
189             size -= (variant << 1);
190         }
191           
192         StringBuffer JavaDoc prefix = new StringBuffer JavaDoc();
193         char c = (char)(m_dataInputStream_.readByte() & 0x00FF);
194         while (c != 0)
195         {
196             prefix.append(c);
197             c = (char)(m_dataInputStream_.readByte() & 0x00FF);
198         }
199         
200         result.setPrefix(prefix.toString());
201         
202         size -= (ALG_INFO_SIZE_ + prefix.length() + 1);
203         
204         if (size > 0)
205         {
206             byte string[] = new byte[size];
207             m_dataInputStream_.readFully(string);
208             result.setFactorString(string);
209         }
210         return result;
211     }
212 }
213
214
Popular Tags