From d283d20f6ca4b9347084cac4663a5f557950bb6f Mon Sep 17 00:00:00 2001 From: dzmipt Date: Fri, 13 Sep 2024 16:49:43 +0200 Subject: [PATCH] Implicitly move tmp file to the target config file --- src/studio/utils/FilesBackup.java | 41 ++------------- src/studio/utils/PropertiesConfig.java | 4 +- src/studio/utils/TmpfileOutputStream.java | 62 +++++++++++++++++++++++ 3 files changed, 68 insertions(+), 39 deletions(-) create mode 100644 src/studio/utils/TmpfileOutputStream.java diff --git a/src/studio/utils/FilesBackup.java b/src/studio/utils/FilesBackup.java index bef5593b..6dccd28b 100644 --- a/src/studio/utils/FilesBackup.java +++ b/src/studio/utils/FilesBackup.java @@ -5,9 +5,7 @@ import org.apache.logging.log4j.Logger; import studio.utils.log4j.EnvConfig; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -17,8 +15,6 @@ import java.util.HashMap; import java.util.Map; -import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; - public class FilesBackup { private static final String BACKUP_FOLDER = "backup"; @@ -59,46 +55,14 @@ protected FilesBackup(String backupFolder) { } } - public OutputStream newFileOutputStream(String filename) throws IOException { + public TmpfileOutputStream newFileOutputStream(String filename) throws IOException { try { backup(filename); } catch (IOException e) { log.error("Error on backup {}", filename, e); } - Path tmpFile = Paths.get(filename).resolveSibling(FilenameUtils.getName(filename) + ".tmp"); - log.info("Saving to tmp file {}", tmpFile.toString()); - Files.deleteIfExists(tmpFile); - - FileOutputStream outputStream = new FileOutputStream(tmpFile.toFile()); - return new OutputStream() { - @Override - public void write(byte[] b) throws IOException { - outputStream.write(b); - } - - @Override - public void write(byte[] b, int off, int len) throws IOException { - outputStream.write(b, off, len); - } - - @Override - public void write(int b) throws IOException { - outputStream.write(b); - } - - @Override - public void flush() throws IOException { - outputStream.flush(); - } - - @Override - public void close() throws IOException { - outputStream.close(); - Files.move(tmpFile, Paths.get(filename), REPLACE_EXISTING); - log.info("moved {} -> {}", tmpFile.toString(), filename); - } - }; + return new TmpfileOutputStream(filename); } protected void backup(String filename) throws IOException { @@ -206,4 +170,5 @@ int getIndex() { } } + } diff --git a/src/studio/utils/PropertiesConfig.java b/src/studio/utils/PropertiesConfig.java index afccca4b..c369b2b6 100644 --- a/src/studio/utils/PropertiesConfig.java +++ b/src/studio/utils/PropertiesConfig.java @@ -98,7 +98,7 @@ public void saveToDisk() { log.info("Saving config to file {} with number of properties {}", filename, size()); - try (OutputStream out = FilesBackup.getInstance().newFileOutputStream(filename)) { + try (TmpfileOutputStream out = FilesBackup.getInstance().newFileOutputStream(filename)) { byte[] lineSeparator = System.getProperty("line.separator").getBytes(CHARSET); BufferedReader reader = new BufferedReader( @@ -122,6 +122,8 @@ public void saveToDisk() { out.write(lineSeparator); } + out.writeCompleted(); + propertiesToSave = null; } catch (IOException e) { log.error("Error in saving config to the file {}", filename, e); diff --git a/src/studio/utils/TmpfileOutputStream.java b/src/studio/utils/TmpfileOutputStream.java new file mode 100644 index 00000000..2d5f70cc --- /dev/null +++ b/src/studio/utils/TmpfileOutputStream.java @@ -0,0 +1,62 @@ +package studio.utils; + +import org.apache.commons.io.FilenameUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + +public class TmpfileOutputStream extends OutputStream { + private static final Logger log = LogManager.getLogger(); + + private final String filename; + private final Path tmpFile; + private final FileOutputStream outputStream; + + public TmpfileOutputStream(String filename) throws IOException { + this.filename = filename; + tmpFile = Paths.get(filename).resolveSibling(FilenameUtils.getName(filename) + + "." + System.currentTimeMillis() + ".tmp"); + log.info("Saving to tmp file {}", tmpFile.toString()); + Files.deleteIfExists(tmpFile); + outputStream = new FileOutputStream(tmpFile.toFile()); + } + + @Override + public void write(byte[] b) throws IOException { + outputStream.write(b); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + outputStream.write(b, off, len); + } + + @Override + public void write(int b) throws IOException { + outputStream.write(b); + } + + @Override + public void flush() throws IOException { + outputStream.flush(); + } + + @Override + public void close() throws IOException { + outputStream.close(); + } + + public void writeCompleted() throws IOException { + Files.move(tmpFile, Paths.get(filename), REPLACE_EXISTING); + log.info("moved {} -> {}", tmpFile.toString(), filename); + } + +}