You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ia...@apache.org on 2013/12/13 17:18:58 UTC

[16/19] git commit: Android: Clean up unclosed file objects

Android: Clean up unclosed file objects


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/commit/6d0dad63
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/6d0dad63
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/6d0dad63

Branch: refs/heads/dev
Commit: 6d0dad633e4a62c3043604ead3566f31d74ae335
Parents: 0a6adf0
Author: Ian Clelland <ic...@chromium.org>
Authored: Mon Dec 2 15:53:43 2013 -0500
Committer: Ian Clelland <ic...@chromium.org>
Committed: Fri Dec 13 11:16:36 2013 -0500

----------------------------------------------------------------------
 src/android/LocalFilesystem.java | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/6d0dad63/src/android/LocalFilesystem.java
----------------------------------------------------------------------
diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java
index adb3ef6..be1d2d1 100644
--- a/src/android/LocalFilesystem.java
+++ b/src/android/LocalFilesystem.java
@@ -537,19 +537,20 @@ public class LocalFilesystem implements Filesystem {
 		String contentType;
 		
 		File file = new File(this.filesystemPathForURL(inputURL));
-		InputStream inputStream = new FileInputStream(file);
-		
 		contentType = FileHelper.getMimeTypeForExtension(file.getAbsolutePath());
+		
+		InputStream inputStream = new FileInputStream(file);
 		int numBytesRead = 0;
-
-		if (start > 0) {
-			inputStream.skip(start);
-		}
-
-		while (numBytesToRead > 0 && (numBytesRead = inputStream.read(bytes, numBytesRead, numBytesToRead)) >= 0) {
-			numBytesToRead -= numBytesRead;
+		try {
+			if (start > 0) {
+				inputStream.skip(start);
+			}
+			while (numBytesToRead > 0 && (numBytesRead = inputStream.read(bytes, numBytesRead, numBytesToRead)) >= 0) {
+				numBytesToRead -= numBytesRead;
+			}
+		} finally {
+			inputStream.close();
 		}
-		inputStream.close();
 		readFileCallback.handleData(bytes, contentType);
 	}
 
@@ -572,12 +573,16 @@ public class LocalFilesystem implements Filesystem {
         ByteArrayInputStream in = new ByteArrayInputStream(rawData);
         try
         {
+        	byte buff[] = new byte[rawData.length];
             FileOutputStream out = new FileOutputStream(this.filesystemPathForURL(inputURL), append);
-            byte buff[] = new byte[rawData.length];
-            in.read(buff, 0, buff.length);
-            out.write(buff, 0, rawData.length);
-            out.flush();
-            out.close();
+            try {
+            	in.read(buff, 0, buff.length);
+            	out.write(buff, 0, rawData.length);
+            	out.flush();
+            } finally {
+            	// Always close the output
+            	out.close();
+            }
         }
         catch (NullPointerException e)
         {