forked from SKCraft/Launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for UTF-8 encoded locale files
Fixes SKCraft#467
- Loading branch information
1 parent
39487dd
commit 23e01de
Showing
2 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
launcher/src/main/java/com/skcraft/launcher/util/LocaleEncodingControl.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.skcraft.launcher.util; | ||
|
||
import lombok.extern.java.Log; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Locale; | ||
import java.util.PropertyResourceBundle; | ||
import java.util.ResourceBundle; | ||
|
||
@Log | ||
public class LocaleEncodingControl extends ResourceBundle.Control { | ||
@Override | ||
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException { | ||
if (!format.equals("java.properties")) { | ||
return super.newBundle(baseName, locale, format, loader, reload); | ||
} | ||
|
||
String bundleName = this.toBundleName(baseName, locale); | ||
String resourceName = this.toResourceName(bundleName, "properties"); | ||
|
||
InputStream is = this.getResourceAsStream(resourceName, loader, reload); | ||
if (is == null) { | ||
return null; | ||
} | ||
|
||
// Let's do the timewalk | ||
boolean isUtf8; | ||
is.mark(3); | ||
{ | ||
byte[] buf = new byte[3]; | ||
int read = 0; | ||
while (read < 3) { | ||
read = is.read(buf); | ||
} | ||
|
||
// the BOM is 0xEF,0xBB,0xBF | ||
isUtf8 = buf[0] == (byte) 0xEF && buf[1] == (byte) 0xBB && buf[2] == (byte) 0xBF; | ||
} | ||
is.reset(); | ||
|
||
if (isUtf8) { | ||
log.info("Found UTF-8 locale file " + resourceName); | ||
} | ||
|
||
Charset charset = isUtf8 ? StandardCharsets.UTF_8 : StandardCharsets.ISO_8859_1; | ||
Reader reader = new InputStreamReader(is, charset); | ||
|
||
try { | ||
PropertyResourceBundle bundle = new PropertyResourceBundle(reader); | ||
reader.close(); | ||
|
||
return bundle; | ||
} finally { | ||
// Just in case of exception... | ||
reader.close(); | ||
} | ||
} | ||
|
||
private InputStream getResourceAsStream(String resourceName, ClassLoader loader, boolean reload) throws IOException { | ||
if (reload) { | ||
URL url = loader.getResource(resourceName); | ||
if (url != null) { | ||
URLConnection conn = url.openConnection(); | ||
if (conn != null) { | ||
conn.setUseCaches(false); | ||
|
||
return conn.getInputStream(); | ||
} | ||
} | ||
} | ||
|
||
return loader.getResourceAsStream(resourceName); | ||
} | ||
} |
This file contains 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