ソースを参照

Merge pull request #13193 from nextcloud/file-stream-constructor-auto-close

Auto Close File Streams
Alper Öztürk 9 ヶ月 前
コミット
f9e4261061

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

@@ -19,6 +19,7 @@ import com.owncloud.android.utils.SyncedFolderUtils;
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.HashSet;
@@ -212,7 +213,8 @@ public class FilesystemDataProvider {
 
     private long getFileChecksum(String filepath) {
 
-        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(filepath))) {
+        try (FileInputStream fileInputStream = new FileInputStream(filepath);
+            InputStream inputStream = new BufferedInputStream(fileInputStream)) {
             CRC32 crc = new CRC32();
             byte[] buf = new byte[1024 * 64];
             int size;

+ 4 - 16
app/src/main/java/com/owncloud/android/ui/adapter/PrintAdapter.java

@@ -17,19 +17,17 @@ import android.print.PrintDocumentInfo;
 
 import com.owncloud.android.lib.common.utils.Log_OC;
 
-import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.Objects;
 
 public class PrintAdapter extends PrintDocumentAdapter {
     private static final String TAG = PrintAdapter.class.getSimpleName();
     private static final String PDF_NAME = "finalPrint.pdf";
 
-    private String filePath;
+    private final String filePath;
 
     public PrintAdapter(String filePath) {
         this.filePath = filePath;
@@ -58,11 +56,9 @@ public class PrintAdapter extends PrintDocumentAdapter {
                         ParcelFileDescriptor destination,
                         CancellationSignal cancellationSignal,
                         WriteResultCallback callback) {
-        InputStream in = null;
-        OutputStream out = null;
-        try {
-            in = new FileInputStream(new File(filePath));
-            out = new FileOutputStream(destination.getFileDescriptor());
+
+        try (InputStream in = new FileInputStream(filePath);
+             OutputStream out = new FileOutputStream(destination.getFileDescriptor())) {
 
             byte[] buf = new byte[16384];
             int size;
@@ -79,14 +75,6 @@ public class PrintAdapter extends PrintDocumentAdapter {
 
         } catch (IOException e) {
             Log_OC.e(TAG, "Error using temp file", e);
-        } finally {
-            try {
-                Objects.requireNonNull(in).close();
-                Objects.requireNonNull(out).close();
-
-            } catch (IOException | NullPointerException e) {
-                Log_OC.e(TAG, "Error closing streams", e);
-            }
         }
     }
 }

+ 4 - 22
app/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java

@@ -132,12 +132,9 @@ public class FileOperationsHelper {
     private String getUrlFromFile(String storagePath, Pattern pattern) {
         String url = null;
 
-        InputStreamReader fr = null;
-        BufferedReader br = null;
-        try {
-            fr = new InputStreamReader(new FileInputStream(storagePath), StandardCharsets.UTF_8);
-            br = new BufferedReader(fr);
-
+        try (FileInputStream inputStream = new FileInputStream(storagePath);
+             InputStreamReader fr = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
+             BufferedReader br = new BufferedReader(fr)) {
             String line;
             while ((line = br.readLine()) != null) {
                 Matcher m = pattern.matcher(line);
@@ -148,23 +145,8 @@ public class FileOperationsHelper {
             }
         } catch (IOException e) {
             Log_OC.d(TAG, e.getMessage());
-        } finally {
-            if (br != null) {
-                try {
-                    br.close();
-                } catch (IOException e) {
-                    Log_OC.d(TAG, "Error closing buffered reader for URL file", e);
-                }
-            }
-
-            if (fr != null) {
-                try {
-                    fr.close();
-                } catch (IOException e) {
-                    Log_OC.d(TAG, "Error closing file reader for URL file", e);
-                }
-            }
         }
+
         return url;
     }
 

+ 4 - 23
app/src/main/java/com/owncloud/android/utils/FileStorageUtils.java

@@ -371,17 +371,13 @@ public final class FileStorageUtils {
         }
     }
 
-    @SuppressFBWarnings(value="OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE",
-            justification="False-positive on the output stream")
+    @SuppressFBWarnings(value = "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE",
+        justification = "False-positive on the output stream")
     public static boolean copyFile(File src, File target) {
         boolean ret = true;
 
-        InputStream in = null;
-        OutputStream out = null;
-
-        try {
-            in = new FileInputStream(src);
-            out = new FileOutputStream(target);
+        try (InputStream in = new FileInputStream(src);
+             OutputStream out = new FileOutputStream(target)) {
             byte[] buf = new byte[1024];
             int len;
             while ((len = in.read(buf)) > 0) {
@@ -389,21 +385,6 @@ public final class FileStorageUtils {
             }
         } catch (IOException ex) {
             ret = false;
-        } finally {
-            if (in != null) {
-                try {
-                    in.close();
-                } catch (IOException e) {
-                    Log_OC.e(TAG, "Error closing input stream during copy", e);
-                }
-            }
-            if (out != null) {
-                try {
-                    out.close();
-                } catch (IOException e) {
-                    Log_OC.e(TAG, "Error closing output stream during copy", e);
-                }
-            }
         }
 
         return ret;