jdk/src/java.base/share/classes/java/util/PropertyResourceBundle.java
changeset 31902 1c90f9a5a76d
parent 25859 3317bb8137f4
child 37593 824750ada3d6
--- a/jdk/src/java.base/share/classes/java/util/PropertyResourceBundle.java	Wed Jul 29 11:47:19 2015 +0200
+++ b/jdk/src/java.base/share/classes/java/util/PropertyResourceBundle.java	Wed Jul 29 13:36:53 2015 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -40,8 +40,17 @@
 package java.util;
 
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.MalformedInputException;
+import java.nio.charset.StandardCharsets;
+import java.nio.charset.UnmappableCharacterException;
+import java.security.AccessController;
+import java.util.Locale;
+import sun.security.action.GetPropertyAction;
+import sun.util.PropertyResourceBundleCharset;
 import sun.util.ResourceBundleEnumeration;
 
 /**
@@ -108,11 +117,20 @@
  * <strong>Note:</strong> PropertyResourceBundle can be constructed either
  * from an InputStream or a Reader, which represents a property file.
  * Constructing a PropertyResourceBundle instance from an InputStream requires
- * that the input stream be encoded in ISO-8859-1.  In that case, characters
- * that cannot be represented in ISO-8859-1 encoding must be represented by Unicode Escapes
- * as defined in section 3.3 of
- * <cite>The Java&trade; Language Specification</cite>
+ * that the input stream be encoded in UTF-8. By default, if a
+ * {@link java.nio.charset.MalformedInputException} or an
+ * {@link java.nio.charset.UnmappableCharacterException} occurs on reading the
+ * input stream, then the PropertyResourceBundle instance resets to the state
+ * before the exception, re-reads the input stream in {@code ISO-8859-1}, and
+ * continues reading. If the system property
+ * {@code java.util.PropertyResourceBundle.encoding} is set to either
+ * "ISO-8859-1" or "UTF-8", the input stream is solely read in that encoding,
+ * and throws the exception if it encounters an invalid sequence.
+ * If "ISO-8859-1" is specified, characters that cannot be represented in
+ * ISO-8859-1 encoding must be represented by Unicode Escapes as defined in section
+ * 3.3 of <cite>The Java&trade; Language Specification</cite>
  * whereas the other constructor which takes a Reader does not have that limitation.
+ * Other encoding values are ignored for this system property.
  *
  * @see ResourceBundle
  * @see ListResourceBundle
@@ -120,10 +138,26 @@
  * @since 1.1
  */
 public class PropertyResourceBundle extends ResourceBundle {
+
+    // Check whether the strict encoding is specified.
+    // The possible encoding is either "ISO-8859-1" or "UTF-8".
+    private static final String encoding =
+        AccessController.doPrivileged(
+            new GetPropertyAction("java.util.PropertyResourceBundle.encoding", ""))
+        .toUpperCase(Locale.ROOT);
+
     /**
      * Creates a property resource bundle from an {@link java.io.InputStream
-     * InputStream}.  The property file read with this constructor
-     * must be encoded in ISO-8859-1.
+    * InputStream}. This constructor reads the property file in UTF-8 by default.
+    * If a {@link java.nio.charset.MalformedInputException} or an
+    * {@link java.nio.charset.UnmappableCharacterException} occurs on reading the
+    * input stream, then the PropertyResourceBundle instance resets to the state
+    * before the exception, re-reads the input stream in {@code ISO-8859-1} and
+    * continues reading. If the system property
+    * {@code java.util.PropertyResourceBundle.encoding} is set to either
+    * "ISO-8859-1" or "UTF-8", the input stream is solely read in that encoding,
+    * and throws the exception if it encounters an invalid sequence. Other
+    * encoding values are ignored for this system property.
      *
      * @param stream an InputStream that represents a property file
      *        to read from.
@@ -131,12 +165,19 @@
      * @throws NullPointerException if <code>stream</code> is null
      * @throws IllegalArgumentException if {@code stream} contains a
      *     malformed Unicode escape sequence.
+     * @throws MalformedInputException if the system property
+     *     {@code java.util.PropertyResourceBundle.encoding} is set to "UTF-8"
+     *     and {@code stream} contains an invalid UTF-8 byte sequence.
+     * @throws UnmappableCharacterException if the system property
+     *     {@code java.util.PropertyResourceBundle.encoding} is set to "UTF-8"
+     *     and {@code stream} contains an unmappable UTF-8 byte sequence.
      */
     @SuppressWarnings({"unchecked", "rawtypes"})
     public PropertyResourceBundle (InputStream stream) throws IOException {
-        Properties properties = new Properties();
-        properties.load(stream);
-        lookup = new HashMap(properties);
+        this(new InputStreamReader(stream,
+            "ISO-8859-1".equals(encoding) ?
+                StandardCharsets.ISO_8859_1.newDecoder() :
+                new PropertyResourceBundleCharset("UTF-8".equals(encoding)).newDecoder()));
     }
 
     /**