[Jython-checkins] jython (2.5): Use finally for resource closings.

frank.wierzbicki jython-checkins at python.org
Sun Jan 1 23:00:01 CET 2012


http://hg.python.org/jython/rev/abcb98e5b619
changeset:   6293:abcb98e5b619
branch:      2.5
parent:      6291:958ecf3eb8f0
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Sun Jan 01 13:32:26 2012 -0800
summary:
  Use finally for resource closings.

files:
  src/org/python/core/packagecache/CachedJarsPackageManager.java |  76 +++++++--
  1 files changed, 56 insertions(+), 20 deletions(-)


diff --git a/src/org/python/core/packagecache/CachedJarsPackageManager.java b/src/org/python/core/packagecache/CachedJarsPackageManager.java
--- a/src/org/python/core/packagecache/CachedJarsPackageManager.java
+++ b/src/org/python/core/packagecache/CachedJarsPackageManager.java
@@ -308,17 +308,22 @@
                     entry.mtime = mtime;
                 }
 
-                InputStream jarin;
-                if (jarconn == null) {
-                    jarin = new BufferedInputStream(
-                            new FileInputStream(jarfile));
-                } else {
-                    jarin = jarconn.getInputStream();
+                InputStream jarin = null;
+                try {
+                    if (jarconn == null) {
+                        jarin = new BufferedInputStream(
+                                new FileInputStream(jarfile));
+                    } else {
+                        jarin = jarconn.getInputStream();
+                    }
+
+                    zipPackages = getZipPackages(jarin);
+                } finally {
+                    if (jarin != null) {
+                        jarin.close();
+                    }
                 }
 
-                zipPackages = getZipPackages(jarin);
-                jarin.close();
-
                 if (caching) {
                     writeCacheFile(entry, jarcanon, zipPackages, brandNew);
                 }
@@ -357,8 +362,9 @@
 
         debug("reading cache, '" + jarcanon + "'");
 
+        DataInputStream istream = null;
         try {
-            DataInputStream istream = inOpenCacheFile(cachefile);
+            istream = inOpenCacheFile(cachefile);
             String old_jarcanon = istream.readUTF();
             long old_mtime = istream.readLong();
             if ((!old_jarcanon.equals(jarcanon)) || (old_mtime != mtime)) {
@@ -383,22 +389,30 @@
                     packs.put(packageName, classes);
                 }
             } catch (EOFException eof) {
-                ;
+                //ignore
             }
-            istream.close();
 
             return packs;
         } catch (IOException ioe) {
             // if (cachefile.exists()) cachefile.delete();
             return null;
+        } finally {
+            if (istream != null) {
+                try {
+                    istream.close();
+                } catch (IOException ignore) {
+                    //ignore
+                }
+            }
         }
     }
 
     // Write a cache file storing package info for a single .jar
     private void writeCacheFile(JarXEntry entry, String jarcanon,
             Map<String,String> zipPackages, boolean brandNew) {
+        DataOutputStream ostream = null;
         try {
-            DataOutputStream ostream = outCreateCacheFile(entry, brandNew);
+            ostream = outCreateCacheFile(entry, brandNew);
             ostream.writeUTF(jarcanon);
             ostream.writeLong(entry.mtime);
             comment("rewriting cachefile for '" + jarcanon + "'");
@@ -412,9 +426,16 @@
                     ostream.writeUTF(part);
                 }
             }
-            ostream.close();
         } catch (IOException ioe) {
             warning("can't write cache file for '" + jarcanon + "'");
+        } finally {
+            if (ostream != null) {
+                try {
+                    ostream.close();
+                } catch (IOException ignore) {
+                    //ignore
+                }
+            }
         }
     }
 
@@ -455,8 +476,9 @@
         this.indexModified = false;
         this.jarfiles = Generic.map();
 
+        DataInputStream istream = null;
         try {
-            DataInputStream istream = inOpenIndex();
+            istream = inOpenIndex();
             if (istream == null) {
                 return;
             }
@@ -469,13 +491,19 @@
                     this.jarfiles.put(jarcanon, new JarXEntry(cachefile, mtime));
                 }
             } catch (EOFException eof) {
-                ;
+                //ignore
             }
-            istream.close();
         } catch (IOException ioe) {
             warning("invalid index file");
+        } finally {
+            if (istream != null) {
+                try {
+                    istream.close();
+                } catch (IOException ignore) {
+                    //ignore
+                }
+            }
         }
-
     }
 
     /**
@@ -491,8 +519,9 @@
 
         comment("writing modified index file");
 
+        DataOutputStream ostream = null;
         try {
-            DataOutputStream ostream = outOpenIndex();
+            ostream = outOpenIndex();
             for (Entry<String,JarXEntry> entry : jarfiles.entrySet()) {
                 String jarcanon = entry.getKey();
                 JarXEntry xentry = entry.getValue();
@@ -500,9 +529,16 @@
                 ostream.writeUTF(xentry.cachefile);
                 ostream.writeLong(xentry.mtime);
             }
-            ostream.close();
         } catch (IOException ioe) {
             warning("can't write index file");
+        } finally {
+            if (ostream != null) {
+                try {
+                    ostream.close();
+                } catch (IOException ignore) {
+                    //ignore
+                }
+            }
         }
     }
 

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list