[FEATURE] Safe config write

This commit is contained in:
Gravita 2022-12-09 21:17:07 +07:00
parent 0d1b32fc1c
commit 553cdf5250

View file

@ -28,10 +28,7 @@
import pro.gravit.utils.helper.JVMHelper; import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import java.io.BufferedReader; import java.io.*;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.Security; import java.security.Security;
@ -148,9 +145,11 @@ public LaunchServerRuntimeConfig readRuntimeConfig() throws IOException {
@Override @Override
public void writeConfig(LaunchServerConfig config) throws IOException { 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) { if (Launcher.gsonManager.configGson != null) {
Launcher.gsonManager.configGson.toJson(config, writer); Launcher.gsonManager.configGson.toJson(config, writer);
IOHelper.write(configFile, output.toByteArray());
} else { } else {
logger.error("Error writing LaunchServer runtime config file. Gson is null"); logger.error("Error writing LaunchServer runtime config file. Gson is null");
} }
@ -159,9 +158,11 @@ public void writeConfig(LaunchServerConfig config) throws IOException {
@Override @Override
public void writeRuntimeConfig(LaunchServerRuntimeConfig config) throws IOException { 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) { if (Launcher.gsonManager.configGson != null) {
Launcher.gsonManager.configGson.toJson(config, writer); Launcher.gsonManager.configGson.toJson(config, writer);
IOHelper.write(runtimeConfigFile, output.toByteArray());
} else { } else {
logger.error("Error writing LaunchServer runtime config file. Gson is null"); logger.error("Error writing LaunchServer runtime config file. Gson is null");
} }