Browse Source

Fix potential file handle leak in TextFile

Change-Id: Ib89eccb575b2464af5ba1aa21f3e1d5827f6fdf9
Reviewed-on: http://gerrit.dmdirc.com/355
Reviewed-by: Gregory Holmes <greboid@dmdirc.com>
Tested-by: Gregory Holmes <greboid@dmdirc.com>
tags/0.6.3b1
Chris Smith 14 years ago
parent
commit
27fef67524
1 changed files with 19 additions and 11 deletions
  1. 19
    11
      src/com/dmdirc/util/TextFile.java

+ 19
- 11
src/com/dmdirc/util/TextFile.java View File

@@ -130,18 +130,26 @@ public class TextFile {
130 130
      * @throws IOException If an I/O exception occurs
131 131
      */
132 132
     public void readLines() throws IOException {
133
-        final BufferedReader reader = new BufferedReader(
134
-                new InputStreamReader(file == null ? is : new FileInputStream(file),
135
-                charset));
136
-        lines = new ArrayList<String>();
137
-        
138
-        String line;
139
-        
140
-        while ((line = reader.readLine()) != null) {
141
-            lines.add(line);
133
+        BufferedReader reader = null;
134
+        InputStreamReader inputReader = null;
135
+        InputStream inputStream = null;
136
+
137
+        try {
138
+            inputStream = file == null ? is : new FileInputStream(file);
139
+            inputReader = new InputStreamReader(inputStream, charset);
140
+            reader = new BufferedReader(inputReader);
141
+            lines = new ArrayList<String>();
142
+
143
+            String line;
144
+
145
+            while ((line = reader.readLine()) != null) {
146
+                lines.add(line);
147
+            }
148
+        } finally {
149
+            StreamUtil.close(reader);
150
+            StreamUtil.close(inputReader);
151
+            StreamUtil.close(inputStream);
142 152
         }
143
-        
144
-        reader.close();
145 153
     }
146 154
     
147 155
     /**

Loading…
Cancel
Save