OSDN Git Service

Code cleanup: make sure FileInputStreams are closed.
authorRaphael <raphael@google.com>
Fri, 14 Oct 2011 01:47:02 +0000 (18:47 -0700)
committerRaphael <raphael@google.com>
Fri, 14 Oct 2011 01:47:02 +0000 (18:47 -0700)
Various places of the code construct a new FileInputStream
on the fly and give it to another method. One many
occasions the stream is never properly closed, which can
lock files on Windows.

2 specific cases:
- Properties.load() doesn't seem to close its input
  (when looking at the source bundled with the JRE).
- The doc of InputSource (used by various XML parsers like
  the pull parser) indicates the caller should in general
  not close the stream and the parser itself should do it.

Change-Id: I622b54a22f97ed2c9c8fdc56ccde331207d9d212

anttasks/src/com/android/ant/TaskHelper.java
ddms/app/src/com/android/ddms/Main.java
ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/BugReportImporter.java
ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/EventLogImporter.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/project/ExportHelper.java
sdkmanager/app/src/com/android/sdkmanager/internal/repository/AboutPage.java
traceview/src/com/android/traceview/MainWindow.java

index a360eaf..8a3d6bc 100644 (file)
@@ -59,7 +59,20 @@ final class TaskHelper {
             // tools folder must exist, or this custom task wouldn't run!
             File toolsFolder= new File(sdkFile, SdkConstants.FD_TOOLS);
             File sourceProp = new File(toolsFolder, SdkConstants.FN_SOURCE_PROP);
-            p.load(new FileInputStream(sourceProp));
+
+            FileInputStream fis = null;
+            try {
+                fis = new FileInputStream(sourceProp);
+                p.load(fis);
+            } finally {
+                if (fis != null) {
+                    try {
+                        fis.close();
+                    } catch (IOException ignore) {
+                    }
+                }
+            }
+
             String value = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
             if (value != null) {
                 return Integer.parseInt(value);
index 583feb5..6fe69c7 100644 (file)
@@ -144,7 +144,19 @@ public class Main {
             } else {
                 sourceProp = new File("source.properties"); //$NON-NLS-1$
             }
-            p.load(new FileInputStream(sourceProp));
+            FileInputStream fis = null;
+            try {
+                fis = new FileInputStream(sourceProp);
+                p.load(fis);
+            } finally {
+                if (fis != null) {
+                    try {
+                        fis.close();
+                    } catch (IOException ignore) {
+                    }
+                }
+            }
+
             sRevision = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
             if (sRevision != null && sRevision.length() > 0) {
                 stats.ping("ddms", sRevision);  //$NON-NLS-1$
index 9de1ac7..da41e70 100644 (file)
@@ -24,14 +24,14 @@ import java.io.InputStreamReader;
 import java.util.ArrayList;
 
 public class BugReportImporter {
-    
+
     private final static String TAG_HEADER = "------ EVENT LOG TAGS ------";
     private final static String LOG_HEADER = "------ EVENT LOG ------";
     private final static String HEADER_TAG = "------";
-    
+
     private String[] mTags;
     private String[] mLog;
-    
+
     public BugReportImporter(String filePath) throws FileNotFoundException {
         BufferedReader reader = new BufferedReader(
                 new InputStreamReader(new FileInputStream(filePath)));
@@ -45,20 +45,27 @@ public class BugReportImporter {
                 }
             }
         } catch (IOException e) {
+        } finally {
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException ignore) {
+                }
+            }
         }
     }
-    
+
     public String[] getTags() {
         return mTags;
     }
-    
+
     public String[] getLog() {
         return mLog;
     }
 
     private void readTags(BufferedReader reader) throws IOException {
         String line;
-        
+
         ArrayList<String> content = new ArrayList<String>();
         while ((line = reader.readLine()) != null) {
             if (LOG_HEADER.equals(line)) {
@@ -82,8 +89,8 @@ public class BugReportImporter {
                 break;
             }
         }
-        
+
         mLog = content.toArray(new String[content.size()]);
     }
-    
+
 }
index a1303f6..011bcf1 100644 (file)
@@ -47,6 +47,19 @@ public class EventLogImporter {
             readTags(tagReader);
             readLog(eventReader);
         } catch (IOException e) {
+        } finally {
+            if (tagReader != null) {
+                try {
+                    tagReader.close();
+                } catch (IOException ignore) {
+                }
+            }
+            if (eventReader != null) {
+                try {
+                    eventReader.close();
+                } catch (IOException ignore) {
+                }
+            }
         }
     }
 
index d12f168..479098d 100644 (file)
@@ -345,9 +345,19 @@ public final class ExportHelper {
             // put the content of the file.
             byte[] buffer = new byte[1024];
             int count;
-            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
-            while ((count = bis.read(buffer)) != -1) {
-                jar.write(buffer, 0, count);
+            BufferedInputStream bis = null;
+            try {
+                bis = new BufferedInputStream(new FileInputStream(file));
+                while ((count = bis.read(buffer)) != -1) {
+                    jar.write(buffer, 0, count);
+                }
+            } finally {
+                if (bis != null) {
+                    try {
+                        bis.close();
+                    } catch (IOException ignore) {
+                    }
+                }
             }
             jar.closeEntry();
         }
index ba552e3..a12268c 100755 (executable)
@@ -111,7 +111,19 @@ public class AboutPage extends UpdaterPage {
             } else {\r
                 sourceProp = new File(toolsdir, SdkConstants.FN_SOURCE_PROP);\r
             }\r
-            p.load(new FileInputStream(sourceProp));\r
+            FileInputStream fis = null;\r
+            try {\r
+                fis = new FileInputStream(sourceProp);\r
+                p.load(fis);\r
+            } finally {\r
+                if (fis != null) {\r
+                    try {\r
+                        fis.close();\r
+                    } catch (IOException ignore) {\r
+                    }\r
+                }\r
+            }\r
+\r
             String revision = p.getProperty(PkgProps.PKG_REVISION);\r
             if (revision != null) {\r
                 return revision;\r
index feb9295..3414d84 100644 (file)
@@ -178,7 +178,20 @@ public class MainWindow extends ApplicationWindow {
             } else {
                 sourceProp = new File(toolsdir, "source.properties"); //$NON-NLS-1$
             }
-            p.load(new FileInputStream(sourceProp));
+
+            FileInputStream fis = null;
+            try {
+                fis = new FileInputStream(sourceProp);
+                p.load(fis);
+            } finally {
+                if (fis != null) {
+                    try {
+                        fis.close();
+                    } catch (IOException ignore) {
+                    }
+                }
+            }
+
             String revision = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
             if (revision != null && revision.length() > 0) {
                 return revision;