src/share/classes/com/sun/java/util/jar/pack/PropMap.java

Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: XXX

@@ -120,31 +120,31 @@
         props.put(Pack200.Packer.EFFORT, "5");
 
         // Define certain attribute layouts by default.
         // Do this after the previous props are put in place,
         // to allow override if necessary.
-        InputStream propStr = null;
-        try {
             String propFile = "intrinsic.properties";
-            propStr = PackerImpl.class.getResourceAsStream(propFile);
-            props.load(new BufferedInputStream(propStr));
+
+        // TODO TWR: narrowed the scope of IOException handling
+        boolean propsLoaded = false;
+        try (InputStream propStr = PackerImpl.class.getResourceAsStream(propFile)) {
+            props.load(propStr);
+            propsLoaded = true;
+        } catch (IOException ee) {
+            // ignore exception if it came from the close()
+            if (!propsLoaded) {
+                throw new RuntimeException(ee);
+            }
+        }
+
             for (Map.Entry<Object, Object> e : props.entrySet()) {
                 String key = (String) e.getKey();
                 String val = (String) e.getValue();
                 if (key.startsWith("attribute.")) {
                     e.setValue(Attribute.normalizeLayoutString(val));
                 }
             }
-        } catch (IOException ee) {
-            throw new RuntimeException(ee);
-        } finally {
-            try {
-                if (propStr != null) {
-                    propStr.close();
-                }
-            } catch (IOException ignore) {}
-        }
 
         defaultProps = (new HashMap<>(props));  // shrink to fit
     }
 
     PropMap() {