From 9cd422d56a1489cb5cfd138f7749df0f84a02ce5 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Tue, 1 Mar 2016 08:30:03 +1100 Subject: [PATCH] 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). By: Matt --- .../configuration/file/FileConfiguration.java | 41 ++----------------- .../configuration/file/YamlConfiguration.java | 1 - .../org/bukkit/plugin/PluginAwareness.java | 5 +-- .../org/bukkit/plugin/java/JavaPlugin.java | 40 +++--------------- 4 files changed, 10 insertions(+), 77 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/configuration/file/FileConfiguration.java b/paper-api/src/main/java/org/bukkit/configuration/file/FileConfiguration.java index 9cbe0cadf7..a037d3e45e 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/file/FileConfiguration.java +++ b/paper-api/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/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java b/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java index ea4c2b3540..c7f2808465 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java +++ b/paper-api/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/paper-api/src/main/java/org/bukkit/plugin/PluginAwareness.java b/paper-api/src/main/java/org/bukkit/plugin/PluginAwareness.java index ddb47b7e6a..3f535ed5d4 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/PluginAwareness.java +++ b/paper-api/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. *

@@ -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/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java index c961b18c03..98975d7a02 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPlugin.java +++ b/paper-api/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. + *

+ * 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