From f5faa8fc4d49bb25c305bfc8f87cab89fe16c7e7 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 19 Jan 2023 19:30:12 +0100 Subject: [PATCH] try with resources in CredentialStore --- .../android/database/CredentialStore.java | 29 +++++++++++-------- .../im/conversations/android/xmpp/Entity.java | 4 +++ 2 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 src/main/java/im/conversations/android/xmpp/Entity.java diff --git a/src/main/java/im/conversations/android/database/CredentialStore.java b/src/main/java/im/conversations/android/database/CredentialStore.java index ceaa1ed2b..e5b8b22f0 100644 --- a/src/main/java/im/conversations/android/database/CredentialStore.java +++ b/src/main/java/im/conversations/android/database/CredentialStore.java @@ -16,7 +16,6 @@ import im.conversations.android.xmpp.sasl.HashedToken; import im.conversations.android.xmpp.sasl.SaslMechanism; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; @@ -80,7 +79,7 @@ public class CredentialStore { this.set(account, modifiedCredential); } - public void setFastToken( + public synchronized void setFastToken( final Account account, final HashedToken.Mechanism mechanism, final String token) throws GeneralSecurityException, IOException { final Credential credential = getOrEmpty(account); @@ -98,7 +97,8 @@ public class CredentialStore { this.set(account, modifiedCredential); } - public void resetFastToken(final Account account) throws GeneralSecurityException, IOException { + public synchronized void resetFastToken(final Account account) + throws GeneralSecurityException, IOException { final Credential credential = getOrEmpty(account); final Credential modifiedCredential = new Credential( @@ -114,7 +114,8 @@ public class CredentialStore { this.set(account, modifiedCredential); } - public void setPinnedMechanism(final Account account, final SaslMechanism mechanism) + public synchronized void setPinnedMechanism( + final Account account, final SaslMechanism mechanism) throws GeneralSecurityException, IOException { final String pinnedMechanism = mechanism.getMechanism(); final String pinnedChannelBinding; @@ -139,7 +140,7 @@ public class CredentialStore { this.set(account, modifiedCredential); } - public void resetPinnedMechanism(final Account account) + public synchronized void resetPinnedMechanism(final Account account) throws GeneralSecurityException, IOException { final Credential credential = getOrEmpty(account); final Credential modifiedCredential = @@ -181,19 +182,23 @@ public class CredentialStore { private Map load() throws GeneralSecurityException, IOException { final EncryptedFile encryptedFile = getEncryptedFile(); - final FileInputStream inputStream = encryptedFile.openFileInput(); - final Type type = new TypeToken>() {}.getType(); - return GSON.fromJson(new InputStreamReader(inputStream), type); + try (final FileInputStream inputStream = encryptedFile.openFileInput()) { + final Type type = new TypeToken>() {}.getType(); + return GSON.fromJson(new InputStreamReader(inputStream), type); + } } private void store(final Map store) throws GeneralSecurityException, IOException { final File file = getCredentialStoreFile(); - file.delete(); + if (file.delete()) { + // Log.d(Config.LOGTAG,"delete old credential file: "+file.getAbsolutePath()); + } final EncryptedFile encryptedFile = getEncryptedFile(file); - try (final FileOutputStream outputStream = encryptedFile.openFileOutput()) { - GSON.toJson(store, new OutputStreamWriter(outputStream)); - outputStream.flush(); + try (final OutputStreamWriter writer = + new OutputStreamWriter(encryptedFile.openFileOutput())) { + GSON.toJson(store, writer); + writer.flush(); } } diff --git a/src/main/java/im/conversations/android/xmpp/Entity.java b/src/main/java/im/conversations/android/xmpp/Entity.java new file mode 100644 index 000000000..b3061ed0e --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/Entity.java @@ -0,0 +1,4 @@ +package im.conversations.android.xmpp; + +public class Entity { +}