package votorola.g.util; import java.io.*; import java.net.*; import java.security.*; import java.util.*; import votorola.g.lang.*; /** A resource bundle control to enable loading of UTF-8 property files, as opposed the * ISO-8859-1 encoding expected by the default control. * * @see java.util.PropertyResourceBundle */ public @ThreadSafe final class BundleControlU extends ResourceBundle.Control { // as BalusC recommends, http://stackoverflow.com/a/4660195 public @Override ResourceBundle newBundle( final String baseName, final Locale locale, final String format, final ClassLoader loader, final boolean reload ) throws IllegalAccessException, InstantiationException, IOException { if( !"java.properties".equals( format )) { return super.newBundle( baseName, locale, format, loader, reload ); } final String bundleName = toBundleName( baseName, locale ); ResourceBundle bundle = null; // Modified from Oracle JDK 1.7.0_02 where marked "MCA", under /_/licence/none.txt // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - final String resourceName = toResourceName(bundleName, "properties"); final ClassLoader classLoader = loader; final boolean reloadFlag = reload; InputStream stream = null; try { stream = AccessController.doPrivileged( new PrivilegedExceptionAction() { public InputStream run() throws IOException { InputStream is = null; if (reloadFlag) { URL url = classLoader.getResource(resourceName); if (url != null) { URLConnection connection = url.openConnection(); if (connection != null) { // Disable caches to get fresh data for // reloading. connection.setUseCaches(false); is = connection.getInputStream(); } } } else { is = classLoader.getResourceAsStream(resourceName); } return is; } }); } catch (PrivilegedActionException e) { throw (IOException) e.getException(); } if (stream != null) { try { // bundle = new PropertyResourceBundle(stream); // MCA: bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8")); } finally { stream.close(); } } return bundle; } }