UCharacterNameReader.java revision 2ae130017183d2f66d55bf0ca51f8da3294644fd
12ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/* GENERATED SOURCE. DO NOT MODIFY. */
22ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/*
32ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *******************************************************************************
42ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Copyright (C) 1996-2015, International Business Machines Corporation and
52ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * others. All Rights Reserved.
62ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *******************************************************************************
72ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */
82ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
92ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpackage android.icu.impl;
102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.io.IOException;
122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.nio.ByteBuffer;
132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.util.Arrays;
142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/**
162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* <p>Internal reader class for ICU data file uname.dat containing
172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* Unicode codepoint name data.</p>
182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* <p>This class simply reads unames.icu, authenticates that it is a valid
192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* ICU data file and split its contents up into blocks of data for use in
202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* <a href=UCharacterName.html>com.ibm.icu.impl.UCharacterName</a>.
212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* </p>
222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* <p>unames.icu which is in big-endian format is jared together with this
232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* package.</p>
242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* @author Syn Wee Quek
252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller* @since release 2.1, February 1st 2002
262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*/
272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerfinal class UCharacterNameReader implements ICUBinary.Authenticate
292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller{
302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // public methods ----------------------------------------------------
312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public boolean isDataVersionAcceptable(byte version[])
332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    {
342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return version[0] == 1;
352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // protected constructor ---------------------------------------------
382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * <p>Protected constructor.</p>
412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * @param bytes ICU uprop.dat file buffer
422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * @exception IOException throw if data file fails authentication
432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    */
442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    protected UCharacterNameReader(ByteBuffer bytes) throws IOException
452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    {
462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        ICUBinary.readHeader(bytes, DATA_FORMAT_ID_, this);
472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        m_byteBuffer_ = bytes;
482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // protected methods -------------------------------------------------
512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * Read and break up the stream of data passed in as arguments
542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * and fills up UCharacterName.
552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * If unsuccessful false will be returned.
562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * @param data instance of datablock
572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * @exception IOException thrown when there's a data error.
582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    */
592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    protected void read(UCharacterName data) throws IOException
602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    {
612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // reading index
622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        m_tokenstringindex_ = m_byteBuffer_.getInt();
632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        m_groupindex_       = m_byteBuffer_.getInt();
642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        m_groupstringindex_ = m_byteBuffer_.getInt();
652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        m_algnamesindex_    = m_byteBuffer_.getInt();
662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // reading tokens
682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int count = m_byteBuffer_.getChar();
692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        char token[] = ICUBinary.getChars(m_byteBuffer_, count, 0);
702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int size = m_groupindex_ - m_tokenstringindex_;
712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        byte tokenstr[] = new byte[size];
722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        m_byteBuffer_.get(tokenstr);
732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        data.setToken(token, tokenstr);
742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // reading the group information records
762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        count = m_byteBuffer_.getChar();
772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        data.setGroupCountSize(count, GROUP_INFO_SIZE_);
782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        count *= GROUP_INFO_SIZE_;
792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        char group[] = ICUBinary.getChars(m_byteBuffer_, count, 0);
802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        size = m_algnamesindex_ - m_groupstringindex_;
822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        byte groupstring[] = new byte[size];
832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        m_byteBuffer_.get(groupstring);
842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        data.setGroup(group, groupstring);
862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        count = m_byteBuffer_.getInt();
882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        UCharacterName.AlgorithmName alg[] =
892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                                 new UCharacterName.AlgorithmName[count];
902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for (int i = 0; i < count; i ++)
922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        {
932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            UCharacterName.AlgorithmName an = readAlg();
942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if (an == null) {
952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                throw new IOException("unames.icu read error: Algorithmic names creation error");
962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            alg[i] = an;
982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        data.setAlgorithm(alg);
1002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * <p>Checking the file for the correct format.</p>
1042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * @param dataformatid
1052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * @param dataformatversion
1062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * @return true if the file format version is correct
1072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    */
1082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    ///CLOVER:OFF
1092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    protected boolean authenticate(byte dataformatid[],
1102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                                   byte dataformatversion[])
1112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    {
1122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return Arrays.equals(
1132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ICUBinary.getVersionByteArrayFromCompactInt(DATA_FORMAT_ID_),
1142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                dataformatid) &&
1152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller               isDataVersionAcceptable(dataformatversion);
1162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    ///CLOVER:ON
1182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // private variables -------------------------------------------------
1202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * Byte buffer for names
1232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    */
1242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private ByteBuffer m_byteBuffer_;
1252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * Size of the group information block in number of char
1272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    */
1282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static final int GROUP_INFO_SIZE_ = 3;
1292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * Index of the offset information
1322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    */
1332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int m_tokenstringindex_;
1342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int m_groupindex_;
1352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int m_groupstringindex_;
1362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int m_algnamesindex_;
1372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * Size of an algorithmic name information group
1402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * start code point size + end code point size + type size + variant size +
1412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * size of data size
1422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    */
1432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static final int ALG_INFO_SIZE_ = 12;
1442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * File format id that this class understands.
1472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    */
1482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static final int DATA_FORMAT_ID_ = 0x756E616D;
1492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // private methods ---------------------------------------------------
1512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * Reads an individual record of AlgorithmNames
1542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * @return an instance of AlgorithNames if read is successful otherwise null
1552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    * @exception IOException thrown when file read error occurs or data is corrupted
1562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    */
1572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private UCharacterName.AlgorithmName readAlg() throws IOException
1582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    {
1592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        UCharacterName.AlgorithmName result =
1602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                                       new UCharacterName.AlgorithmName();
1612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int rangestart = m_byteBuffer_.getInt();
1622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int rangeend   = m_byteBuffer_.getInt();
1632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        byte type      = m_byteBuffer_.get();
1642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        byte variant   = m_byteBuffer_.get();
1652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (!result.setInfo(rangestart, rangeend, type, variant)) {
1662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return null;
1672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int size = m_byteBuffer_.getChar();
1702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (type == UCharacterName.AlgorithmName.TYPE_1_)
1712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        {
1722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            char factor[] = ICUBinary.getChars(m_byteBuffer_, variant, 0);
1732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            result.setFactor(factor);
1752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            size -= (variant << 1);
1762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        StringBuilder prefix = new StringBuilder();
1792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        char c = (char)(m_byteBuffer_.get() & 0x00FF);
1802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        while (c != 0)
1812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        {
1822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            prefix.append(c);
1832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            c = (char)(m_byteBuffer_.get() & 0x00FF);
1842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        result.setPrefix(prefix.toString());
1872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        size -= (ALG_INFO_SIZE_ + prefix.length() + 1);
1892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (size > 0)
1912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        {
1922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            byte string[] = new byte[size];
1932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            m_byteBuffer_.get(string);
1942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            result.setFactorString(string);
1952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return result;
1972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller}
199