Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/text/normalizer/ICUBinary.java
38830 views
/*1* Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*******************************************************************************27* (C) Copyright IBM Corp. 1996-2005 - 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*/3637package sun.text.normalizer;3839import java.io.InputStream;40import java.io.DataInputStream;41import java.io.IOException;42import java.util.Arrays;4344public final class ICUBinary45{46// public inner interface ------------------------------------------------4748/**49* Special interface for data authentication50*/51public static interface Authenticate52{53/**54* Method used in ICUBinary.readHeader() to provide data format55* authentication.56* @param version version of the current data57* @return true if dataformat is an acceptable version, false otherwise58*/59public boolean isDataVersionAcceptable(byte version[]);60}6162// public methods --------------------------------------------------------6364/**65* <p>ICU data header reader method.66* Takes a ICU generated big-endian input stream, parse the ICU standard67* file header and authenticates them.</p>68* <p>Header format:69* <ul>70* <li> Header size (char)71* <li> Magic number 1 (byte)72* <li> Magic number 2 (byte)73* <li> Rest of the header size (char)74* <li> Reserved word (char)75* <li> Big endian indicator (byte)76* <li> Character set family indicator (byte)77* <li> Size of a char (byte) for c++ and c use78* <li> Reserved byte (byte)79* <li> Data format identifier (4 bytes), each ICU data has its own80* identifier to distinguish them. [0] major [1] minor81* [2] milli [3] micro82* <li> Data version (4 bytes), the change version of the ICU data83* [0] major [1] minor [2] milli [3] micro84* <li> Unicode version (4 bytes) this ICU is based on.85* </ul>86* </p>87* <p>88* Example of use:<br>89* <pre>90* try {91* FileInputStream input = new FileInputStream(filename);92* If (Utility.readICUDataHeader(input, dataformat, dataversion,93* unicode) {94* System.out.println("Verified file header, this is a ICU data file");95* }96* } catch (IOException e) {97* System.out.println("This is not a ICU data file");98* }99* </pre>100* </p>101* @param inputStream input stream that contains the ICU data header102* @param dataFormatIDExpected Data format expected. An array of 4 bytes103* information about the data format.104* E.g. data format ID 1.2.3.4. will became an array of105* {1, 2, 3, 4}106* @param authenticate user defined extra data authentication. This value107* can be null, if no extra authentication is needed.108* @exception IOException thrown if there is a read error or109* when header authentication fails.110* @draft 2.1111*/112public static final byte[] readHeader(InputStream inputStream,113byte dataFormatIDExpected[],114Authenticate authenticate)115throws IOException116{117DataInputStream input = new DataInputStream(inputStream);118char headersize = input.readChar();119int readcount = 2;120//reading the header format121byte magic1 = input.readByte();122readcount ++;123byte magic2 = input.readByte();124readcount ++;125if (magic1 != MAGIC1 || magic2 != MAGIC2) {126throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);127}128129input.readChar(); // reading size130readcount += 2;131input.readChar(); // reading reserved word132readcount += 2;133byte bigendian = input.readByte();134readcount ++;135byte charset = input.readByte();136readcount ++;137byte charsize = input.readByte();138readcount ++;139input.readByte(); // reading reserved byte140readcount ++;141142byte dataFormatID[] = new byte[4];143input.readFully(dataFormatID);144readcount += 4;145byte dataVersion[] = new byte[4];146input.readFully(dataVersion);147readcount += 4;148byte unicodeVersion[] = new byte[4];149input.readFully(unicodeVersion);150readcount += 4;151if (headersize < readcount) {152throw new IOException("Internal Error: Header size error");153}154input.skipBytes(headersize - readcount);155156if (bigendian != BIG_ENDIAN_ || charset != CHAR_SET_157|| charsize != CHAR_SIZE_158|| !Arrays.equals(dataFormatIDExpected, dataFormatID)159|| (authenticate != null160&& !authenticate.isDataVersionAcceptable(dataVersion))) {161throw new IOException(HEADER_AUTHENTICATION_FAILED_);162}163return unicodeVersion;164}165166// private variables -------------------------------------------------167168/**169* Magic numbers to authenticate the data file170*/171private static final byte MAGIC1 = (byte)0xda;172private static final byte MAGIC2 = (byte)0x27;173174/**175* File format authentication values176*/177private static final byte BIG_ENDIAN_ = 1;178private static final byte CHAR_SET_ = 0;179private static final byte CHAR_SIZE_ = 2;180181/**182* Error messages183*/184private static final String MAGIC_NUMBER_AUTHENTICATION_FAILED_ =185"ICU data file error: Not an ICU data file";186private static final String HEADER_AUTHENTICATION_FAILED_ =187"ICU data file error: Header authentication failed, please check if you have a valid ICU data file";188}189190191