From 796b6c0491833587ba096a33bd0d34329f6213c5 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 13 Oct 2011 18:47:02 -0700 Subject: Code cleanup: make sure FileInputStreams are closed. 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 --- .../ddmuilib/log/event/BugReportImporter.java | 23 ++++++++++++++-------- .../ddmuilib/log/event/EventLogImporter.java | 13 ++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) (limited to 'ddms/libs') diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/BugReportImporter.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/BugReportImporter.java index 9de1ac7..da41e70 100644 --- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/BugReportImporter.java +++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/BugReportImporter.java @@ -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 content = new ArrayList(); 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()]); } - + } diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/EventLogImporter.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/EventLogImporter.java index a1303f6..011bcf1 100644 --- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/EventLogImporter.java +++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/EventLogImporter.java @@ -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) { + } + } } } -- cgit v1.1