KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.ibm.icu.util.VersionInfo;
13
14 /**
15 * <p>Internal reader class for ICU data file uprops.icu containing
16 * Unicode codepoint data.</p>
17 * <p>This class simply reads uprops.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=UCharacterProperty.html>com.ibm.icu.impl.UCharacterProperty</a>.
20 * </p>
21 * <p>uprops.icu which is in big-endian format is jared together with this
22 * package.</p>
23 *
24 * Unicode character properties file format see
25 * (ICU4C)/source/tools/genprops/store.c
26 *
27 * @author Syn Wee Quek
28 * @since release 2.1, February 1st 2002
29 */

30 final class UCharacterPropertyReader implements ICUBinary.Authenticate
31 {
32     // public methods ----------------------------------------------------
33

34     public boolean isDataVersionAcceptable(byte version[])
35     {
36         return version[0] == DATA_FORMAT_VERSION_[0]
37                && version[2] == DATA_FORMAT_VERSION_[2]
38                && version[3] == DATA_FORMAT_VERSION_[3];
39     }
40     
41     // protected constructor ---------------------------------------------
42

43     /**
44     * <p>Protected constructor.</p>
45     * @param inputStream ICU uprop.dat file input stream
46     * @exception IOException throw if data file fails authentication
47     * @draft 2.1
48     */

49     protected UCharacterPropertyReader(InputStream JavaDoc inputStream)
50                                                         throws IOException JavaDoc
51     {
52         m_unicodeVersion_ = ICUBinary.readHeader(inputStream, DATA_FORMAT_ID_,
53                                                  this);
54         m_dataInputStream_ = new DataInputStream JavaDoc(inputStream);
55     }
56     
57     // protected methods -------------------------------------------------
58

59     /**
60     * <p>Reads uprops.icu, parse it into blocks of data to be stored in
61     * UCharacterProperty.</P
62     * @param ucharppty UCharacterProperty instance
63     * @exception IOException thrown when data reading fails
64     * @draft 2.1
65     */

66     protected void read(UCharacterProperty ucharppty) throws IOException JavaDoc
67     {
68         // read the indexes
69
int count = INDEX_SIZE_;
70         m_propertyOffset_ = m_dataInputStream_.readInt();
71         count --;
72         m_exceptionOffset_ = m_dataInputStream_.readInt();
73         count --;
74         m_caseOffset_ = m_dataInputStream_.readInt();
75         count --;
76         m_additionalOffset_ = m_dataInputStream_.readInt();
77         count --;
78         m_additionalVectorsOffset_ = m_dataInputStream_.readInt();
79         count --;
80         m_additionalColumnsCount_ = m_dataInputStream_.readInt();
81         count --;
82         m_reservedOffset_ = m_dataInputStream_.readInt();
83         count --;
84         m_dataInputStream_.skipBytes(3 << 2);
85         count -= 3;
86         ucharppty.m_maxBlockScriptValue_ = m_dataInputStream_.readInt();
87         count --; // 10
88
ucharppty.m_maxJTGValue_ = m_dataInputStream_.readInt();
89         count --; // 11
90
m_dataInputStream_.skipBytes(count << 2);
91
92         // read the trie index block
93
// m_props_index_ in terms of ints
94
ucharppty.m_trie_ = new CharTrie(m_dataInputStream_, null);
95
96         // skip the 32 bit properties block
97
int size = m_exceptionOffset_ - m_propertyOffset_;
98         m_dataInputStream_.skipBytes(size * 4);
99
100         // reads the 32 bit exceptions block
101
size = m_caseOffset_ - m_exceptionOffset_;
102         m_dataInputStream_.skipBytes(size * 4);
103
104         // reads the 32 bit case block
105
size = (m_additionalOffset_ - m_caseOffset_) << 1;
106         m_dataInputStream_.skipBytes(size * 2);
107
108         if(m_additionalColumnsCount_ > 0) {
109             // reads the additional property block
110
ucharppty.m_additionalTrie_ = new CharTrie(m_dataInputStream_, null);
111                                                                
112             // additional properties
113
size = m_reservedOffset_ - m_additionalVectorsOffset_;
114             ucharppty.m_additionalVectors_ = new int[size];
115             for (int i = 0; i < size; i ++) {
116                 ucharppty.m_additionalVectors_[i] = m_dataInputStream_.readInt();
117             }
118         }
119
120         m_dataInputStream_.close();
121         ucharppty.m_additionalColumnsCount_ = m_additionalColumnsCount_;
122         ucharppty.m_unicodeVersion_ = VersionInfo.getInstance(
123                          (int)m_unicodeVersion_[0], (int)m_unicodeVersion_[1],
124                          (int)m_unicodeVersion_[2], (int)m_unicodeVersion_[3]);
125     }
126     
127     // private variables -------------------------------------------------
128

129     /**
130     * Index size
131     */

132     private static final int INDEX_SIZE_ = 16;
133     
134     /**
135     * ICU data file input stream
136     */

137     private DataInputStream JavaDoc m_dataInputStream_;
138       
139     /**
140     * Offset information in the indexes.
141     */

142     private int m_propertyOffset_;
143     private int m_exceptionOffset_;
144     private int m_caseOffset_;
145     private int m_additionalOffset_;
146     private int m_additionalVectorsOffset_;
147     private int m_additionalColumnsCount_;
148     private int m_reservedOffset_;
149     private byte m_unicodeVersion_[];
150                                       
151     /**
152     * Data format "UPro".
153     */

154     private static final byte DATA_FORMAT_ID_[] = {(byte)0x55, (byte)0x50,
155                                                     (byte)0x72, (byte)0x6F};
156     /**
157      * Format version; this code works with all versions with the same major
158      * version number and the same Trie bit distribution.
159      */

160     private static final byte DATA_FORMAT_VERSION_[] = {(byte)0x4, (byte)0,
161                                              (byte)Trie.INDEX_STAGE_1_SHIFT_,
162                                              (byte)Trie.INDEX_STAGE_2_SHIFT_};
163 }
164
Popular Tags