Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/locale/provider/BreakDictionary.java
38918 views
/*1* Copyright (c) 1999, 2012, 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 Taligent, Inc. 1996, 1997 - All Rights Reserved28* (C) Copyright IBM Corp. 1996 - 2002 - All Rights Reserved29*30* The original version of this source code and documentation31* is copyrighted and owned by Taligent, Inc., a wholly-owned32* subsidiary of IBM. These materials are provided under terms33* of a License Agreement between Taligent and Sun. This technology34* is protected by multiple US and International patents.35*36* This notice and attribution to Taligent may not be removed.37* Taligent is a registered trademark of Taligent, Inc.38*/39package sun.util.locale.provider;4041import java.io.BufferedInputStream;42import java.io.IOException;43import java.security.AccessController;44import java.security.PrivilegedActionException;45import java.security.PrivilegedExceptionAction;46import java.util.MissingResourceException;47import sun.text.CompactByteArray;48import sun.text.SupplementaryCharacterData;4950/**51* This is the class that represents the list of known words used by52* DictionaryBasedBreakIterator. The conceptual data structure used53* here is a trie: there is a node hanging off the root node for every54* letter that can start a word. Each of these nodes has a node hanging55* off of it for every letter that can be the second letter of a word56* if this node is the first letter, and so on. The trie is represented57* as a two-dimensional array that can be treated as a table of state58* transitions. Indexes are used to compress this array, taking59* advantage of the fact that this array will always be very sparse.60*/61class BreakDictionary {6263//=========================================================================64// data members65//=========================================================================6667/**68* The version of the dictionary that was read in.69*/70private static int supportedVersion = 1;7172/**73* Maps from characters to column numbers. The main use of this is to74* avoid making room in the array for empty columns.75*/76private CompactByteArray columnMap = null;77private SupplementaryCharacterData supplementaryCharColumnMap = null;7879/**80* The number of actual columns in the table81*/82private int numCols;8384/**85* Columns are organized into groups of 32. This says how many86* column groups. (We could calculate this, but we store the87* value to avoid having to repeatedly calculate it.)88*/89private int numColGroups;9091/**92* The actual compressed state table. Each conceptual row represents93* a state, and the cells in it contain the row numbers of the states94* to transition to for each possible letter. 0 is used to indicate95* an illegal combination of letters (i.e., the error state). The96* table is compressed by eliminating all the unpopulated (i.e., zero)97* cells. Multiple conceptual rows can then be doubled up in a single98* physical row by sliding them up and possibly shifting them to one99* side or the other so the populated cells don't collide. Indexes100* are used to identify unpopulated cells and to locate populated cells.101*/102private short[] table = null;103104/**105* This index maps logical row numbers to physical row numbers106*/107private short[] rowIndex = null;108109/**110* A bitmap is used to tell which cells in the comceptual table are111* populated. This array contains all the unique bit combinations112* in that bitmap. If the table is more than 32 columns wide,113* successive entries in this array are used for a single row.114*/115private int[] rowIndexFlags = null;116117/**118* This index maps from a logical row number into the bitmap table above.119* (This keeps us from storing duplicate bitmap combinations.) Since there120* are a lot of rows with only one populated cell, instead of wasting space121* in the bitmap table, we just store a negative number in this index for122* rows with one populated cell. The absolute value of that number is123* the column number of the populated cell.124*/125private short[] rowIndexFlagsIndex = null;126127/**128* For each logical row, this index contains a constant that is added to129* the logical column number to get the physical column number130*/131private byte[] rowIndexShifts = null;132133//=========================================================================134// deserialization135//=========================================================================136137BreakDictionary(String dictionaryName)138throws IOException, MissingResourceException {139140readDictionaryFile(dictionaryName);141}142143private void readDictionaryFile(final String dictionaryName)144throws IOException, MissingResourceException {145146BufferedInputStream in;147try {148in = AccessController.doPrivileged(149new PrivilegedExceptionAction<BufferedInputStream>() {150@Override151public BufferedInputStream run() throws Exception {152return new BufferedInputStream(getClass().getResourceAsStream("/sun/text/resources/" + dictionaryName));153}154}155);156}157catch (PrivilegedActionException e) {158throw new InternalError(e.toString(), e);159}160161byte[] buf = new byte[8];162if (in.read(buf) != 8) {163throw new MissingResourceException("Wrong data length",164dictionaryName, "");165}166167// check version168int version = RuleBasedBreakIterator.getInt(buf, 0);169if (version != supportedVersion) {170throw new MissingResourceException("Dictionary version(" + version + ") is unsupported",171dictionaryName, "");172}173174// get data size175int len = RuleBasedBreakIterator.getInt(buf, 4);176buf = new byte[len];177if (in.read(buf) != len) {178throw new MissingResourceException("Wrong data length",179dictionaryName, "");180}181182// close the stream183in.close();184185int l;186int offset = 0;187188// read in the column map for BMP characteres (this is serialized in189// its internal form: an index array followed by a data array)190l = RuleBasedBreakIterator.getInt(buf, offset);191offset += 4;192short[] temp = new short[l];193for (int i = 0; i < l; i++, offset+=2) {194temp[i] = RuleBasedBreakIterator.getShort(buf, offset);195}196l = RuleBasedBreakIterator.getInt(buf, offset);197offset += 4;198byte[] temp2 = new byte[l];199for (int i = 0; i < l; i++, offset++) {200temp2[i] = buf[offset];201}202columnMap = new CompactByteArray(temp, temp2);203204// read in numCols and numColGroups205numCols = RuleBasedBreakIterator.getInt(buf, offset);206offset += 4;207numColGroups = RuleBasedBreakIterator.getInt(buf, offset);208offset += 4;209210// read in the row-number index211l = RuleBasedBreakIterator.getInt(buf, offset);212offset += 4;213rowIndex = new short[l];214for (int i = 0; i < l; i++, offset+=2) {215rowIndex[i] = RuleBasedBreakIterator.getShort(buf, offset);216}217218// load in the populated-cells bitmap: index first, then bitmap list219l = RuleBasedBreakIterator.getInt(buf, offset);220offset += 4;221rowIndexFlagsIndex = new short[l];222for (int i = 0; i < l; i++, offset+=2) {223rowIndexFlagsIndex[i] = RuleBasedBreakIterator.getShort(buf, offset);224}225l = RuleBasedBreakIterator.getInt(buf, offset);226offset += 4;227rowIndexFlags = new int[l];228for (int i = 0; i < l; i++, offset+=4) {229rowIndexFlags[i] = RuleBasedBreakIterator.getInt(buf, offset);230}231232// load in the row-shift index233l = RuleBasedBreakIterator.getInt(buf, offset);234offset += 4;235rowIndexShifts = new byte[l];236for (int i = 0; i < l; i++, offset++) {237rowIndexShifts[i] = buf[offset];238}239240// load in the actual state table241l = RuleBasedBreakIterator.getInt(buf, offset);242offset += 4;243table = new short[l];244for (int i = 0; i < l; i++, offset+=2) {245table[i] = RuleBasedBreakIterator.getShort(buf, offset);246}247248// finally, prepare the column map for supplementary characters249l = RuleBasedBreakIterator.getInt(buf, offset);250offset += 4;251int[] temp3 = new int[l];252for (int i = 0; i < l; i++, offset+=4) {253temp3[i] = RuleBasedBreakIterator.getInt(buf, offset);254}255supplementaryCharColumnMap = new SupplementaryCharacterData(temp3);256}257258//=========================================================================259// access to the words260//=========================================================================261262/**263* Uses the column map to map the character to a column number, then264* passes the row and column number to getNextState()265* @param row The current state266* @param ch The character whose column we're interested in267* @return The new state to transition to268*/269public final short getNextStateFromCharacter(int row, int ch) {270int col;271if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {272col = columnMap.elementAt((char)ch);273} else {274col = supplementaryCharColumnMap.getValue(ch);275}276return getNextState(row, col);277}278279/**280* Returns the value in the cell with the specified (logical) row and281* column numbers. In DictionaryBasedBreakIterator, the row number is282* a state number, the column number is an input, and the return value283* is the row number of the new state to transition to. (0 is the284* "error" state, and -1 is the "end of word" state in a dictionary)285* @param row The row number of the current state286* @param col The column number of the input character (0 means "not a287* dictionary character")288* @return The row number of the new state to transition to289*/290public final short getNextState(int row, int col) {291if (cellIsPopulated(row, col)) {292// we map from logical to physical row number by looking up the293// mapping in rowIndex; we map from logical column number to294// physical column number by looking up a shift value for this295// logical row and offsetting the logical column number by296// the shift amount. Then we can use internalAt() to actually297// get the value out of the table.298return internalAt(rowIndex[row], col + rowIndexShifts[row]);299}300else {301return 0;302}303}304305/**306* Given (logical) row and column numbers, returns true if the307* cell in that position is populated308*/309private boolean cellIsPopulated(int row, int col) {310// look up the entry in the bitmap index for the specified row.311// If it's a negative number, it's the column number of the only312// populated cell in the row313if (rowIndexFlagsIndex[row] < 0) {314return col == -rowIndexFlagsIndex[row];315}316317// if it's a positive number, it's the offset of an entry in the bitmap318// list. If the table is more than 32 columns wide, the bitmap is stored319// successive entries in the bitmap list, so we have to divide the column320// number by 32 and offset the number we got out of the index by the result.321// Once we have the appropriate piece of the bitmap, test the appropriate322// bit and return the result.323else {324int flags = rowIndexFlags[rowIndexFlagsIndex[row] + (col >> 5)];325return (flags & (1 << (col & 0x1f))) != 0;326}327}328329/**330* Implementation of getNextState() when we know the specified cell is331* populated.332* @param row The PHYSICAL row number of the cell333* @param col The PHYSICAL column number of the cell334* @return The value stored in the cell335*/336private short internalAt(int row, int col) {337// the table is a one-dimensional array, so this just does the math necessary338// to treat it as a two-dimensional array (we don't just use a two-dimensional339// array because two-dimensional arrays are inefficient in Java)340return table[row * numCols + col];341}342}343344345