Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/util/resources/BreakIteratorResourceBundle.java
41159 views
1
/*
2
* Copyright (c) 2016, 2021, 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
package sun.util.resources;
27
28
import java.io.InputStream;
29
import java.security.AccessController;
30
import java.security.PrivilegedActionException;
31
import java.security.PrivilegedExceptionAction;
32
import java.util.Collections;
33
import java.util.Enumeration;
34
import java.util.ResourceBundle;
35
import java.util.Set;
36
37
/**
38
* BreakIteratorResourceBundle is an abstract class for loading BreakIterator
39
* data (rules or dictionary) from each module. An implementation class must
40
* implement getBreakIteratorInfo() that returns an instance of the
41
* corresponding BreakIteratorInfo (basename). The data name is taken from the
42
* BreakIteratorInfo instance.
43
*
44
* <p>For example, if the given key is "WordDictionary" and Locale is "th", the
45
* data name is taken from a BreakIteratorInfo_th and the key's value is
46
* "thai_dict". Its data thai_dict is loaded from the Module of the
47
* implementation class of this class.
48
*/
49
50
public abstract class BreakIteratorResourceBundle extends ResourceBundle {
51
// If any keys that are not for data names are added to BreakIteratorInfo*,
52
// those keys must be added to NON_DATA_KEYS.
53
private static final Set<String> NON_DATA_KEYS = Set.of("BreakIteratorClasses");
54
55
private volatile Set<String> keys;
56
57
/**
58
* Returns an instance of the corresponding {@code BreakIteratorInfo} (basename).
59
* The instance shouldn't have its parent.
60
*/
61
protected abstract ResourceBundle getBreakIteratorInfo();
62
63
@Override
64
protected Object handleGetObject(String key) {
65
if (NON_DATA_KEYS.contains(key)) {
66
return null;
67
}
68
ResourceBundle info = getBreakIteratorInfo();
69
if (!info.containsKey(key)) {
70
return null;
71
}
72
String path = getClass().getPackageName().replace('.', '/')
73
+ '/' + info.getString(key);
74
byte[] data;
75
try (InputStream is = getResourceAsStream(path)) {
76
data = is.readAllBytes();
77
} catch (Exception e) {
78
throw new InternalError("Can't load " + path, e);
79
}
80
return data;
81
}
82
83
@SuppressWarnings("removal")
84
private InputStream getResourceAsStream(String path) throws Exception {
85
PrivilegedExceptionAction<InputStream> pa;
86
pa = () -> getClass().getModule().getResourceAsStream(path);
87
InputStream is;
88
try {
89
is = AccessController.doPrivileged(pa);
90
} catch (PrivilegedActionException e) {
91
throw e.getException();
92
}
93
return is;
94
}
95
96
@Override
97
public Enumeration<String> getKeys() {
98
return Collections.enumeration(keySet());
99
}
100
101
@Override
102
protected Set<String> handleKeySet() {
103
if (keys == null) {
104
ResourceBundle info = getBreakIteratorInfo();
105
Set<String> k = info.keySet();
106
k.removeAll(NON_DATA_KEYS);
107
synchronized (this) {
108
if (keys == null) {
109
keys = k;
110
}
111
}
112
}
113
return keys;
114
}
115
}
116
117