1 /*
   2  * Copyright (c) 2005, 2020, 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  ******************************************************************************
  28  * Copyright (C) 2003, International Business Machines Corporation and   *
  29  * others. All Rights Reserved.                                               *
  30  ******************************************************************************
  31  *
  32  * Created on May 2, 2003
  33  *
  34  * To change the template for this generated file go to
  35  * Window>Preferences>Java>Code Generation>Code and Comments
  36  */
  37 // CHANGELOG
  38 //      2005-05-19 Edward Wang
  39 //          - copy this file from icu4jsrc_3_2/src/com/ibm/icu/impl/StringPrepDataReader.java
  40 //          - move from package com.ibm.icu.impl to package sun.net.idn
  41 //
  42 package jdk.internal.icu.impl;
  43 
  44 import java.io.DataInputStream;
  45 import java.io.IOException;
  46 import java.io.InputStream;
  47 
  48 import jdk.internal.icu.impl.ICUBinary;
  49 
  50 
  51 /**
  52  * @author ram
  53  *
  54  * To change the template for this generated type comment go to
  55  * Window>Preferences>Java>Code Generation>Code and Comments
  56  */
  57 public final class StringPrepDataReader implements ICUBinary.Authenticate {
  58 
  59    /**
  60     * <p>private constructor.</p>
  61     * @param inputStream ICU uprop.dat file input stream
  62     * @exception IOException throw if data file fails authentication
  63     * @draft 2.1
  64     */
  65     public StringPrepDataReader(InputStream inputStream)
  66                                         throws IOException{
  67 
  68         unicodeVersion = ICUBinary.readHeader(inputStream, DATA_FORMAT_ID, this);
  69 
  70 
  71         dataInputStream = new DataInputStream(inputStream);
  72 
  73     }
  74 
  75     public void read(byte[] idnaBytes,
  76                         char[] mappingTable)
  77                         throws IOException{
  78 
  79         //Read the bytes that make up the idnaTrie
  80         dataInputStream.read(idnaBytes);
  81 
  82         //Read the extra data
  83         for(int i=0;i<mappingTable.length;i++){
  84             mappingTable[i]=dataInputStream.readChar();
  85         }
  86     }
  87 
  88     public byte[] getDataFormatVersion(){
  89         return DATA_FORMAT_VERSION;
  90     }
  91 
  92     public boolean isDataVersionAcceptable(byte version[]){
  93         return version[0] == DATA_FORMAT_VERSION[0]
  94                && version[2] == DATA_FORMAT_VERSION[2]
  95                && version[3] == DATA_FORMAT_VERSION[3];
  96     }
  97     public int[] readIndexes(int length)throws IOException{
  98         int[] indexes = new int[length];
  99         //Read the indexes
 100         for (int i = 0; i <length ; i++) {
 101              indexes[i] = dataInputStream.readInt();
 102         }
 103         return indexes;
 104     }
 105 
 106     public byte[] getUnicodeVersion(){
 107         return unicodeVersion;
 108     }
 109     // private data members -------------------------------------------------
 110 
 111 
 112     /**
 113     * ICU data file input stream
 114     */
 115     private DataInputStream dataInputStream;
 116     private byte[] unicodeVersion;
 117     /**
 118     * File format version that this class understands.
 119     * No guarantees are made if a older version is used
 120     * see store.c of gennorm for more information and values
 121     */
 122     ///* dataFormat="SPRP" 0x53, 0x50, 0x52, 0x50  */
 123     private static final byte DATA_FORMAT_ID[] = {(byte)0x53, (byte)0x50,
 124                                                     (byte)0x52, (byte)0x50};
 125     private static final byte DATA_FORMAT_VERSION[] = {(byte)0x3, (byte)0x2,
 126                                                         (byte)0x5, (byte)0x2};
 127 
 128 }