Commit | Line | Data |
---|---|---|
2a96e7b2 | 1 | package be.nikiroo.jvcard.resources; |
668268fc NR |
2 | |
3 | import java.io.IOException; | |
4 | import java.io.InputStream; | |
5 | import java.io.InputStreamReader; | |
6 | import java.net.URL; | |
7 | import java.net.URLConnection; | |
8 | import java.util.Locale; | |
9 | import java.util.PropertyResourceBundle; | |
10 | import java.util.ResourceBundle; | |
11 | import java.util.ResourceBundle.Control; | |
12 | ||
2a96e7b2 NR |
13 | /** |
14 | * Fixed ResourceBundle.Control class. It will use UTF-8 for the files to load. | |
15 | * | |
16 | * @author niki | |
17 | * | |
18 | */ | |
19 | class FixedResourceBundleControl extends Control { | |
668268fc NR |
20 | public ResourceBundle newBundle(String baseName, Locale locale, |
21 | String format, ClassLoader loader, boolean reload) | |
22 | throws IllegalAccessException, InstantiationException, IOException { | |
23 | // The below is a copy of the default implementation. | |
24 | String bundleName = toBundleName(baseName, locale); | |
25 | String resourceName = toResourceName(bundleName, "properties"); | |
26 | ResourceBundle bundle = null; | |
27 | InputStream stream = null; | |
28 | if (reload) { | |
29 | URL url = loader.getResource(resourceName); | |
30 | if (url != null) { | |
31 | URLConnection connection = url.openConnection(); | |
32 | if (connection != null) { | |
33 | connection.setUseCaches(false); | |
34 | stream = connection.getInputStream(); | |
35 | } | |
36 | } | |
37 | } else { | |
38 | stream = loader.getResourceAsStream(resourceName); | |
39 | } | |
40 | if (stream != null) { | |
41 | try { | |
42 | // Only this line is changed to make it to read properties files | |
43 | // as UTF-8. | |
44 | // How can someone use an archaic encoding such as ISO 8859-1 by | |
45 | // *DEFAULT* is beyond me... | |
46 | bundle = new PropertyResourceBundle(new InputStreamReader( | |
47 | stream, "UTF-8")); | |
48 | } finally { | |
49 | stream.close(); | |
50 | } | |
51 | } | |
52 | return bundle; | |
53 | } | |
54 | } |