Created
August 17, 2010 18:39
-
-
Save ussy/531362 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.security.AccessController; | |
import java.security.PrivilegedActionException; | |
import java.security.PrivilegedExceptionAction; | |
import java.text.MessageFormat; | |
import java.util.Locale; | |
import java.util.PropertyResourceBundle; | |
import java.util.ResourceBundle; | |
import java.util.ResourceBundle.Control; | |
public class CharsetSupportedControl extends Control { | |
private String charset; | |
public CharsetSupportedControl() { | |
this("utf-8"); | |
} | |
public CharsetSupportedControl(String charset) { | |
this.charset = charset; | |
} | |
@Override | |
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) | |
throws IllegalAccessException, InstantiationException, IOException { | |
if (format.equals("java.class")) { | |
return super.newBundle(baseName, locale, format, loader, reload); | |
} else if (format.equals("java.properties")) { | |
String bundleName = toBundleName(baseName, locale); | |
final String resourceName = toResourceName(bundleName, "properties"); | |
final ClassLoader classLoader = loader; | |
final boolean reloadFlag = reload; | |
InputStream stream = null; | |
try { | |
stream = AccessController.doPrivileged( | |
new PrivilegedExceptionAction<InputStream>() { | |
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 { | |
BufferedReader br = new BufferedReader(new InputStreamReader(stream, charset)); | |
try { | |
return new PropertyResourceBundle(br); | |
} finally { | |
br.close(); | |
} | |
} finally { | |
stream.close(); | |
} | |
} | |
return null; | |
} else { | |
throw new IllegalArgumentException("unknown format: " + format); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
// message.properties をクラスパス直下に | |
ResourceBundle bundle = ResourceBundle.getBundle("messages", new CharsetSupportedControl()); | |
System.out.println(bundle.getString("message")); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment