Fixed resource not closing

This commit is contained in:
LOOHP 2021-02-20 18:12:11 +08:00
parent 8d2b7ea0cb
commit 6013235c73
6 changed files with 31 additions and 16 deletions

View File

@ -24,9 +24,11 @@ public class FileConfiguration {
private Map<String, Object> mapping; private Map<String, Object> mapping;
private String header; private String header;
public FileConfiguration(File file) throws FileNotFoundException { public FileConfiguration(File file) throws IOException {
if (file.exists()) { if (file.exists()) {
reloadConfig(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
reloadConfig(reader);
reader.close();
} else { } else {
mapping = new LinkedHashMap<>(); mapping = new LinkedHashMap<>();
} }
@ -113,7 +115,10 @@ public class FileConfiguration {
pw.flush(); pw.flush();
pw.close(); pw.close();
return writer.toString(); String str = writer.toString();
writer.close();
return str;
} }
public void saveConfig(File file) throws IOException { public void saveConfig(File file) throws IOException {

View File

@ -47,17 +47,23 @@ public class ServerProperties {
this.file = file; this.file = file;
Properties def = new Properties(); Properties def = new Properties();
def.load(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("server.properties"), StandardCharsets.UTF_8)); InputStreamReader defStream = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("server.properties"), StandardCharsets.UTF_8);
def.load(defStream);
defStream.close();
Properties prop = new Properties(); Properties prop = new Properties();
prop.load(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); InputStreamReader stream = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
prop.load(stream);
stream.close();
for (Entry<Object, Object> entry : def.entrySet()) { for (Entry<Object, Object> entry : def.entrySet()) {
String key = entry.getKey().toString(); String key = entry.getKey().toString();
String value = entry.getValue().toString(); String value = entry.getValue().toString();
prop.putIfAbsent(key, value); prop.putIfAbsent(key, value);
} }
prop.store(new PrintWriter(file, StandardCharsets.UTF_8), COMMENT); PrintWriter pw = new PrintWriter(file, StandardCharsets.UTF_8);
prop.store(pw, COMMENT);
pw.close();
protocol = Limbo.getInstance().serverImplmentationProtocol; protocol = Limbo.getInstance().serverImplmentationProtocol;

View File

@ -4,8 +4,8 @@ import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -14,6 +14,7 @@ import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -190,7 +191,9 @@ public class Limbo {
console.sendMessage("Loading packet id mappings from mapping.json ..."); console.sendMessage("Loading packet id mappings from mapping.json ...");
JSONObject json = (JSONObject) new JSONParser().parse(new FileReader(mappingFile)); InputStreamReader reader = new InputStreamReader(new FileInputStream(mappingFile), StandardCharsets.UTF_8);
JSONObject json = (JSONObject) new JSONParser().parse(reader);
reader.close();
String classPrefix = Packet.class.getName().substring(0, Packet.class.getName().lastIndexOf(".") + 1); String classPrefix = Packet.class.getName().substring(0, Packet.class.getName().lastIndexOf(".") + 1);
int mappingsCount = 0; int mappingsCount = 0;

View File

@ -3,7 +3,6 @@ package com.loohp.limbo.Metrics;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
@ -60,9 +59,9 @@ public class Metrics {
* @param serverUUID The uuid of the server. * @param serverUUID The uuid of the server.
* @param logFailedRequests Whether failed requests should be logged or not. * @param logFailedRequests Whether failed requests should be logged or not.
* @param logger The logger for the failed requests. * @param logger The logger for the failed requests.
* @throws FileNotFoundException * @throws IOException
*/ */
public Metrics() throws FileNotFoundException { public Metrics() throws IOException {
name = "Limbo"; name = "Limbo";
// Get the config file // Get the config file

View File

@ -1,7 +1,7 @@
package com.loohp.limbo.Permissions; package com.loohp.limbo.Permissions;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -23,7 +23,7 @@ public class PermissionsManager {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void loadDefaultPermissionFile(File file) throws FileNotFoundException { public void loadDefaultPermissionFile(File file) throws IOException {
FileConfiguration config = new FileConfiguration(file); FileConfiguration config = new FileConfiguration(file);
permissions.put("default", new ArrayList<>()); permissions.put("default", new ArrayList<>());
try { try {

View File

@ -1,9 +1,11 @@
package com.loohp.limbo.World; package com.loohp.limbo.World;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
@ -36,8 +38,8 @@ public class DimensionRegistry {
this.reg = file; this.reg = file;
try { try (InputStreamReader reader = new InputStreamReader(new FileInputStream(reg), StandardCharsets.UTF_8)) {
JSONObject json = (JSONObject) new JSONParser().parse(new FileReader(reg)); JSONObject json = (JSONObject) new JSONParser().parse(reader);
CompoundTag tag = CustomNBTUtils.getCompoundTagFromJson((JSONObject) json.get("value")); CompoundTag tag = CustomNBTUtils.getCompoundTagFromJson((JSONObject) json.get("value"));
defaultTag = tag; defaultTag = tag;
codec = defaultTag.clone(); codec = defaultTag.clone();