From d111fd7138333e05149dc2bfd486f65d73af60ec Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 12 Sep 2004 02:08:42 +0000 Subject: [PATCH] more checks to make sure files get closed in Directory.java darcs-hash:20040912020842-5007d-cec5f1640643fc49af71701903c7bdb17e2f9d9d.gz --- src/org/ibex/js/Directory.java | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/org/ibex/js/Directory.java b/src/org/ibex/js/Directory.java index e9f7b95..17adab7 100644 --- a/src/org/ibex/js/Directory.java +++ b/src/org/ibex/js/Directory.java @@ -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); + } } } -- 1.7.10.4