Browse Source

Merge pull request #2403 from ardevd/bugfixes

Properly close Closable objects to avoid memleaks
Andy Scherzinger 7 years ago
parent
commit
c1fabc2f60

+ 1 - 3
src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java

@@ -213,9 +213,7 @@ public class FilesystemDataProvider {
 
     private long getFileChecksum(String filepath) {
 
-        InputStream inputStream;
-        try {
-            inputStream = new BufferedInputStream(new FileInputStream(filepath));
+        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(filepath))){
             CRC32 crc = new CRC32();
             int cnt;
             while ((cnt = inputStream.read()) != -1) {

+ 1 - 2
src/main/java/com/owncloud/android/jobs/ContactsImportJob.java

@@ -115,8 +115,7 @@ public class ContactsImportJob extends Job {
         String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
         Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
         VCard vCard = null;
-        try {
-            InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
+        try (InputStream inputStream = getContext().getContentResolver().openInputStream(uri)){
             ArrayList<VCard> vCardList = new ArrayList<>();
             vCardList.addAll(Ezvcard.parse(inputStream).all());
             if (vCardList.size() > 0) {

+ 1 - 3
src/main/java/com/owncloud/android/operations/DownloadFileOperation.java

@@ -201,12 +201,10 @@ public class DownloadFileOperation extends RemoteOperation {
                 byte[] authenticationTag = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles()
                         .get(mFile.getEncryptedFileName()).getAuthenticationTag());
 
-                try {
+                try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)){
                     byte[] decryptedBytes = EncryptionUtils.decryptFile(tmpFile, key, iv, authenticationTag);
 
-                    FileOutputStream fileOutputStream = new FileOutputStream(tmpFile);
                     fileOutputStream.write(decryptedBytes);
-                    fileOutputStream.close();
                 } catch (Exception e) {
                     return new RemoteOperationResult(e);
                 }

+ 2 - 6
src/main/java/com/owncloud/android/providers/DiskLruImageCacheFileProvider.java

@@ -89,15 +89,11 @@ public class DiskLruImageCacheFileProvider extends ContentProvider {
             byte[] bitmapData = bos.toByteArray();
 
             //write the bytes in file
-            FileOutputStream fos = null;
-            try {
-                fos = new FileOutputStream(f);
+            try (FileOutputStream fos = new FileOutputStream(f)){
+                fos.write(bitmapData);
             } catch (FileNotFoundException e) {
                 Log_OC.e(TAG, "File not found: " + e.getMessage());
             }
-            fos.write(bitmapData);
-            fos.flush();
-            fos.close();
 
         } catch (Exception e) {
             Log_OC.e(TAG, "Error opening file: " + e.getMessage());

+ 10 - 10
src/main/java/com/owncloud/android/ui/asynctasks/LoadingVersionNumberTask.java

@@ -28,6 +28,7 @@ import com.owncloud.android.lib.common.utils.Log_OC;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.net.MalformedURLException;
 import java.net.URL;
 
 /**
@@ -39,16 +40,15 @@ public class LoadingVersionNumberTask extends AsyncTask<String, Void, Integer> {
     protected Integer doInBackground(String... args) {
         try {
             URL url = new URL(args[0]);
-            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
-
-            Integer latestVersion = Integer.parseInt(in.readLine());
-            in.close();
-
-            return latestVersion;
-
-        } catch (IOException e) {
-            Log_OC.e(TAG, "Error loading version number", e);
+            try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))){
+                return Integer.parseInt(in.readLine());
+
+            } catch (IOException e) {
+                Log_OC.e(TAG, "Error loading version number", e);
+            }
+        } catch (MalformedURLException e) {
+            Log_OC.e(TAG, "Malformed URL", e);
         }
         return -1;
     }
-}
+}

+ 1 - 2
src/main/java/com/owncloud/android/ui/preview/ImageViewCustom.java

@@ -130,8 +130,7 @@ public class ImageViewCustom extends AppCompatImageView {
      * @param storagePath the storage path of the GIF image
      */
     public void setGIFImageFromStoragePath(String storagePath) {
-        try {
-            InputStream gifInputStream = new FileInputStream(storagePath);
+        try (InputStream gifInputStream = new FileInputStream(storagePath)){
             setLayerType(View.LAYER_TYPE_SOFTWARE, null);
             setFocusable(true);