View Javadoc
1   /*
2    * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  /*
26   *******************************************************************************
27   * (C) Copyright IBM Corp. and others, 1996-2009 - All Rights Reserved         *
28   *                                                                             *
29   * The original version of this source code and documentation is copyrighted   *
30   * and owned by IBM, These materials are provided under terms of a License     *
31   * Agreement between IBM and Sun. This technology is protected by multiple     *
32   * US and International patents. This notice and attribution to IBM may not    *
33   * to removed.                                                                 *
34   *******************************************************************************
35   */
36  
37  package sun.text.normalizer;
38  
39  import java.io.DataInputStream;
40  import java.io.InputStream;
41  import java.io.IOException;
42  
43  /**
44  * <p>Internal reader class for ICU data file uprops.icu containing
45  * Unicode codepoint data.</p>
46  * <p>This class simply reads uprops.icu, authenticates that it is a valid
47  * ICU data file and split its contents up into blocks of data for use in
48  * <a href=UCharacterProperty.html>com.ibm.icu.impl.UCharacterProperty</a>.
49  * </p>
50  * <p>uprops.icu which is in big-endian format is jared together with this
51  * package.</p>
52  *
53  * Unicode character properties file format see
54  * (ICU4C)/source/tools/genprops/store.c
55  *
56  * @author Syn Wee Quek
57  * @since release 2.1, February 1st 2002
58  */
59  final class UCharacterPropertyReader implements ICUBinary.Authenticate
60  {
61      // public methods ----------------------------------------------------
62  
63      public boolean isDataVersionAcceptable(byte version[])
64      {
65          return version[0] == DATA_FORMAT_VERSION_[0]
66                 && version[2] == DATA_FORMAT_VERSION_[2]
67                 && version[3] == DATA_FORMAT_VERSION_[3];
68      }
69  
70      // protected constructor ---------------------------------------------
71  
72      /**
73      * <p>Protected constructor.</p>
74      * @param inputStream ICU uprop.dat file input stream
75      * @exception IOException throw if data file fails authentication
76      */
77      protected UCharacterPropertyReader(InputStream inputStream)
78                                                          throws IOException
79      {
80          m_unicodeVersion_ = ICUBinary.readHeader(inputStream, DATA_FORMAT_ID_,
81                                                   this);
82          m_dataInputStream_ = new DataInputStream(inputStream);
83      }
84  
85      // protected methods -------------------------------------------------
86  
87      /**
88      * <p>Reads uprops.icu, parse it into blocks of data to be stored in
89      * UCharacterProperty.</P
90      * @param ucharppty UCharacterProperty instance
91      * @exception IOException thrown when data reading fails
92      */
93      protected void read(UCharacterProperty ucharppty) throws IOException
94      {
95          // read the indexes
96          int count = INDEX_SIZE_;
97          m_propertyOffset_          = m_dataInputStream_.readInt();
98          count --;
99          m_exceptionOffset_         = m_dataInputStream_.readInt();
100         count --;
101         m_caseOffset_              = m_dataInputStream_.readInt();
102         count --;
103         m_additionalOffset_        = m_dataInputStream_.readInt();
104         count --;
105         m_additionalVectorsOffset_ = m_dataInputStream_.readInt();
106         count --;
107         m_additionalColumnsCount_  = m_dataInputStream_.readInt();
108         count --;
109         m_reservedOffset_          = m_dataInputStream_.readInt();
110         count --;
111         m_dataInputStream_.skipBytes(3 << 2);
112         count -= 3;
113         ucharppty.m_maxBlockScriptValue_ = m_dataInputStream_.readInt();
114         count --; // 10
115         ucharppty.m_maxJTGValue_ = m_dataInputStream_.readInt();
116         count --; // 11
117         m_dataInputStream_.skipBytes(count << 2);
118 
119         // read the trie index block
120         // m_props_index_ in terms of ints
121         ucharppty.m_trie_ = new CharTrie(m_dataInputStream_, null);
122 
123         // skip the 32 bit properties block
124         int size = m_exceptionOffset_ - m_propertyOffset_;
125         m_dataInputStream_.skipBytes(size * 4);
126 
127         // reads the 32 bit exceptions block
128         size = m_caseOffset_ - m_exceptionOffset_;
129         m_dataInputStream_.skipBytes(size * 4);
130 
131         // reads the 32 bit case block
132         size = (m_additionalOffset_ - m_caseOffset_) << 1;
133         m_dataInputStream_.skipBytes(size * 2);
134 
135         if(m_additionalColumnsCount_ > 0) {
136             // reads the additional property block
137             ucharppty.m_additionalTrie_ = new CharTrie(m_dataInputStream_, null);
138 
139             // additional properties
140             size = m_reservedOffset_ - m_additionalVectorsOffset_;
141             ucharppty.m_additionalVectors_ = new int[size];
142             for (int i = 0; i < size; i ++) {
143                 ucharppty.m_additionalVectors_[i] = m_dataInputStream_.readInt();
144             }
145         }
146 
147         m_dataInputStream_.close();
148         ucharppty.m_additionalColumnsCount_ = m_additionalColumnsCount_;
149         ucharppty.m_unicodeVersion_ = VersionInfo.getInstance(
150                          (int)m_unicodeVersion_[0], (int)m_unicodeVersion_[1],
151                          (int)m_unicodeVersion_[2], (int)m_unicodeVersion_[3]);
152     }
153 
154     // private variables -------------------------------------------------
155 
156     /**
157     * Index size
158     */
159     private static final int INDEX_SIZE_ = 16;
160 
161     /**
162     * ICU data file input stream
163     */
164     private DataInputStream m_dataInputStream_;
165 
166     /**
167     * Offset information in the indexes.
168     */
169     private int m_propertyOffset_;
170     private int m_exceptionOffset_;
171     private int m_caseOffset_;
172     private int m_additionalOffset_;
173     private int m_additionalVectorsOffset_;
174     private int m_additionalColumnsCount_;
175     private int m_reservedOffset_;
176     private byte m_unicodeVersion_[];
177 
178     /**
179     * Data format "UPro".
180     */
181     private static final byte DATA_FORMAT_ID_[] = {(byte)0x55, (byte)0x50,
182                                                     (byte)0x72, (byte)0x6F};
183     /**
184      * Format version; this code works with all versions with the same major
185      * version number and the same Trie bit distribution.
186      */
187     private static final byte DATA_FORMAT_VERSION_[] = {(byte)0x5, (byte)0,
188                                              (byte)Trie.INDEX_STAGE_1_SHIFT_,
189                                              (byte)Trie.INDEX_STAGE_2_SHIFT_};
190 }