1 /*
2  * Copyright (c) 1996, 2020, 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 /*
27  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
28  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
29  *
30  * The original version of this source code and documentation
31  * is copyrighted and owned by Taligent, Inc., a wholly-owned
32  * subsidiary of IBM. These materials are provided under terms
33  * of a License Agreement between Taligent and Sun. This technology
34  * 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  */
39 
40 package java.util;
41 
42 import static java.nio.charset.StandardCharsets.ISO_8859_1;
43 
44 import java.io.InputStream;
45 import java.io.InputStreamReader;
46 import java.io.Reader;
47 import java.io.IOException;
48 import java.nio.charset.MalformedInputException;
49 import java.nio.charset.StandardCharsets;
50 import java.nio.charset.UnmappableCharacterException;
51 import sun.security.action.GetPropertyAction;
52 import sun.util.PropertyResourceBundleCharset;
53 import sun.util.ResourceBundleEnumeration;
54 
55 /**
56  * {@code PropertyResourceBundle} is a concrete subclass of
57  * {@code ResourceBundle} that manages resources for a locale
58  * using a set of static strings from a property file. See
59  * {@link ResourceBundle ResourceBundle} for more information about resource
60  * bundles.
61  *
62  * <p>
63  * Unlike other types of resource bundle, you don't subclass
64  * {@code PropertyResourceBundle}.  Instead, you supply properties
65  * files containing the resource data.  {@code ResourceBundle.getBundle}
66  * will automatically look for the appropriate properties file and create a
67  * {@code PropertyResourceBundle} that refers to it. See
68  * {@link ResourceBundle#getBundle(String, Locale, ClassLoader) ResourceBundle.getBundle}
69  * for a complete description of the search and instantiation strategy.
70  *
71  * <p>
72  * The following <a id="sample">example</a> shows a member of a resource
73  * bundle family with the base name "MyResources".
74  * The text defines the bundle "MyResources_de",
75  * the German member of the bundle family.
76  * This member is based on {@code PropertyResourceBundle}, and the text
77  * therefore is the content of the file "MyResources_de.properties"
78  * (a related <a href="ListResourceBundle.html#sample">example</a> shows
79  * how you can add bundles to this family that are implemented as subclasses
80  * of {@code ListResourceBundle}).
81  * The keys in this example are of the form "s1" etc. The actual
82  * keys are entirely up to your choice, so long as they are the same as
83  * the keys you use in your program to retrieve the objects from the bundle.
84  * Keys are case-sensitive.
85  * <blockquote>
86  * <pre>
87  * # MessageFormat pattern
88  * s1=Die Platte \"{1}\" enth&auml;lt {0}.
89  *
90  * # location of {0} in pattern
91  * s2=1
92  *
93  * # sample disk name
94  * s3=Meine Platte
95  *
96  * # first ChoiceFormat choice
97  * s4=keine Dateien
98  *
99  * # second ChoiceFormat choice
100  * s5=eine Datei
101  *
102  * # third ChoiceFormat choice
103  * s6={0,number} Dateien
104  *
105  * # sample date
106  * s7=3. M&auml;rz 1996
107  * </pre>
108  * </blockquote>
109  *
110  <!-- Android-removed: we're sticking to old behaviour for now.
111  * @apiNote
112  * {@code PropertyResourceBundle} can be constructed either
113  * from an {@code InputStream} or a {@code Reader}, which represents a property file.
114  * Constructing a {@code PropertyResourceBundle} instance from an {@code InputStream}
115  * requires that the input stream be encoded in {@code UTF-8}. By default, if a
116  * {@link java.nio.charset.MalformedInputException} or an
117  * {@link java.nio.charset.UnmappableCharacterException} occurs on reading the
118  * input stream, then the {@code PropertyResourceBundle} instance resets to the state
119  * before the exception, re-reads the input stream in {@code ISO-8859-1}, and
120  * continues reading. If the system property
121  * {@systemProperty java.util.PropertyResourceBundle.encoding} is set to either
122  * "ISO-8859-1" or "UTF-8", the input stream is solely read in that encoding,
123  * and throws the exception if it encounters an invalid sequence.
124  * If "ISO-8859-1" is specified, characters that cannot be represented in
125  * ISO-8859-1 encoding must be represented by Unicode Escapes as defined in section
126  * {@jls 3.3} of <cite>The Java Language Specification</cite>
127  * whereas the other constructor which takes a {@code Reader} does not have that limitation.
128  * Other encoding values are ignored for this system property.
129  * The system property is read and evaluated when initializing this class.
130  * Changing or removing the property has no effect after the initialization.
131  * --/>
132  * @implSpec
133  * The implementation of a {@code PropertyResourceBundle} subclass must be
134  * thread-safe if it's simultaneously used by multiple threads. The default
135  * implementations of the non-abstract methods in this class are thread-safe.
136  *
137  * @see ResourceBundle
138  * @see ListResourceBundle
139  * @see Properties
140  * @since 1.1
141  */
142 public class PropertyResourceBundle extends ResourceBundle {
143 
144     // Check whether the strict encoding is specified.
145     // The possible encoding is either "ISO-8859-1" or "UTF-8".
146     private static final String encoding = GetPropertyAction
147         .privilegedGetProperty("java.util.PropertyResourceBundle.encoding", "")
148         .toUpperCase(Locale.ROOT);
149 
150     /**
151      * Creates a property resource bundle from an {@link java.io.InputStream
152      * InputStream}. This constructor reads the property file in UTF-8 by default.
153      * If a {@link java.nio.charset.MalformedInputException} or an
154      * {@link java.nio.charset.UnmappableCharacterException} occurs on reading the
155      * input stream, then the PropertyResourceBundle instance resets to the state
156      * before the exception, re-reads the input stream in {@code ISO-8859-1} and
157      * continues reading. If the system property
158      * {@code java.util.PropertyResourceBundle.encoding} is set to either
159      * "ISO-8859-1" or "UTF-8", the input stream is solely read in that encoding,
160      * and throws the exception if it encounters an invalid sequence. Other
161      * encoding values are ignored for this system property.
162      * The system property is read and evaluated when initializing this class.
163      * Changing or removing the property has no effect after the initialization.
164      *
165      * @param stream an InputStream that represents a property file
166      *        to read from.
167      * @throws IOException if an I/O error occurs
168      * @throws NullPointerException if {@code stream} is null
169      * @throws IllegalArgumentException if {@code stream} contains a
170      *     malformed Unicode escape sequence.
171      * @throws MalformedInputException if the system property
172      *     {@code java.util.PropertyResourceBundle.encoding} is set to "UTF-8"
173      *     and {@code stream} contains an invalid UTF-8 byte sequence.
174      * @throws UnmappableCharacterException if the system property
175      *     {@code java.util.PropertyResourceBundle.encoding} is set to "UTF-8"
176      *     and {@code stream} contains an unmappable UTF-8 byte sequence.
177      */
178     @SuppressWarnings({"unchecked", "rawtypes"})
PropertyResourceBundle(InputStream stream)179     public PropertyResourceBundle (InputStream stream) throws IOException {
180         // Android-changed: keep old behaviour which uses ISO-8859-1 by default.
181         // TODO(b/259671905): revert this patch once verified that it's safe.
182         /*
183         this(new InputStreamReader(stream,
184             "ISO-8859-1".equals(encoding) ?
185                 ISO_8859_1.INSTANCE.newDecoder() :
186                 new PropertyResourceBundleCharset("UTF-8".equals(encoding)).newDecoder()));
187         */
188         Properties properties = new Properties();
189         properties.load(stream);
190         lookup = new HashMap(properties);
191     }
192 
193     /**
194      * Creates a property resource bundle from a {@link java.io.Reader
195      * Reader}.  Unlike the constructor
196      * {@link #PropertyResourceBundle(java.io.InputStream) PropertyResourceBundle(InputStream)},
197      * there is no limitation as to the encoding of the input property file.
198      *
199      * @param reader a Reader that represents a property file to
200      *        read from.
201      * @throws IOException if an I/O error occurs
202      * @throws NullPointerException if {@code reader} is null
203      * @throws IllegalArgumentException if a malformed Unicode escape sequence appears
204      *     from {@code reader}.
205      * @since 1.6
206      */
207     @SuppressWarnings({"unchecked", "rawtypes"})
PropertyResourceBundle(Reader reader)208     public PropertyResourceBundle (Reader reader) throws IOException {
209         Properties properties = new Properties();
210         properties.load(reader);
211         lookup = new HashMap(properties);
212     }
213 
214     // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
handleGetObject(String key)215     public Object handleGetObject(String key) {
216         if (key == null) {
217             throw new NullPointerException();
218         }
219         return lookup.get(key);
220     }
221 
222     /**
223      * Returns an {@code Enumeration} of the keys contained in
224      * this {@code ResourceBundle} and its parent bundles.
225      *
226      * @return an {@code Enumeration} of the keys contained in
227      *         this {@code ResourceBundle} and its parent bundles.
228      * @see #keySet()
229      */
getKeys()230     public Enumeration<String> getKeys() {
231         ResourceBundle parent = this.parent;
232         return new ResourceBundleEnumeration(lookup.keySet(),
233                 (parent != null) ? parent.getKeys() : null);
234     }
235 
236     /**
237      * Returns a {@code Set} of the keys contained
238      * <em>only</em> in this {@code ResourceBundle}.
239      *
240      * @return a {@code Set} of the keys contained only in this
241      *         {@code ResourceBundle}
242      * @since 1.6
243      * @see #keySet()
244      */
handleKeySet()245     protected Set<String> handleKeySet() {
246         return lookup.keySet();
247     }
248 
249     // ==================privates====================
250     private final Map<String,Object> lookup;
251 }
252