From 838fba9173cb75980a4f64a22436d0689e222944 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 9 Mar 2017 17:55:33 +0000 Subject: [PATCH] Switch all usages of file reader/writers to use java nio methods - closes #204 --- .../common/commands/misc/ExportCommand.java | 15 ++++----- .../common/commands/misc/ImportCommand.java | 9 +++-- .../luckperms/common/commands/utils/Util.java | 8 ++--- .../storage/backing/FlatfileBacking.java | 32 +++++++----------- .../common/storage/backing/JSONBacking.java | 33 +++++-------------- .../common/storage/backing/YAMLBacking.java | 29 ++++------------ .../luckperms/common/utils/PasteUtils.java | 3 +- 7 files changed, 46 insertions(+), 83 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/misc/ExportCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/misc/ExportCommand.java index c4b5782b..3414ba25 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/misc/ExportCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/misc/ExportCommand.java @@ -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; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/misc/ImportCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/misc/ImportCommand.java index 9e1a53d0..d81d7b81 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/misc/ImportCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/misc/ImportCommand.java @@ -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 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); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/utils/Util.java b/common/src/main/java/me/lucko/luckperms/common/commands/utils/Util.java index 82a81c5c..30837738 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/utils/Util.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/utils/Util.java @@ -70,7 +70,7 @@ public class Util { ListIterator 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> 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> { + public static class MetaComparator implements Comparator> { @Override public int compare(Map.Entry o1, Map.Entry o2) { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/FlatfileBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/FlatfileBacking.java index 8e240a23..5c7ade22 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/FlatfileBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/FlatfileBacking.java @@ -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 getUUIDCache() { Map 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 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(); } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java index 0635bffd..02396db5 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/JSONBacking.java @@ -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 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 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()); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java index c1c73e29..65f1898d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/YAMLBacking.java @@ -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 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, 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) getYaml().load(bufferedReader)); - } - } - } + try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) { + success = readOperation.apply((Map) getYaml().load(reader)); } catch (Throwable t) { plugin.getLog().warn("Exception whilst reading from file: " + file.getAbsolutePath()); t.printStackTrace(); diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java b/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java index 9e8a150c..3baa5934 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/PasteUtils.java @@ -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();