From 553cdf5250fd15491f9691800eabe7140bc5e4a5 Mon Sep 17 00:00:00 2001 From: Gravita Date: Fri, 9 Dec 2022 21:17:07 +0700 Subject: [PATCH] [FEATURE] Safe config write --- .../gravit/launchserver/LaunchServerStarter.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java index cab9413e..d4208a21 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java @@ -28,10 +28,7 @@ import pro.gravit.utils.helper.JVMHelper; import pro.gravit.utils.helper.LogHelper; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.io.Writer; +import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.security.Security; @@ -148,9 +145,11 @@ public LaunchServerRuntimeConfig readRuntimeConfig() throws IOException { @Override public void writeConfig(LaunchServerConfig config) throws IOException { - try (Writer writer = IOHelper.newWriter(configFile)) { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + try (Writer writer = IOHelper.newWriter(output)) { if (Launcher.gsonManager.configGson != null) { Launcher.gsonManager.configGson.toJson(config, writer); + IOHelper.write(configFile, output.toByteArray()); } else { logger.error("Error writing LaunchServer runtime config file. Gson is null"); } @@ -159,9 +158,11 @@ public void writeConfig(LaunchServerConfig config) throws IOException { @Override public void writeRuntimeConfig(LaunchServerRuntimeConfig config) throws IOException { - try (Writer writer = IOHelper.newWriter(runtimeConfigFile)) { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + try (Writer writer = IOHelper.newWriter(output)) { if (Launcher.gsonManager.configGson != null) { Launcher.gsonManager.configGson.toJson(config, writer); + IOHelper.write(runtimeConfigFile, output.toByteArray()); } else { logger.error("Error writing LaunchServer runtime config file. Gson is null"); }