Switch all usages of file reader/writers to use java nio methods - closes #204
This commit is contained in:
parent
0b6f326c18
commit
838fba9173
@ -40,9 +40,10 @@ import me.lucko.luckperms.common.utils.ProgressLogger;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
@ -83,6 +84,8 @@ public class ExportCommand extends SingleCommand {
|
||||
return CommandResult.INVALID_ARGS;
|
||||
}
|
||||
|
||||
Path path = f.toPath();
|
||||
|
||||
try {
|
||||
f.createNewFile();
|
||||
} catch (IOException e) {
|
||||
@ -91,12 +94,12 @@ public class ExportCommand extends SingleCommand {
|
||||
return CommandResult.FAILURE;
|
||||
}
|
||||
|
||||
if (!Files.isWritable(f.toPath())) {
|
||||
if (!Files.isWritable(path)) {
|
||||
Message.LOG_EXPORT_NOT_WRITABLE.send(sender, f.getAbsolutePath());
|
||||
return CommandResult.FAILURE;
|
||||
}
|
||||
|
||||
try (FileWriter fWriter = new FileWriter(f, true); BufferedWriter writer = new BufferedWriter(fWriter)) {
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
|
||||
log.log("Starting.");
|
||||
|
||||
write(writer, "# LuckPerms Export File");
|
||||
@ -194,11 +197,7 @@ public class ExportCommand extends SingleCommand {
|
||||
}
|
||||
log.log("Exported " + userCount.get() + " users.");
|
||||
|
||||
try {
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
writer.flush();
|
||||
|
||||
log.getListeners().forEach(l -> Message.LOG_EXPORT_SUCCESS.send(l, f.getAbsolutePath()));
|
||||
return CommandResult.SUCCESS;
|
||||
|
@ -34,8 +34,9 @@ import me.lucko.luckperms.common.utils.Predicates;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
public class ImportCommand extends SingleCommand {
|
||||
@ -62,7 +63,9 @@ public class ImportCommand extends SingleCommand {
|
||||
return CommandResult.INVALID_ARGS;
|
||||
}
|
||||
|
||||
if (!Files.isReadable(f.toPath())) {
|
||||
Path path = f.toPath();
|
||||
|
||||
if (!Files.isReadable(path)) {
|
||||
Message.IMPORT_LOG_NOT_READABLE.send(sender, f.getAbsolutePath());
|
||||
return CommandResult.FAILURE;
|
||||
}
|
||||
@ -70,7 +73,7 @@ public class ImportCommand extends SingleCommand {
|
||||
List<String> commands;
|
||||
|
||||
try {
|
||||
commands = Files.readAllLines(f.toPath(), Charset.defaultCharset());
|
||||
commands = Files.readAllLines(path, StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Message.IMPORT_LOG_FAILURE.send(sender);
|
||||
|
@ -70,7 +70,7 @@ public class Util {
|
||||
ListIterator<String> iterator = input.listIterator();
|
||||
while (iterator.hasNext()) {
|
||||
String value = iterator.next();
|
||||
if (!(value.length() >= 3)) {
|
||||
if (value.length() < 3) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -224,7 +224,7 @@ public class Util {
|
||||
new FancyMessage("Click to remove this node from " + holderName).color(ChatColor.getByChar('7'))
|
||||
);
|
||||
|
||||
String command = NodeFactory.nodeAsCommand(node, group ? holderName : holderName, group)
|
||||
String command = NodeFactory.nodeAsCommand(node, holderName, group)
|
||||
.replace("/luckperms", "/" + label)
|
||||
.replace("permission set", "permission unset")
|
||||
.replace("parent add", "parent remove")
|
||||
@ -250,7 +250,7 @@ public class Util {
|
||||
int index = pageNumber - 1;
|
||||
List<List<Node>> pages = divideList(l, 15);
|
||||
|
||||
if ((index < 0 || index >= pages.size())) {
|
||||
if (index < 0 || index >= pages.size()) {
|
||||
pageNumber = 1;
|
||||
index = 0;
|
||||
}
|
||||
@ -460,7 +460,7 @@ public class Util {
|
||||
return sb.delete(sb.length() - 6, sb.length()).toString();
|
||||
}
|
||||
|
||||
public class MetaComparator implements Comparator<Map.Entry<Integer, ? extends Node>> {
|
||||
public static class MetaComparator implements Comparator<Map.Entry<Integer, ? extends Node>> {
|
||||
|
||||
@Override
|
||||
public int compare(Map.Entry<Integer, ? extends Node> o1, Map.Entry<Integer, ? extends Node> o2) {
|
||||
|
@ -30,9 +30,9 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -140,15 +140,11 @@ abstract class FlatfileBacking extends AbstractBacking {
|
||||
private Map<String, String> getUUIDCache() {
|
||||
Map<String, String> cache = new HashMap<>();
|
||||
|
||||
try {
|
||||
try (FileReader fileReader = new FileReader(uuidData)) {
|
||||
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
|
||||
Properties props = new Properties();
|
||||
props.load(bufferedReader);
|
||||
for (String key : props.stringPropertyNames()) {
|
||||
cache.put(key, props.getProperty(key));
|
||||
}
|
||||
}
|
||||
try (BufferedReader reader = Files.newBufferedReader(uuidData.toPath(), StandardCharsets.UTF_8)) {
|
||||
Properties props = new Properties();
|
||||
props.load(reader);
|
||||
for (String key : props.stringPropertyNames()) {
|
||||
cache.put(key, props.getProperty(key));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@ -157,15 +153,11 @@ abstract class FlatfileBacking extends AbstractBacking {
|
||||
}
|
||||
|
||||
private void saveUUIDCache(Map<String, String> cache) {
|
||||
try {
|
||||
try (FileWriter fileWriter = new FileWriter(uuidData)) {
|
||||
try (BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
|
||||
Properties properties = new Properties();
|
||||
properties.putAll(cache);
|
||||
properties.store(bufferedWriter, null);
|
||||
}
|
||||
}
|
||||
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(uuidData.toPath(), StandardCharsets.UTF_8)) {
|
||||
Properties properties = new Properties();
|
||||
properties.putAll(cache);
|
||||
properties.store(writer, null);
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -43,12 +43,9 @@ import me.lucko.luckperms.common.utils.ThrowingFunction;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
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.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
@ -78,17 +75,11 @@ public class JSONBacking extends FlatfileBacking {
|
||||
|
||||
private boolean fileToWriter(File file, ThrowingFunction<JsonWriter, Boolean> writeOperation) {
|
||||
boolean success = false;
|
||||
try {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
|
||||
try (JsonWriter jsonWriter = new JsonWriter(writer)) {
|
||||
jsonWriter.setIndent(" "); // 4 spaces
|
||||
success = writeOperation.apply(jsonWriter);
|
||||
jsonWriter.flush();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLog().warn("Exception whilst writing to file: " + file.getAbsolutePath());
|
||||
@ -99,15 +90,9 @@ public class JSONBacking extends FlatfileBacking {
|
||||
|
||||
private boolean fileToReader(File file, ThrowingFunction<JsonReader, 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)) {
|
||||
try (JsonReader jsonReader = new JsonReader(bufferedReader)) {
|
||||
success = readOperation.apply(jsonReader);
|
||||
}
|
||||
}
|
||||
}
|
||||
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
||||
try (JsonReader jsonReader = new JsonReader(reader)) {
|
||||
success = readOperation.apply(jsonReader);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLog().warn("Exception whilst reading from file: " + file.getAbsolutePath());
|
||||
|
@ -43,12 +43,9 @@ import org.yaml.snakeyaml.Yaml;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
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.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -84,16 +81,10 @@ public class YAMLBacking extends FlatfileBacking {
|
||||
}
|
||||
|
||||
private boolean writeMapToFile(File file, Map<String, Object> values) {
|
||||
try {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
|
||||
getYaml().dump(values, writer);
|
||||
writer.flush();
|
||||
return true;
|
||||
} catch (Throwable t) {
|
||||
plugin.getLog().warn("Exception whilst writing to file: " + file.getAbsolutePath());
|
||||
t.printStackTrace();
|
||||
@ -103,14 +94,8 @@ 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
||||
success = readOperation.apply((Map<String, Object>) getYaml().load(reader));
|
||||
} catch (Throwable t) {
|
||||
plugin.getLog().warn("Exception whilst reading from file: " + file.getAbsolutePath());
|
||||
t.printStackTrace();
|
||||
|
@ -22,7 +22,6 @@
|
||||
|
||||
package me.lucko.luckperms.common.utils;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
@ -62,7 +61,7 @@ public class PasteUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
JsonObject response = new Gson().fromJson(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8), JsonObject.class);
|
||||
JsonObject response = new Gson().fromJson(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8), JsonObject.class);
|
||||
String pasteUrl = response.get("html_url").getAsString();
|
||||
connection.disconnect();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user