Fix reading and writing UTF-8 characters from storage files - closes #129

This commit is contained in:
Luck 2017-01-15 12:17:54 +00:00
parent f1a1612f5d
commit 7d24e748b7
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 48 additions and 33 deletions

View File

@ -43,9 +43,12 @@ import me.lucko.luckperms.common.utils.ThrowingFunction;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -76,8 +79,9 @@ public class JSONBacking extends FlatfileBacking {
private boolean fileToWriter(File file, ThrowingFunction<JsonWriter, Boolean> writeOperation) { private boolean fileToWriter(File file, ThrowingFunction<JsonWriter, Boolean> writeOperation) {
boolean success = false; boolean success = false;
try { try {
try (FileWriter fileWriter = new FileWriter(file)) { try (FileOutputStream outputStream = new FileOutputStream(file)) {
try (BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) { try (OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
try (BufferedWriter bufferedWriter = new BufferedWriter(outputWriter)) {
try (JsonWriter jsonWriter = new JsonWriter(bufferedWriter)) { try (JsonWriter jsonWriter = new JsonWriter(bufferedWriter)) {
jsonWriter.setIndent(" "); jsonWriter.setIndent(" ");
success = writeOperation.apply(jsonWriter); success = writeOperation.apply(jsonWriter);
@ -85,6 +89,7 @@ public class JSONBacking extends FlatfileBacking {
} }
} }
} }
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -94,13 +99,15 @@ public class JSONBacking extends FlatfileBacking {
private boolean fileToReader(File file, ThrowingFunction<JsonReader, Boolean> readOperation) { private boolean fileToReader(File file, ThrowingFunction<JsonReader, Boolean> readOperation) {
boolean success = false; boolean success = false;
try { try {
try (FileReader fileReader = new FileReader(file)) { try (FileInputStream fileInput = new FileInputStream(file)) {
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) { try (InputStreamReader inputReader = new InputStreamReader(fileInput, StandardCharsets.UTF_8)) {
try (BufferedReader bufferedReader = new BufferedReader(inputReader)) {
try (JsonReader jsonReader = new JsonReader(bufferedReader)) { try (JsonReader jsonReader = new JsonReader(bufferedReader)) {
success = readOperation.apply(jsonReader); success = readOperation.apply(jsonReader);
} }
} }
} }
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -43,9 +43,12 @@ import org.yaml.snakeyaml.Yaml;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -62,6 +65,7 @@ import static me.lucko.luckperms.common.core.model.PermissionHolder.exportToLega
public class YAMLBacking extends FlatfileBacking { public class YAMLBacking extends FlatfileBacking {
private static Yaml getYaml() { private static Yaml getYaml() {
DumperOptions options = new DumperOptions(); DumperOptions options = new DumperOptions();
options.setAllowUnicode(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
return new Yaml(options); return new Yaml(options);
} }
@ -79,35 +83,39 @@ public class YAMLBacking extends FlatfileBacking {
super(plugin, "YAML", pluginDir); super(plugin, "YAML", pluginDir);
} }
private boolean readMapFromFile(File file, Function<Map<String, Object>, Boolean> readOperation) {
boolean success = false;
try {
try (FileReader fileReader = new FileReader(file)) {
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
success = readOperation.apply((Map<String, Object>) getYaml().load(bufferedReader));
}
}
} catch (Throwable t) {
t.printStackTrace();
}
return success;
}
private boolean writeMapToFile(File file, Map<String, Object> values) { private boolean writeMapToFile(File file, Map<String, Object> values) {
try { try {
try (FileWriter fileWriter = new FileWriter(file)) { try (FileOutputStream outputStream = new FileOutputStream(file)) {
try (BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) { try (OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
try (BufferedWriter bufferedWriter = new BufferedWriter(outputWriter)) {
getYaml().dump(values, bufferedWriter); getYaml().dump(values, bufferedWriter);
bufferedWriter.flush(); bufferedWriter.flush();
return true; return true;
} }
} }
}
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
return false; return false;
} }
} }
private boolean readMapFromFile(File file, Function<Map<String, Object>, Boolean> readOperation) {
boolean success = false;
try {
try (FileInputStream fileInput = new FileInputStream(file)) {
try (InputStreamReader inputReader = new InputStreamReader(fileInput, StandardCharsets.UTF_8)) {
try (BufferedReader bufferedReader = new BufferedReader(inputReader)) {
success = readOperation.apply((Map<String, Object>) getYaml().load(bufferedReader));
}
}
}
} catch (Throwable t) {
t.printStackTrace();
}
return success;
}
@Override @Override
public boolean loadUser(UUID uuid, String username) { public boolean loadUser(UUID uuid, String username) {
User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username)); User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));