Fixed JSONDatabaseHandler to avoid memory leak

Thanks Sonarcloud ^^
This commit is contained in:
Florian CUNY 2018-10-29 21:39:48 +01:00
parent 2dd8fcb13c
commit 493a525775

View File

@ -99,15 +99,13 @@ public class JSONDatabaseHandler<T> extends AbstractJSONDatabaseHandler<T> {
String toStore = getGson().toJson(instance); String toStore = getGson().toJson(instance);
try { try (FileWriter fileWriter = new FileWriter(file)) {
File tmpFile = new File(tableFolder, fileName + ".bak"); File tmpFile = new File(tableFolder, fileName + ".bak");
if (file.exists()) { if (file.exists()) {
// Make a backup of file // Make a backup of file
Files.copy(file.toPath(), tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(file.toPath(), tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} }
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(toStore); fileWriter.write(toStore);
fileWriter.close();
Files.deleteIfExists(tmpFile.toPath()); Files.deleteIfExists(tmpFile.toPath());
} catch (IOException e) { } catch (IOException e) {
plugin.logError("Could not save json file: " + path + " " + fileName + " " + e.getMessage()); plugin.logError("Could not save json file: " + path + " " + fileName + " " + e.getMessage());