changeset 3774:192bbf6cd5de

7022382: convert pack200 library code to use try-with-resources Reviewed-by: ksrini
author smarks
date Thu, 17 Mar 2011 18:50:06 -0700
parents bf37edb38fbb
children a76262ab5ea6
files src/share/classes/com/sun/java/util/jar/pack/BandStructure.java src/share/classes/com/sun/java/util/jar/pack/Driver.java src/share/classes/com/sun/java/util/jar/pack/NativeUnpack.java src/share/classes/com/sun/java/util/jar/pack/PackageReader.java src/share/classes/com/sun/java/util/jar/pack/PackageWriter.java src/share/classes/com/sun/java/util/jar/pack/PropMap.java src/share/classes/com/sun/java/util/jar/pack/UnpackerImpl.java src/share/classes/com/sun/java/util/jar/pack/Utils.java
diffstat 8 files changed, 107 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/java/util/jar/pack/BandStructure.java	Thu Mar 17 14:42:40 2011 -0700
+++ b/src/share/classes/com/sun/java/util/jar/pack/BandStructure.java	Thu Mar 17 18:50:06 2011 -0700
@@ -743,24 +743,24 @@
 
         private void dumpBand() throws IOException {
             assert(optDumpBands);
-            PrintStream ps = new PrintStream(getDumpStream(this, ".txt"));
-            String irr = (bandCoding == regularCoding) ? "" : " irregular";
-            ps.print("# length="+length+
-                     " size="+outputSize()+
-                     irr+" coding="+bandCoding);
-            if (metaCoding != noMetaCoding) {
-                StringBuffer sb = new StringBuffer();
-                for (int i = 0; i < metaCoding.length; i++) {
-                    if (i == 1)  sb.append(" /");
-                    sb.append(" ").append(metaCoding[i] & 0xFF);
+            try (PrintStream ps = new PrintStream(getDumpStream(this, ".txt"))) {
+                String irr = (bandCoding == regularCoding) ? "" : " irregular";
+                ps.print("# length="+length+
+                         " size="+outputSize()+
+                         irr+" coding="+bandCoding);
+                if (metaCoding != noMetaCoding) {
+                    StringBuffer sb = new StringBuffer();
+                    for (int i = 0; i < metaCoding.length; i++) {
+                        if (i == 1)  sb.append(" /");
+                        sb.append(" ").append(metaCoding[i] & 0xFF);
+                    }
+                    ps.print(" //header: "+sb);
                 }
-                ps.print(" //header: "+sb);
+                printArrayTo(ps, values, 0, length);
             }
-            printArrayTo(ps, values, 0, length);
-            ps.close();
-            OutputStream ds = getDumpStream(this, ".bnd");
-            bandCoding.writeArrayTo(ds, values, 0, length);
-            ds.close();
+            try (OutputStream ds = getDumpStream(this, ".bnd")) {
+                bandCoding.writeArrayTo(ds, values, 0, length);
+            }
         }
 
         /** Disburse one value. */
@@ -829,12 +829,12 @@
 
         private void dumpBand() throws IOException {
             assert(optDumpBands);
-            OutputStream ds = getDumpStream(this, ".bnd");
-            if (bytesForDump != null)
-                bytesForDump.writeTo(ds);
-            else
-                bytes.writeTo(ds);
-            ds.close();
+            try (OutputStream ds = getDumpStream(this, ".bnd")) {
+                if (bytesForDump != null)
+                    bytesForDump.writeTo(ds);
+                else
+                    bytes.writeTo(ds);
+            }
         }
 
         public void readDataFrom(InputStream in) throws IOException {
--- a/src/share/classes/com/sun/java/util/jar/pack/Driver.java	Thu Mar 17 14:42:40 2011 -0700
+++ b/src/share/classes/com/sun/java/util/jar/pack/Driver.java	Thu Mar 17 18:50:06 2011 -0700
@@ -150,12 +150,12 @@
                 // See if there is any other action to take.
                 if ("--config-file=".equals(state)) {
                     String propFile = av.remove(0);
-                    InputStream propIn = new FileInputStream(propFile);
                     Properties fileProps = new Properties();
-                    fileProps.load(new BufferedInputStream(propIn));
+                    try (InputStream propIn = new FileInputStream(propFile)) {
+                        fileProps.load(propIn);
+                    }
                     if (engProps.get(verboseProp) != null)
                         fileProps.list(System.out);
-                    propIn.close();
                     for (Map.Entry<Object,Object> me : fileProps.entrySet()) {
                         engProps.put((String) me.getKey(), (String) me.getValue());
                     }
@@ -348,10 +348,10 @@
                 else
                     fileOut = new FileOutputStream(outfile);
                 fileOut = new BufferedOutputStream(fileOut);
-                JarOutputStream out = new JarOutputStream(fileOut);
-                junpack.unpack(in, out);
-                //in.close();  // p200 closes in but not out
-                out.close();
+                try (JarOutputStream out = new JarOutputStream(fileOut)) {
+                    junpack.unpack(in, out);
+                    // p200 closes in but not out
+                }
                 // At this point, we have a good jarfile (or newfile, if -r)
             }
 
@@ -411,8 +411,7 @@
         long filelen = new File(jarfile).length();
         if (filelen <= 0)  return "";
         long skiplen = Math.max(0, filelen - tail.length);
-        InputStream in = new FileInputStream(new File(jarfile));
-        try {
+        try (InputStream in = new FileInputStream(new File(jarfile))) {
             in.skip(skiplen);
             in.read(tail);
             for (int i = tail.length-4; i >= 0; i--) {
@@ -426,8 +425,6 @@
                 }
             }
             return "";
-        } finally {
-            in.close();
         }
     }
 
--- a/src/share/classes/com/sun/java/util/jar/pack/NativeUnpack.java	Thu Mar 17 14:42:40 2011 -0700
+++ b/src/share/classes/com/sun/java/util/jar/pack/NativeUnpack.java	Thu Mar 17 18:50:06 2011 -0700
@@ -241,9 +241,9 @@
     void run(File inFile, JarOutputStream jstream) throws IOException {
         // %%% maybe memory-map the file, and pass it straight into unpacker
         ByteBuffer mappedFile = null;
-        FileInputStream fis = new FileInputStream(inFile);
-        run(fis, jstream, mappedFile);
-        fis.close();
+        try (FileInputStream fis = new FileInputStream(inFile)) {
+            run(fis, jstream, mappedFile);
+        }
         // Note:  caller is responsible to finish with jstream.
     }
 
--- a/src/share/classes/com/sun/java/util/jar/pack/PackageReader.java	Thu Mar 17 14:42:40 2011 -0700
+++ b/src/share/classes/com/sun/java/util/jar/pack/PackageReader.java	Thu Mar 17 18:50:06 2011 -0700
@@ -540,9 +540,9 @@
             Index index = initCPIndex(tag, cpMap);
 
             if (optDumpBands) {
-                PrintStream ps = new PrintStream(getDumpStream(index, ".idx"));
-                printArrayTo(ps, index.cpMap, 0, index.cpMap.length);
-                ps.close();
+                try (PrintStream ps = new PrintStream(getDumpStream(index, ".idx"))) {
+                    printArrayTo(ps, index.cpMap, 0, index.cpMap.length);
+                }
             }
         }
 
@@ -828,26 +828,27 @@
         attr_definition_headers.readFrom(in);
         attr_definition_name.readFrom(in);
         attr_definition_layout.readFrom(in);
-        PrintStream dump = !optDumpBands ? null
-            : new PrintStream(getDumpStream(attr_definition_headers, ".def"));
-        for (int i = 0; i < numAttrDefs; i++) {
-            int       header = attr_definition_headers.getByte();
-            Utf8Entry name   = (Utf8Entry) attr_definition_name.getRef();
-            Utf8Entry layout = (Utf8Entry) attr_definition_layout.getRef();
-            int       ctype  = (header &  ADH_CONTEXT_MASK);
-            int       index  = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
-            Attribute.Layout def = new Attribute.Layout(ctype,
-                                                        name.stringValue(),
-                                                        layout.stringValue());
-            // Check layout string for Java 6 extensions.
-            String pvLayout = def.layoutForPackageMajver(getPackageMajver());
-            if (!pvLayout.equals(def.layout())) {
-                throw new IOException("Bad attribute layout in version 150 archive: "+def.layout());
+        try (PrintStream dump = !optDumpBands ? null
+                 : new PrintStream(getDumpStream(attr_definition_headers, ".def")))
+        {
+            for (int i = 0; i < numAttrDefs; i++) {
+                int       header = attr_definition_headers.getByte();
+                Utf8Entry name   = (Utf8Entry) attr_definition_name.getRef();
+                Utf8Entry layout = (Utf8Entry) attr_definition_layout.getRef();
+                int       ctype  = (header &  ADH_CONTEXT_MASK);
+                int       index  = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
+                Attribute.Layout def = new Attribute.Layout(ctype,
+                                                            name.stringValue(),
+                                                            layout.stringValue());
+                // Check layout string for Java 6 extensions.
+                String pvLayout = def.layoutForPackageMajver(getPackageMajver());
+                if (!pvLayout.equals(def.layout())) {
+                    throw new IOException("Bad attribute layout in version 150 archive: "+def.layout());
+                }
+                this.setAttributeLayoutIndex(def, index);
+                if (dump != null)  dump.println(index+" "+def);
             }
-            this.setAttributeLayoutIndex(def, index);
-            if (dump != null)  dump.println(index+" "+def);
         }
-        if (dump != null)  dump.close();
         attr_definition_headers.doneDisbursing();
         attr_definition_name.doneDisbursing();
         attr_definition_layout.doneDisbursing();
--- a/src/share/classes/com/sun/java/util/jar/pack/PackageWriter.java	Thu Mar 17 14:42:40 2011 -0700
+++ b/src/share/classes/com/sun/java/util/jar/pack/PackageWriter.java	Thu Mar 17 18:50:06 2011 -0700
@@ -458,9 +458,9 @@
                 Utils.log.info("Writing "+cpMap.length+" "+ConstantPool.tagName(tag)+" entries...");
 
             if (optDumpBands) {
-                PrintStream ps = new PrintStream(getDumpStream(index, ".idx"));
-                printArrayTo(ps, cpMap, 0, cpMap.length);
-                ps.close();
+                try (PrintStream ps = new PrintStream(getDumpStream(index, ".idx"))) {
+                    printArrayTo(ps, cpMap, 0, cpMap.length);
+                }
             }
 
             switch (tag) {
@@ -923,33 +923,34 @@
             }
         });
         attrDefsWritten = new Attribute.Layout[numAttrDefs];
-        PrintStream dump = !optDumpBands ? null
-            : new PrintStream(getDumpStream(attr_definition_headers, ".def"));
-        int[] indexForDebug = Arrays.copyOf(attrIndexLimit, ATTR_CONTEXT_LIMIT);
-        for (int i = 0; i < defs.length; i++) {
-            int header = ((Integer)defs[i][0]).intValue();
-            Attribute.Layout def = (Attribute.Layout) defs[i][1];
-            attrDefsWritten[i] = def;
-            assert((header & ADH_CONTEXT_MASK) == def.ctype());
-            attr_definition_headers.putByte(header);
-            attr_definition_name.putRef(ConstantPool.getUtf8Entry(def.name()));
-            String layout = def.layoutForPackageMajver(getPackageMajver());
-            attr_definition_layout.putRef(ConstantPool.getUtf8Entry(layout));
-            // Check that we are transmitting that correct attribute index:
-            boolean debug = false;
-            assert(debug = true);
-            if (debug) {
-                int hdrIndex = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
-                if (hdrIndex < 0)  hdrIndex = indexForDebug[def.ctype()]++;
-                int realIndex = (attrIndexTable.get(def)).intValue();
-                assert(hdrIndex == realIndex);
-            }
-            if (dump != null) {
-                int index = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
-                dump.println(index+" "+def);
+        try (PrintStream dump = !optDumpBands ? null
+                 : new PrintStream(getDumpStream(attr_definition_headers, ".def")))
+        {
+            int[] indexForDebug = Arrays.copyOf(attrIndexLimit, ATTR_CONTEXT_LIMIT);
+            for (int i = 0; i < defs.length; i++) {
+                int header = ((Integer)defs[i][0]).intValue();
+                Attribute.Layout def = (Attribute.Layout) defs[i][1];
+                attrDefsWritten[i] = def;
+                assert((header & ADH_CONTEXT_MASK) == def.ctype());
+                attr_definition_headers.putByte(header);
+                attr_definition_name.putRef(ConstantPool.getUtf8Entry(def.name()));
+                String layout = def.layoutForPackageMajver(getPackageMajver());
+                attr_definition_layout.putRef(ConstantPool.getUtf8Entry(layout));
+                // Check that we are transmitting that correct attribute index:
+                boolean debug = false;
+                assert(debug = true);
+                if (debug) {
+                    int hdrIndex = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
+                    if (hdrIndex < 0)  hdrIndex = indexForDebug[def.ctype()]++;
+                    int realIndex = (attrIndexTable.get(def)).intValue();
+                    assert(hdrIndex == realIndex);
+                }
+                if (dump != null) {
+                    int index = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
+                    dump.println(index+" "+def);
+                }
             }
         }
-        if (dump != null)  dump.close();
     }
 
     void writeAttrCounts() throws IOException {
--- a/src/share/classes/com/sun/java/util/jar/pack/PropMap.java	Thu Mar 17 14:42:40 2011 -0700
+++ b/src/share/classes/com/sun/java/util/jar/pack/PropMap.java	Thu Mar 17 18:50:06 2011 -0700
@@ -122,26 +122,23 @@
         // 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));
-            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));
-                }
+        String propFile = "intrinsic.properties";
+
+        try (InputStream propStr = PackerImpl.class.getResourceAsStream(propFile)) {
+            if (propStr == null) {
+                throw new RuntimeException(propFile + " cannot be loaded");
             }
+            props.load(propStr);
         } catch (IOException ee) {
             throw new RuntimeException(ee);
-        } finally {
-            try {
-                if (propStr != null) {
-                    propStr.close();
-                }
-            } catch (IOException ignore) {}
+        }
+
+        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));
+            }
         }
 
         defaultProps = (new HashMap<>(props));  // shrink to fit
--- a/src/share/classes/com/sun/java/util/jar/pack/UnpackerImpl.java	Thu Mar 17 14:42:40 2011 -0700
+++ b/src/share/classes/com/sun/java/util/jar/pack/UnpackerImpl.java	Thu Mar 17 18:50:06 2011 -0700
@@ -161,8 +161,9 @@
         }
         // Use the stream-based implementation.
         // %%% Reconsider if native unpacker learns to memory-map the file.
-        FileInputStream instr = new FileInputStream(in);
-        unpack(instr, out);
+        try (FileInputStream instr = new FileInputStream(in)) {
+            unpack(instr, out);
+        }
         if (props.getBoolean(Utils.UNPACK_REMOVE_PACKFILE)) {
             in.delete();
         }
--- a/src/share/classes/com/sun/java/util/jar/pack/Utils.java	Thu Mar 17 14:42:40 2011 -0700
+++ b/src/share/classes/com/sun/java/util/jar/pack/Utils.java	Thu Mar 17 18:50:06 2011 -0700
@@ -268,18 +268,18 @@
         // 4947205 : Peformance is slow when using pack-effort=0
         out = new BufferedOutputStream(out);
         out = new NonCloser(out); // protect from JarOutputStream.close()
-        JarOutputStream jout = new JarOutputStream(out);
-        copyJarFile(in, jout);
-        jout.close();
+        try (JarOutputStream jout = new JarOutputStream(out)) {
+            copyJarFile(in, jout);
+        }
     }
     static void copyJarFile(JarFile in, OutputStream out) throws IOException {
 
         // 4947205 : Peformance is slow when using pack-effort=0
         out = new BufferedOutputStream(out);
         out = new NonCloser(out); // protect from JarOutputStream.close()
-        JarOutputStream jout = new JarOutputStream(out);
-        copyJarFile(in, jout);
-        jout.close();
+        try (JarOutputStream jout = new JarOutputStream(out)) {
+            copyJarFile(in, jout);
+        }
     }
         // Wrapper to prevent closing of client-supplied stream.
     static private