]> nmode's Git Repositories - signal-cli/commitdiff
Synchronize fileChannel access
authorAsamK <asamk@gmx.de>
Thu, 5 Sep 2019 08:15:52 +0000 (10:15 +0200)
committerAsamK <asamk@gmx.de>
Thu, 5 Sep 2019 11:49:19 +0000 (13:49 +0200)
Potention fix for #89

src/main/java/org/asamk/signal/storage/SignalAccount.java

index 4378d052a64871850b6c52c4d4a30fdb7f91b1e9..6af9d460ee118ebe58d79b36fc057e16cd846a26 100644 (file)
@@ -129,7 +129,11 @@ public class SignalAccount {
     }
 
     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");
         if (node != null) {
@@ -204,10 +208,12 @@ public class SignalAccount {
                 .putPOJO("threadStore", threadStore)
         ;
         try {
-            fileChannel.position(0);
-            jsonProcessor.writeValue(Channels.newOutputStream(fileChannel), rootNode);
-            fileChannel.truncate(fileChannel.position());
-            fileChannel.force(false);
+            synchronized (fileChannel) {
+                fileChannel.position(0);
+                jsonProcessor.writeValue(Channels.newOutputStream(fileChannel), rootNode);
+                fileChannel.truncate(fileChannel.position());
+                fileChannel.force(false);
+            }
         } catch (Exception e) {
             System.err.println(String.format("Error saving file: %s", e.getMessage()));
         }