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 <mattbdev@outlook.com>
This commit is contained in:
Bukkit/Spigot
2016-03-01 08:30:03 +11:00
parent 6e5cf52206
commit 9cd422d56a
4 changed files with 10 additions and 77 deletions

View File

@@ -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));
}
/**

View File

@@ -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();