more checks to make sure files get closed in Directory.java
[org.ibex.js.git] / src / org / ibex / js / Directory.java
index e9f7b95..17adab7 100644 (file)
@@ -76,10 +76,13 @@ public class Directory extends JS {
                 }
             } else {
                 OutputStream out = new FileOutputStream(f2);
-                Writer w = new OutputStreamWriter(out);
-                w.write(toString(val));
-                w.flush();
-                out.close();
+                try {
+                    Writer w = new OutputStreamWriter(out);
+                    w.write(toString(val));
+                    w.flush();
+                } finally {
+                    out.close();
+                }
             }
         } catch (IOException ioe) {
             throw new JSExn.IO(ioe);
@@ -87,6 +90,7 @@ public class Directory extends JS {
     }
 
     public Object get(Object key0) throws JSExn {
+        FileInputStream fis = null;
         try {
             if (key0 == null) return null;
             String key = toString(key0);
@@ -95,7 +99,8 @@ public class Directory extends JS {
             if (f2.isDirectory()) return new Directory(f2);
             char[] chars = new char[((int)f2.length()) * 2];
             int numchars = 0;
-            Reader r = new InputStreamReader(new FileInputStream(f2));
+            fis = new FileInputStream(f2);
+            Reader r = new InputStreamReader(fis);
             while(true) {
                 int numread = r.read(chars, numchars, chars.length - numchars);
                 if (numread == -1) return new String(chars, 0, numchars);
@@ -103,6 +108,12 @@ public class Directory extends JS {
             }
         } catch (IOException ioe) {
             throw new JSExn.IO(ioe);
+        } finally {
+            try {
+                if (fis != null) fis.close();
+            } catch (IOException ioe) {
+                throw new JSExn.IO(ioe);
+            }
         }
     }