forked from CharlesSkelton/studio
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implicitly move tmp file to the target config file
- Loading branch information
Showing
3 changed files
with
68 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |