mirror of
https://github.com/nextcloud/talk-android
synced 2025-07-12 23:34:31 +01:00
utils: use try-with-resources to properly close Closable. (#522)
Use try-with-resources to properly close Closable
This commit is contained in:
parent
f6c3c51e2c
commit
ae77f48a6d
@ -24,7 +24,9 @@ import android.content.Context;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Base64;
|
import android.util.Base64;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import autodagger.AutoInjector;
|
import autodagger.AutoInjector;
|
||||||
|
|
||||||
import com.bluelinelabs.logansquare.LoganSquare;
|
import com.bluelinelabs.logansquare.LoganSquare;
|
||||||
import com.nextcloud.talk.R;
|
import com.nextcloud.talk.R;
|
||||||
import com.nextcloud.talk.api.NcApi;
|
import com.nextcloud.talk.api.NcApi;
|
||||||
@ -36,12 +38,15 @@ import com.nextcloud.talk.models.json.push.PushConfigurationState;
|
|||||||
import com.nextcloud.talk.models.json.push.PushRegistrationOverall;
|
import com.nextcloud.talk.models.json.push.PushRegistrationOverall;
|
||||||
import com.nextcloud.talk.utils.database.user.UserUtils;
|
import com.nextcloud.talk.utils.database.user.UserUtils;
|
||||||
import com.nextcloud.talk.utils.preferences.AppPreferences;
|
import com.nextcloud.talk.utils.preferences.AppPreferences;
|
||||||
|
|
||||||
import io.reactivex.Observer;
|
import io.reactivex.Observer;
|
||||||
import io.reactivex.disposables.Disposable;
|
import io.reactivex.disposables.Disposable;
|
||||||
import io.reactivex.schedulers.Schedulers;
|
import io.reactivex.schedulers.Schedulers;
|
||||||
|
|
||||||
import org.greenrobot.eventbus.EventBus;
|
import org.greenrobot.eventbus.EventBus;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
import java.security.spec.InvalidKeySpecException;
|
import java.security.spec.InvalidKeySpecException;
|
||||||
@ -128,15 +133,18 @@ public class PushUtils {
|
|||||||
|
|
||||||
private int saveKeyToFile(Key key, String path) {
|
private int saveKeyToFile(Key key, String path) {
|
||||||
byte[] encoded = key.getEncoded();
|
byte[] encoded = key.getEncoded();
|
||||||
FileOutputStream keyFileOutputStream = null;
|
|
||||||
try {
|
try {
|
||||||
if (!new File(path).exists()) {
|
if (!new File(path).exists()) {
|
||||||
new File(path).createNewFile();
|
if (!new File(path).createNewFile()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (FileOutputStream keyFileOutputStream = new FileOutputStream(path)) {
|
||||||
|
keyFileOutputStream.write(encoded);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
keyFileOutputStream = new FileOutputStream(path);
|
|
||||||
keyFileOutputStream.write(encoded);
|
|
||||||
keyFileOutputStream.close();
|
|
||||||
return 0;
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
Log.d(TAG, "Failed to save key to file");
|
Log.d(TAG, "Failed to save key to file");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -397,12 +405,9 @@ public class PushUtils {
|
|||||||
path = privateKeyFile.getAbsolutePath();
|
path = privateKeyFile.getAbsolutePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInputStream fileInputStream = null;
|
try (FileInputStream fileInputStream = new FileInputStream(path)) {
|
||||||
try {
|
|
||||||
fileInputStream = new FileInputStream(path);
|
|
||||||
byte[] bytes = new byte[fileInputStream.available()];
|
byte[] bytes = new byte[fileInputStream.available()];
|
||||||
fileInputStream.read(bytes);
|
fileInputStream.read(bytes);
|
||||||
fileInputStream.close();
|
|
||||||
|
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user