Synchronize fileChannel access

Potention fix for #89
This commit is contained in:
AsamK 2019-09-05 10:15:52 +02:00
parent 8c295a3f90
commit e36a54e7cc

View file

@ -129,7 +129,11 @@ public class SignalAccount {
} }
private void load() throws IOException { private void load() throws IOException {
JsonNode rootNode = jsonProcessor.readTree(Channels.newInputStream(fileChannel)); JsonNode rootNode;
synchronized (fileChannel) {
fileChannel.position(0);
rootNode = jsonProcessor.readTree(Channels.newInputStream(fileChannel));
}
JsonNode node = rootNode.get("deviceId"); JsonNode node = rootNode.get("deviceId");
if (node != null) { if (node != null) {
@ -204,10 +208,12 @@ public class SignalAccount {
.putPOJO("threadStore", threadStore) .putPOJO("threadStore", threadStore)
; ;
try { try {
fileChannel.position(0); synchronized (fileChannel) {
jsonProcessor.writeValue(Channels.newOutputStream(fileChannel), rootNode); fileChannel.position(0);
fileChannel.truncate(fileChannel.position()); jsonProcessor.writeValue(Channels.newOutputStream(fileChannel), rootNode);
fileChannel.force(false); fileChannel.truncate(fileChannel.position());
fileChannel.force(false);
}
} catch (Exception e) { } catch (Exception e) {
System.err.println(String.format("Error saving file: %s", e.getMessage())); System.err.println(String.format("Error saving file: %s", e.getMessage()));
} }