diff options
author | Matt <mattbdev@outlook.com> | 2016-03-01 08:30:03 +1100 |
---|---|---|
committer | md_5 <git@md-5.net> | 2016-03-01 08:30:03 +1100 |
commit | a4971ffe76bc5059a2dd6705ea404e51b9721a12 (patch) | |
tree | 538d9bc73610809d7bf127d868fcb0e5150216a5 | |
parent | 09141842cdf4c38d2dbd42e69cd72f01c270d853 (diff) | |
download | bukkit-a4971ffe76bc5059a2dd6705ea404e51b9721a12.tar bukkit-a4971ffe76bc5059a2dd6705ea404e51b9721a12.tar.gz bukkit-a4971ffe76bc5059a2dd6705ea404e51b9721a12.tar.lz bukkit-a4971ffe76bc5059a2dd6705ea404e51b9721a12.tar.xz bukkit-a4971ffe76bc5059a2dd6705ea404e51b9721a12.zip |
Use UTF-8 as default encoding
Since March 2014 Bukkit has been converting and preparing plugins for UTF-8 compliance. With this change in place, all plugins will now load their configuration files as UTF-8, which is a supported encoding on any valid JVM implementation. This should pose no issues on any server which has run Bukkit after 2014, and the only possible breaking path is when an embedded file is of a non UTF-8 compatible encoding (which it should not be anyway).
4 files changed, 10 insertions, 77 deletions
diff --git a/src/main/java/org/bukkit/configuration/file/FileConfiguration.java b/src/main/java/org/bukkit/configuration/file/FileConfiguration.java index 9cbe0cad..a037d3e4 100644 --- a/src/main/java/org/bukkit/configuration/file/FileConfiguration.java +++ b/src/main/java/org/bukkit/configuration/file/FileConfiguration.java @@ -21,47 +21,12 @@ import java.nio.charset.Charset; import org.bukkit.configuration.Configuration; import org.bukkit.configuration.MemoryConfiguration; -import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; /** * This is a base class for all File based implementations of {@link * Configuration} */ public abstract class FileConfiguration extends MemoryConfiguration { - /** - * This value specified that the system default encoding should be - * completely ignored, as it cannot handle the ASCII character set, or it - * is a strict-subset of UTF8 already (plain ASCII). - * - * @deprecated temporary compatibility measure - */ - @Deprecated - public static final boolean UTF8_OVERRIDE; - /** - * This value specifies if the system default encoding is unicode, but - * cannot parse standard ASCII. - * - * @deprecated temporary compatibility measure - */ - @Deprecated - public static final boolean UTF_BIG; - /** - * This value specifies if the system supports unicode. - * - * @deprecated temporary compatibility measure - */ - @Deprecated - public static final boolean SYSTEM_UTF; - static { - final byte[] testBytes = Base64Coder.decode("ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX4NCg=="); - final String testString = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\r\n"; - final Charset defaultCharset = Charset.defaultCharset(); - final String resultString = new String(testBytes, defaultCharset); - final boolean trueUTF = defaultCharset.name().contains("UTF"); - UTF8_OVERRIDE = !testString.equals(resultString) || defaultCharset.equals(Charset.forName("US-ASCII")); - SYSTEM_UTF = trueUTF || UTF8_OVERRIDE; - UTF_BIG = trueUTF && UTF8_OVERRIDE; - } /** * Creates an empty {@link FileConfiguration} with no default values. @@ -102,7 +67,7 @@ public abstract class FileConfiguration extends MemoryConfiguration { String data = saveToString(); - Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF8_OVERRIDE && !UTF_BIG ? Charsets.UTF_8 : Charset.defaultCharset()); + Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8); try { writer.write(data); @@ -166,7 +131,7 @@ public abstract class FileConfiguration extends MemoryConfiguration { final FileInputStream stream = new FileInputStream(file); - load(new InputStreamReader(stream, UTF8_OVERRIDE && !UTF_BIG ? Charsets.UTF_8 : Charset.defaultCharset())); + load(new InputStreamReader(stream, Charsets.UTF_8)); } /** @@ -191,7 +156,7 @@ public abstract class FileConfiguration extends MemoryConfiguration { public void load(InputStream stream) throws IOException, InvalidConfigurationException { Validate.notNull(stream, "Stream cannot be null"); - load(new InputStreamReader(stream, UTF8_OVERRIDE ? Charsets.UTF_8 : Charset.defaultCharset())); + load(new InputStreamReader(stream, Charsets.UTF_8)); } /** diff --git a/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java index ea4c2b35..c7f28084 100644 --- a/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java +++ b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java @@ -33,7 +33,6 @@ public class YamlConfiguration extends FileConfiguration { public String saveToString() { yamlOptions.setIndent(options().indent()); yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - yamlOptions.setAllowUnicode(SYSTEM_UTF); yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); String header = buildHeader(); diff --git a/src/main/java/org/bukkit/plugin/PluginAwareness.java b/src/main/java/org/bukkit/plugin/PluginAwareness.java index ddb47b7e..3f535ed5 100644 --- a/src/main/java/org/bukkit/plugin/PluginAwareness.java +++ b/src/main/java/org/bukkit/plugin/PluginAwareness.java @@ -2,8 +2,6 @@ package org.bukkit.plugin; import java.util.Set; -import org.bukkit.plugin.java.JavaPlugin; - /** * Represents a concept that a plugin is aware of. * <p> @@ -21,8 +19,9 @@ public interface PluginAwareness { * This specifies that all (text) resources stored in a plugin's jar * use UTF-8 encoding. * - * @see JavaPlugin#getTextResource(String) + * @deprecated all plugins are now assumed to be UTF-8 aware. */ + @Deprecated UTF8, ; } diff --git a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java index c961b18c..98975d7a 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java @@ -165,10 +165,9 @@ public abstract class JavaPlugin extends PluginBase { } /** - * Provides a reader for a text file located inside the jar. The behavior - * of this method adheres to {@link PluginAwareness.Flags#UTF8}, or if not - * defined, uses UTF8 if {@link FileConfiguration#UTF8_OVERRIDE} is - * specified, or system default otherwise. + * Provides a reader for a text file located inside the jar. + * <p> + * The returned reader will read text with the UTF-8 charset. * * @param file the filename of the resource to load * @return null if {@link #getResource(String)} returns null @@ -179,7 +178,7 @@ public abstract class JavaPlugin extends PluginBase { protected final Reader getTextResource(String file) { final InputStream in = getResource(file); - return in == null ? null : new InputStreamReader(in, isStrictlyUTF8() || FileConfiguration.UTF8_OVERRIDE ? Charsets.UTF_8 : Charset.defaultCharset()); + return in == null ? null : new InputStreamReader(in, Charsets.UTF_8); } @SuppressWarnings("deprecation") @@ -192,36 +191,7 @@ public abstract class JavaPlugin extends PluginBase { return; } - final YamlConfiguration defConfig; - if (isStrictlyUTF8() || FileConfiguration.UTF8_OVERRIDE) { - defConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream, Charsets.UTF_8)); - } else { - final byte[] contents; - defConfig = new YamlConfiguration(); - try { - contents = ByteStreams.toByteArray(defConfigStream); - } catch (final IOException e) { - getLogger().log(Level.SEVERE, "Unexpected failure reading config.yml", e); - return; - } - - final String text = new String(contents, Charset.defaultCharset()); - if (!text.equals(new String(contents, Charsets.UTF_8))) { - getLogger().warning("Default system encoding may have misread config.yml from plugin jar"); - } - - try { - defConfig.loadFromString(text); - } catch (final InvalidConfigurationException e) { - getLogger().log(Level.SEVERE, "Cannot load configuration from jar", e); - } - } - - newConfig.setDefaults(defConfig); - } - - private boolean isStrictlyUTF8() { - return getDescription().getAwareness().contains(PluginAwareness.Flags.UTF8); + newConfig.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream, Charsets.UTF_8))); } @Override |