Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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