Switch all usages of file reader/writers to use java nio methods - closes #204

This commit is contained in:
Luck 2017-03-09 17:55:33 +00:00
parent 0b6f326c18
commit 838fba9173
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
7 changed files with 46 additions and 83 deletions

View File

@ -40,9 +40,10 @@ import me.lucko.luckperms.common.utils.ProgressLogger;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
@ -83,6 +84,8 @@ public class ExportCommand extends SingleCommand {
return CommandResult.INVALID_ARGS; return CommandResult.INVALID_ARGS;
} }
Path path = f.toPath();
try { try {
f.createNewFile(); f.createNewFile();
} catch (IOException e) { } catch (IOException e) {
@ -91,12 +94,12 @@ public class ExportCommand extends SingleCommand {
return CommandResult.FAILURE; return CommandResult.FAILURE;
} }
if (!Files.isWritable(f.toPath())) { if (!Files.isWritable(path)) {
Message.LOG_EXPORT_NOT_WRITABLE.send(sender, f.getAbsolutePath()); Message.LOG_EXPORT_NOT_WRITABLE.send(sender, f.getAbsolutePath());
return CommandResult.FAILURE; 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."); log.log("Starting.");
write(writer, "# LuckPerms Export File"); write(writer, "# LuckPerms Export File");
@ -194,11 +197,7 @@ public class ExportCommand extends SingleCommand {
} }
log.log("Exported " + userCount.get() + " users."); log.log("Exported " + userCount.get() + " users.");
try { writer.flush();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
log.getListeners().forEach(l -> Message.LOG_EXPORT_SUCCESS.send(l, f.getAbsolutePath())); log.getListeners().forEach(l -> Message.LOG_EXPORT_SUCCESS.send(l, f.getAbsolutePath()));
return CommandResult.SUCCESS; return CommandResult.SUCCESS;

View File

@ -34,8 +34,9 @@ import me.lucko.luckperms.common.utils.Predicates;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List; import java.util.List;
public class ImportCommand extends SingleCommand { public class ImportCommand extends SingleCommand {
@ -62,7 +63,9 @@ public class ImportCommand extends SingleCommand {
return CommandResult.INVALID_ARGS; 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()); Message.IMPORT_LOG_NOT_READABLE.send(sender, f.getAbsolutePath());
return CommandResult.FAILURE; return CommandResult.FAILURE;
} }
@ -70,7 +73,7 @@ public class ImportCommand extends SingleCommand {
List<String> commands; List<String> commands;
try { try {
commands = Files.readAllLines(f.toPath(), Charset.defaultCharset()); commands = Files.readAllLines(path, StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
Message.IMPORT_LOG_FAILURE.send(sender); Message.IMPORT_LOG_FAILURE.send(sender);

View File

@ -70,7 +70,7 @@ public class Util {
ListIterator<String> iterator = input.listIterator(); ListIterator<String> iterator = input.listIterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
String value = iterator.next(); String value = iterator.next();
if (!(value.length() >= 3)) { if (value.length() < 3) {
continue; continue;
} }
@ -224,7 +224,7 @@ public class Util {
new FancyMessage("Click to remove this node from " + holderName).color(ChatColor.getByChar('7')) 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("/luckperms", "/" + label)
.replace("permission set", "permission unset") .replace("permission set", "permission unset")
.replace("parent add", "parent remove") .replace("parent add", "parent remove")
@ -250,7 +250,7 @@ public class Util {
int index = pageNumber - 1; int index = pageNumber - 1;
List<List<Node>> pages = divideList(l, 15); List<List<Node>> pages = divideList(l, 15);
if ((index < 0 || index >= pages.size())) { if (index < 0 || index >= pages.size()) {
pageNumber = 1; pageNumber = 1;
index = 0; index = 0;
} }
@ -460,7 +460,7 @@ public class Util {
return sb.delete(sb.length() - 6, sb.length()).toString(); 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 @Override
public int compare(Map.Entry<Integer, ? extends Node> o1, Map.Entry<Integer, ? extends Node> o2) { public int compare(Map.Entry<Integer, ? extends Node> o1, Map.Entry<Integer, ? extends Node> o2) {

View File

@ -30,9 +30,9 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
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.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -140,15 +140,11 @@ abstract class FlatfileBacking extends AbstractBacking {
private Map<String, String> getUUIDCache() { private Map<String, String> getUUIDCache() {
Map<String, String> cache = new HashMap<>(); Map<String, String> cache = new HashMap<>();
try { try (BufferedReader reader = Files.newBufferedReader(uuidData.toPath(), StandardCharsets.UTF_8)) {
try (FileReader fileReader = new FileReader(uuidData)) { Properties props = new Properties();
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) { props.load(reader);
Properties props = new Properties(); for (String key : props.stringPropertyNames()) {
props.load(bufferedReader); cache.put(key, props.getProperty(key));
for (String key : props.stringPropertyNames()) {
cache.put(key, props.getProperty(key));
}
}
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -157,15 +153,11 @@ abstract class FlatfileBacking extends AbstractBacking {
} }
private void saveUUIDCache(Map<String, String> cache) { private void saveUUIDCache(Map<String, String> cache) {
try { try (BufferedWriter writer = Files.newBufferedWriter(uuidData.toPath(), StandardCharsets.UTF_8)) {
try (FileWriter fileWriter = new FileWriter(uuidData)) { Properties properties = new Properties();
try (BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) { properties.putAll(cache);
Properties properties = new Properties(); properties.store(writer, null);
properties.putAll(cache); writer.flush();
properties.store(bufferedWriter, null);
}
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -43,12 +43,9 @@ 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.FileInputStream;
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.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -78,17 +75,11 @@ 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 (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
try (FileOutputStream outputStream = new FileOutputStream(file)) { try (JsonWriter jsonWriter = new JsonWriter(writer)) {
try (OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { jsonWriter.setIndent(" "); // 4 spaces
try (BufferedWriter bufferedWriter = new BufferedWriter(outputWriter)) { success = writeOperation.apply(jsonWriter);
try (JsonWriter jsonWriter = new JsonWriter(bufferedWriter)) { jsonWriter.flush();
jsonWriter.setIndent(" ");
success = writeOperation.apply(jsonWriter);
jsonWriter.flush();
}
}
}
} }
} catch (Exception e) { } catch (Exception e) {
plugin.getLog().warn("Exception whilst writing to file: " + file.getAbsolutePath()); 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) { private boolean fileToReader(File file, ThrowingFunction<JsonReader, Boolean> readOperation) {
boolean success = false; boolean success = false;
try { try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
try (FileInputStream fileInput = new FileInputStream(file)) { try (JsonReader jsonReader = new JsonReader(reader)) {
try (InputStreamReader inputReader = new InputStreamReader(fileInput, StandardCharsets.UTF_8)) { success = readOperation.apply(jsonReader);
try (BufferedReader bufferedReader = new BufferedReader(inputReader)) {
try (JsonReader jsonReader = new JsonReader(bufferedReader)) {
success = readOperation.apply(jsonReader);
}
}
}
} }
} catch (Exception e) { } catch (Exception e) {
plugin.getLog().warn("Exception whilst reading from file: " + file.getAbsolutePath()); plugin.getLog().warn("Exception whilst reading from file: " + file.getAbsolutePath());

View File

@ -43,12 +43,9 @@ 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.FileInputStream;
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.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -84,16 +81,10 @@ public class YAMLBacking extends FlatfileBacking {
} }
private boolean writeMapToFile(File file, Map<String, Object> values) { private boolean writeMapToFile(File file, Map<String, Object> values) {
try { try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
try (FileOutputStream outputStream = new FileOutputStream(file)) { getYaml().dump(values, writer);
try (OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { writer.flush();
try (BufferedWriter bufferedWriter = new BufferedWriter(outputWriter)) { return true;
getYaml().dump(values, bufferedWriter);
bufferedWriter.flush();
return true;
}
}
}
} catch (Throwable t) { } catch (Throwable t) {
plugin.getLog().warn("Exception whilst writing to file: " + file.getAbsolutePath()); plugin.getLog().warn("Exception whilst writing to file: " + file.getAbsolutePath());
t.printStackTrace(); t.printStackTrace();
@ -103,14 +94,8 @@ public class YAMLBacking extends FlatfileBacking {
private boolean readMapFromFile(File file, Function<Map<String, Object>, Boolean> readOperation) { private boolean readMapFromFile(File file, Function<Map<String, Object>, Boolean> readOperation) {
boolean success = false; boolean success = false;
try { try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
try (FileInputStream fileInput = new FileInputStream(file)) { success = readOperation.apply((Map<String, Object>) getYaml().load(reader));
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) { } catch (Throwable t) {
plugin.getLog().warn("Exception whilst reading from file: " + file.getAbsolutePath()); plugin.getLog().warn("Exception whilst reading from file: " + file.getAbsolutePath());
t.printStackTrace(); t.printStackTrace();

View File

@ -22,7 +22,6 @@
package me.lucko.luckperms.common.utils; package me.lucko.luckperms.common.utils;
import com.google.common.base.Charsets;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
@ -62,7 +61,7 @@ public class PasteUtils {
return null; 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(); String pasteUrl = response.get("html_url").getAsString();
connection.disconnect(); connection.disconnect();