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 *   file name:  UBiDiProps.java
  36 *   encoding:   US-ASCII
  37 *   tab size:   8 (not used)
  38 *   indentation:4
  39 *
  40 *   created on: 2005jan16
  41 *   created by: Markus W. Scherer
  42 *
  43 *   Low-level Unicode bidi/shaping properties access.
  44 *   Java port of ubidi_props.h/.c.
  45 */
  46 
  47 package sun.text.normalizer;
  48 
  49 import java.io.BufferedInputStream;
  50 import java.io.DataInputStream;
  51 import java.io.InputStream;
  52 import java.io.IOException;
  53 
  54 public final class UBiDiProps {
  55     // constructors etc. --------------------------------------------------- ***
  56 
  57     // port of ubidi_openProps()
  58     public UBiDiProps() throws IOException{
  59         InputStream is=ICUData.getStream(DATA_FILE_NAME);
  60         BufferedInputStream b=new BufferedInputStream(is, 4096 /* data buffer size */);
  61         readData(b);
  62         b.close();
  63         is.close();
  64 
  65     }
  66 
  67     private void readData(InputStream is) throws IOException {
  68         DataInputStream inputStream=new DataInputStream(is);
  69 
  70         // read the header
  71         ICUBinary.readHeader(inputStream, FMT, new IsAcceptable());
  72 
  73         // read indexes[]
  74         int i, count;
  75         count=inputStream.readInt();
  76         if(count<IX_INDEX_TOP) {
  77             throw new IOException("indexes[0] too small in "+DATA_FILE_NAME);
  78         }
  79         indexes=new int[count];
  80 
  81         indexes[0]=count;
  82         for(i=1; i<count; ++i) {
  83             indexes[i]=inputStream.readInt();
  84         }
  85 
  86         // read the trie
  87         trie=new CharTrie(inputStream, null);
  88 
  89         // read mirrors[]
  90         count=indexes[IX_MIRROR_LENGTH];
  91         if(count>0) {
  92             mirrors=new int[count];
  93             for(i=0; i<count; ++i) {
  94                 mirrors[i]=inputStream.readInt();
  95             }
  96         }
  97 
  98         // read jgArray[]
  99         count=indexes[IX_JG_LIMIT]-indexes[IX_JG_START];
 100         jgArray=new byte[count];
 101         for(i=0; i<count; ++i) {
 102             jgArray[i]=inputStream.readByte();
 103         }
 104     }
 105 
 106     // implement ICUBinary.Authenticate
 107     private final class IsAcceptable implements ICUBinary.Authenticate {
 108         public boolean isDataVersionAcceptable(byte version[]) {
 109             return version[0]==1 &&
 110                    version[2]==Trie.INDEX_STAGE_1_SHIFT_ && version[3]==Trie.INDEX_STAGE_2_SHIFT_;
 111         }
 112     }
 113 
 114     // UBiDiProps singleton
 115     private static UBiDiProps gBdp=null;
 116 
 117     // port of ubidi_getSingleton()
 118     public static final synchronized UBiDiProps getSingleton() throws IOException {
 119         if(gBdp==null) {
 120             gBdp=new UBiDiProps();
 121         }
 122         return gBdp;
 123     }
 124 
 125     // UBiDiProps dummy singleton
 126     private static UBiDiProps gBdpDummy=null;
 127 
 128     private UBiDiProps(boolean makeDummy) { // ignore makeDummy, only creates a unique signature
 129         indexes=new int[IX_TOP];
 130         indexes[0]=IX_TOP;
 131         trie=new CharTrie(0, 0, null); // dummy trie, always returns 0
 132     }
 133 
 134     /**
 135      * Get a singleton dummy object, one that works with no real data.
 136      * This can be used when the real data is not available.
 137      * Using the dummy can reduce checks for available data after an initial failure.
 138      * Port of ucase_getDummy().
 139      */
 140     public static final synchronized UBiDiProps getDummy() {
 141         if(gBdpDummy==null) {
 142             gBdpDummy=new UBiDiProps(true);
 143         }
 144         return gBdpDummy;
 145     }
 146 
 147     public final int getClass(int c) {
 148         return getClassFromProps(trie.getCodePointValue(c));
 149     }
 150 
 151     // data members -------------------------------------------------------- ***
 152     private int indexes[];
 153     private int mirrors[];
 154     private byte jgArray[];
 155 
 156     private CharTrie trie;
 157 
 158     // data format constants ----------------------------------------------- ***
 159     private static final String DATA_FILE_NAME = "/sun/text/resources/ubidi.icu";
 160 
 161     /* format "BiDi" */
 162     private static final byte FMT[]={ 0x42, 0x69, 0x44, 0x69 };
 163 
 164     /* indexes into indexes[] */
 165     private static final int IX_INDEX_TOP=0;
 166     private static final int IX_MIRROR_LENGTH=3;
 167 
 168     private static final int IX_JG_START=4;
 169     private static final int IX_JG_LIMIT=5;
 170 
 171     private static final int IX_TOP=16;
 172 
 173     private static final int CLASS_MASK=    0x0000001f;
 174 
 175     private static final int getClassFromProps(int props) {
 176         return props&CLASS_MASK;
 177     }
 178 
 179 }