Path: blob/master/src/java.base/share/classes/sun/util/resources/BreakIteratorResourceBundle.java
41159 views
/*1* Copyright (c) 2016, 2021, 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*/2425package sun.util.resources;2627import java.io.InputStream;28import java.security.AccessController;29import java.security.PrivilegedActionException;30import java.security.PrivilegedExceptionAction;31import java.util.Collections;32import java.util.Enumeration;33import java.util.ResourceBundle;34import java.util.Set;3536/**37* BreakIteratorResourceBundle is an abstract class for loading BreakIterator38* data (rules or dictionary) from each module. An implementation class must39* implement getBreakIteratorInfo() that returns an instance of the40* corresponding BreakIteratorInfo (basename). The data name is taken from the41* BreakIteratorInfo instance.42*43* <p>For example, if the given key is "WordDictionary" and Locale is "th", the44* data name is taken from a BreakIteratorInfo_th and the key's value is45* "thai_dict". Its data thai_dict is loaded from the Module of the46* implementation class of this class.47*/4849public abstract class BreakIteratorResourceBundle extends ResourceBundle {50// If any keys that are not for data names are added to BreakIteratorInfo*,51// those keys must be added to NON_DATA_KEYS.52private static final Set<String> NON_DATA_KEYS = Set.of("BreakIteratorClasses");5354private volatile Set<String> keys;5556/**57* Returns an instance of the corresponding {@code BreakIteratorInfo} (basename).58* The instance shouldn't have its parent.59*/60protected abstract ResourceBundle getBreakIteratorInfo();6162@Override63protected Object handleGetObject(String key) {64if (NON_DATA_KEYS.contains(key)) {65return null;66}67ResourceBundle info = getBreakIteratorInfo();68if (!info.containsKey(key)) {69return null;70}71String path = getClass().getPackageName().replace('.', '/')72+ '/' + info.getString(key);73byte[] data;74try (InputStream is = getResourceAsStream(path)) {75data = is.readAllBytes();76} catch (Exception e) {77throw new InternalError("Can't load " + path, e);78}79return data;80}8182@SuppressWarnings("removal")83private InputStream getResourceAsStream(String path) throws Exception {84PrivilegedExceptionAction<InputStream> pa;85pa = () -> getClass().getModule().getResourceAsStream(path);86InputStream is;87try {88is = AccessController.doPrivileged(pa);89} catch (PrivilegedActionException e) {90throw e.getException();91}92return is;93}9495@Override96public Enumeration<String> getKeys() {97return Collections.enumeration(keySet());98}99100@Override101protected Set<String> handleKeySet() {102if (keys == null) {103ResourceBundle info = getBreakIteratorInfo();104Set<String> k = info.keySet();105k.removeAll(NON_DATA_KEYS);106synchronized (this) {107if (keys == null) {108keys = k;109}110}111}112return keys;113}114}115116117