Skip to content

Commit

Permalink
SystemPropertiesHandler close reader and writer #12772
Browse files Browse the repository at this point in the history
  • Loading branch information
liyuheng55555 authored Jun 20, 2024
1 parent 8818162 commit 60b12d8
Showing 1 changed file with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Properties;
Expand Down Expand Up @@ -62,46 +64,53 @@ public void put(Object... keyOrValue) throws IOException {
+ " : "
+ Arrays.toString(keyOrValue));
}
try (AutoCloseableLock ignore = AutoCloseableLock.acquire(lock.writeLock());
FileOutputStream outputStream = new FileOutputStream(tmpFile)) {
Properties properties = new Properties();
try (AutoCloseableLock ignore = AutoCloseableLock.acquire(lock.writeLock())) {
// read old properties
if (formalFile.exists()) {
try (FileInputStream inputStream = new FileInputStream(formalFile)) {
properties.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
}
}
Properties properties = readWithoutLock(formalFile);
// store new properties
for (int i = 0; i < keyOrValue.length; i += 2) {
properties.put(keyOrValue[i], keyOrValue[i + 1]);
}
properties.store(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), "");
outputStream.getFD().sync();
writeWithoutLock(properties, tmpFile);
// replace file
replaceFormalFile();
}
}

public void overwrite(Properties properties) throws IOException {
try (AutoCloseableLock ignore = AutoCloseableLock.acquire(lock.writeLock())) {
if (!formalFile.exists()) {
writeImpl(properties, formalFile);
writeWithoutLock(properties, formalFile);
return;
}
writeImpl(properties, tmpFile);
writeWithoutLock(properties, tmpFile);
replaceFormalFile();
}
}

public Properties read() throws IOException {
try (AutoCloseableLock ignore = AutoCloseableLock.acquire(lock.readLock())) {
Properties properties = new Properties();
if (formalFile.exists()) {
try (FileInputStream inputStream = new FileInputStream(formalFile)) {
properties.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
}
return readWithoutLock(formalFile);
}
}

private void writeWithoutLock(Properties properties, File file) throws IOException {
try (FileOutputStream fileOutputStream = new FileOutputStream(file);
Writer writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8)) {
properties.store(writer, "");
fileOutputStream.getFD().sync();
}
}

private Properties readWithoutLock(File file) throws IOException {
Properties properties = new Properties();
if (file.exists()) {
try (FileInputStream inputStream = new FileInputStream(formalFile);
Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
properties.load(reader);
}
return properties;
}
return properties;
}

public boolean fileExist() {
Expand Down Expand Up @@ -142,12 +151,6 @@ protected void recover() {
throw new UnsupportedOperationException("Should never touch here");
}

private void writeImpl(Properties properties, File file) throws IOException {
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
properties.store(new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8), "");
}
}

private void replaceFormalFile() throws IOException {
if (!tmpFile.exists()) {
throw new FileNotFoundException(
Expand Down

0 comments on commit 60b12d8

Please sign in to comment.