From 339a4d6f6ce649c354c2974119ebb18c7cf95746 Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Sun, 8 Jan 2023 12:46:42 +0100 Subject: [PATCH] Ensures usage of UTF-8 in SongodaYamlConfig Depending on the environment the default system charset might not be UTF-8 beaking messages files using non-english language etc. I'm not sure but Spigot might even set it to ASCII? The tests succeed locally because I am using Manjaro Linux which uses UTF-8 by default in the JVM. But testing a plugin and logging the default charset returns ASCII instead (on the same machine). --- .../core/configuration/songoda/SongodaYamlConfig.java | 8 ++++---- .../com/songoda/core/locale/LocaleFileManager.java | 3 ++- .../songoda/SongodaYamlConfigRoundtripTest.java | 11 ++++++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Core/src/main/java/com/songoda/core/configuration/songoda/SongodaYamlConfig.java b/Core/src/main/java/com/songoda/core/configuration/songoda/SongodaYamlConfig.java index 40cca8bb..0cd29eb5 100644 --- a/Core/src/main/java/com/songoda/core/configuration/songoda/SongodaYamlConfig.java +++ b/Core/src/main/java/com/songoda/core/configuration/songoda/SongodaYamlConfig.java @@ -11,11 +11,10 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -131,7 +130,7 @@ public class SongodaYamlConfig extends YamlConfiguration { } public void load() throws IOException { - try (Reader reader = new FileReader(this.file)) { + try (Reader reader = Files.newBufferedReader(this.file.toPath(), StandardCharsets.UTF_8)) { load(reader); } catch (FileNotFoundException ignore) { } catch (IOException ex) { @@ -142,7 +141,8 @@ public class SongodaYamlConfig extends YamlConfiguration { public void save() throws IOException { Files.createDirectories(this.file.toPath().getParent()); - try (Writer writer = new FileWriter(this.file)) { + + try (Writer writer = Files.newBufferedWriter(this.file.toPath(), StandardCharsets.UTF_8)) { super.save(writer); } catch (IOException ex) { throw new IOException("Unable to save '" + this.file.getPath() + "'", ex); diff --git a/Core/src/main/java/com/songoda/core/locale/LocaleFileManager.java b/Core/src/main/java/com/songoda/core/locale/LocaleFileManager.java index f39368c7..a9c7cd1a 100644 --- a/Core/src/main/java/com/songoda/core/locale/LocaleFileManager.java +++ b/Core/src/main/java/com/songoda/core/locale/LocaleFileManager.java @@ -11,6 +11,7 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Collections; import java.util.LinkedList; @@ -45,7 +46,7 @@ public class LocaleFileManager { throw new IOException("Failed to download language file " + languageFileName); // TODO: Better exception } - try (Writer writer = new FileWriter(languageFile)) { + try (Writer writer = Files.newBufferedWriter(languageFile.toPath(), StandardCharsets.UTF_8)) { writer.write(languageFileContents); } diff --git a/Core/src/test/java/com/songoda/core/configuration/songoda/SongodaYamlConfigRoundtripTest.java b/Core/src/test/java/com/songoda/core/configuration/songoda/SongodaYamlConfigRoundtripTest.java index bd2e980b..aa41c5c7 100644 --- a/Core/src/test/java/com/songoda/core/configuration/songoda/SongodaYamlConfigRoundtripTest.java +++ b/Core/src/test/java/com/songoda/core/configuration/songoda/SongodaYamlConfigRoundtripTest.java @@ -73,6 +73,10 @@ class SongodaYamlConfigRoundtripTest { .withDefaultValue("Default value") .withComment("This is the entry without an upgrade step"); + ConfigEntry entryWithCyrillic = cfg.createEntry("entryWithCyrillic") + .withDefaultValue("Кириллица") + .withComment("This is the entry with cyrillic characters"); + assertTrue(cfg.init()); assertNull(cfg.get("messages.fooSuccess")); @@ -89,6 +93,9 @@ class SongodaYamlConfigRoundtripTest { assertTrue(entryWithoutUpgradeStep.has()); assertEquals(cfg.get("entryWithoutUpgradeStep"), entryWithoutUpgradeStep.get()); + assertTrue(entryWithCyrillic.has()); + assertEquals(cfg.get("entryWithCyrillic"), entryWithCyrillic.get()); + assertEquals("# Don't touch this – it's used to track the version of the config.\n" + "version: 3\n" + "command:\n" + @@ -100,6 +107,8 @@ class SongodaYamlConfigRoundtripTest { "# This is the incrementer of the 'foo' command\n" + "incrementer: 0\n" + "# This is the entry without an upgrade step\n" + - "entryWithoutUpgradeStep: Default value\n", new String(Files.readAllBytes(testFilePath))); + "entryWithoutUpgradeStep: Default value\n" + + "# This is the entry with cyrillic characters\n" + + "entryWithCyrillic: Кириллица\n", new String(Files.readAllBytes(testFilePath))); } }