szetszwo commented on code in PR #1237:
URL: https://github.com/apache/ratis/pull/1237#discussion_r1999121664
##########
ratis-common/src/main/java/org/apache/ratis/util/AtomicFileOutputStream.java:
##########
@@ -60,7 +60,7 @@ public AtomicFileOutputStream(File outFile) throws
IOException {
}
public AtomicFileOutputStream(File outFile, File tmpFile) throws IOException
{
- super(FileUtils.newOutputStreamForceAtClose(tmpFile,
StandardOpenOption.CREATE, StandardOpenOption.WRITE));
+ super(FileUtils.newOutputStreamForceAtClose(tmpFile,
StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE));
Review Comment:
You are right -- we need `TRUNCATE_EXISTING`.
```
0: 0123456789
0: aaaaaa6789
0: bbb
```
```java
static void main(String[] args) throws Exception {
final File f = new File("a.txt");
try(OutputStream out = newOutputStreamForceAtClose(f,
StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
out.write("0123456789".getBytes(StandardCharsets.UTF_8));
}
dump(f);
try(OutputStream out = newOutputStreamForceAtClose(f,
StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
out.write("aaaaaa".getBytes(StandardCharsets.UTF_8));
}
dump(f);
try(OutputStream out = newOutputStreamForceAtClose(f,
StandardOpenOption.CREATE, StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
out.write("bbb".getBytes(StandardCharsets.UTF_8));
}
dump(f);
}
static void dump(File f) throws Exception {
try(BufferedReader in = new BufferedReader(new InputStreamReader(
newInputStream(f.toPath()), StandardCharsets.UTF_8))) {
String line;
for(int i = 0; (line = in.readLine()) != null; i++) {
System.out.printf("%3d: %s%n", i, line);
}
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]