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.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -76,12 +79,14 @@ public class JSONBacking extends FlatfileBacking {
private boolean fileToWriter(File file, ThrowingFunction<JsonWriter, Boolean> writeOperation) {
boolean success = false;
try {
try (FileWriter fileWriter = new FileWriter(file)) {
try (BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
try (JsonWriter jsonWriter = new JsonWriter(bufferedWriter)) {
jsonWriter.setIndent(" ");
success = writeOperation.apply(jsonWriter);
jsonWriter.flush();
try (FileOutputStream outputStream = new FileOutputStream(file)) {
try (OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
try (BufferedWriter bufferedWriter = new BufferedWriter(outputWriter)) {
try (JsonWriter jsonWriter = new JsonWriter(bufferedWriter)) {
jsonWriter.setIndent(" ");
success = writeOperation.apply(jsonWriter);
jsonWriter.flush();
}
}
}
}
@ -94,10 +99,12 @@ public class JSONBacking extends FlatfileBacking {
private boolean fileToReader(File file, ThrowingFunction<JsonReader, Boolean> readOperation) {
boolean success = false;
try {
try (FileReader fileReader = new FileReader(file)) {
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
try (JsonReader jsonReader = new JsonReader(bufferedReader)) {
success = readOperation.apply(jsonReader);
try (FileInputStream fileInput = new FileInputStream(file)) {
try (InputStreamReader inputReader = new InputStreamReader(fileInput, StandardCharsets.UTF_8)) {
try (BufferedReader bufferedReader = new BufferedReader(inputReader)) {
try (JsonReader jsonReader = new JsonReader(bufferedReader)) {
success = readOperation.apply(jsonReader);
}
}
}
}

View File

@ -43,9 +43,12 @@ import org.yaml.snakeyaml.Yaml;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -62,6 +65,7 @@ import static me.lucko.luckperms.common.core.model.PermissionHolder.exportToLega
public class YAMLBacking extends FlatfileBacking {
private static Yaml getYaml() {
DumperOptions options = new DumperOptions();
options.setAllowUnicode(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
return new Yaml(options);
}
@ -79,27 +83,15 @@ public class YAMLBacking extends FlatfileBacking {
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) {
try {
try (FileWriter fileWriter = new FileWriter(file)) {
try (BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
getYaml().dump(values, bufferedWriter);
bufferedWriter.flush();
return true;
try (FileOutputStream outputStream = new FileOutputStream(file)) {
try (OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
try (BufferedWriter bufferedWriter = new BufferedWriter(outputWriter)) {
getYaml().dump(values, bufferedWriter);
bufferedWriter.flush();
return true;
}
}
}
} catch (Throwable t) {
@ -108,6 +100,22 @@ public class YAMLBacking extends FlatfileBacking {
}
}
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
public boolean loadUser(UUID uuid, String username) {
User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));