View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.exception;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.InputStreamReader;
27  import java.net.URL;
28  import java.net.URLConnection;
29  import java.util.Locale;
30  import java.util.PropertyResourceBundle;
31  import java.util.ResourceBundle;
32  
33  /** Control class loading properties in UTF-8 encoding.
34   * <p>
35   * This class has been very slightly adapted from BalusC answer to question: <a
36   * href="http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle">
37   * How to use UTF-8 in resource properties with ResourceBundle</a>.
38   * </p>
39   */
40  public class UTF8Control extends ResourceBundle.Control {
41  
42      /** Empty constructor.
43       * <p>
44       * This constructor is not strictly necessary, but it prevents spurious
45       * javadoc warnings with JDK 18 and later.
46       * </p>
47       * @since 3.0
48       */
49      public UTF8Control() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
50          // nothing to do
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public ResourceBundle newBundle(final String baseName, final Locale locale, final String format,
56                                      final ClassLoader loader, final boolean reload)
57          throws IllegalAccessException, InstantiationException, IOException {
58          // The below is a copy of the default implementation.
59          final String bundleName = toBundleName(baseName, locale);
60          final String resourceName = toResourceName(bundleName, "utf8");
61          ResourceBundle bundle = null;
62          InputStream stream = null;
63          if (reload) {
64              final URL url = loader.getResource(resourceName);
65              if (url != null) {
66                  final URLConnection connection = url.openConnection();
67                  if (connection != null) {
68                      connection.setUseCaches(false);
69                      stream = connection.getInputStream();
70                  }
71              }
72          } else {
73              stream = loader.getResourceAsStream(resourceName);
74          }
75          if (stream != null) {
76              try { // NOPMD
77                  // Only this line is changed to make it to read properties files as UTF-8.
78                  bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
79              } finally {
80                  stream.close();
81              }
82          }
83          return bundle;
84      }
85  
86  }