UTF8Control.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.exception;

  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;
  25. import java.net.URL;
  26. import java.net.URLConnection;
  27. import java.util.Locale;
  28. import java.util.PropertyResourceBundle;
  29. import java.util.ResourceBundle;

  30. /** Control class loading properties in UTF-8 encoding.
  31.  * <p>
  32.  * This class has been very slightly adapted from BalusC answer to question: <a
  33.  * href="http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle">
  34.  * How to use UTF-8 in resource properties with ResourceBundle</a>.
  35.  * </p>
  36.  */
  37. public class UTF8Control extends ResourceBundle.Control {

  38.     /** Empty constructor.
  39.      * <p>
  40.      * This constructor is not strictly necessary, but it prevents spurious
  41.      * javadoc warnings with JDK 18 and later.
  42.      * </p>
  43.      * @since 3.0
  44.      */
  45.     public UTF8Control() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  46.         // nothing to do
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public ResourceBundle newBundle(final String baseName, final Locale locale, final String format,
  51.                                     final ClassLoader loader, final boolean reload)
  52.         throws IllegalAccessException, InstantiationException, IOException {
  53.         // The below is a copy of the default implementation.
  54.         final String bundleName = toBundleName(baseName, locale);
  55.         final String resourceName = toResourceName(bundleName, "utf8");
  56.         ResourceBundle bundle = null;
  57.         InputStream stream = null;
  58.         if (reload) {
  59.             final URL url = loader.getResource(resourceName);
  60.             if (url != null) {
  61.                 final URLConnection connection = url.openConnection();
  62.                 if (connection != null) {
  63.                     connection.setUseCaches(false);
  64.                     stream = connection.getInputStream();
  65.                 }
  66.             }
  67.         } else {
  68.             stream = loader.getResourceAsStream(resourceName);
  69.         }
  70.         if (stream != null) {
  71.             try { // NOPMD
  72.                 // Only this line is changed to make it to read properties files as UTF-8.
  73.                 bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
  74.             } finally {
  75.                 stream.close();
  76.             }
  77.         }
  78.         return bundle;
  79.     }

  80. }