diff --git a/src/share/classes/com/sun/java/util/jar/pack/BandStructure.java b/src/share/classes/com/sun/java/util/jar/pack/BandStructure.java index 473d51108c0bf875c06fb19dcc32671774508822..6984a2ba35076fd356aa3358d8a9e0601e2174b5 100644 --- a/src/share/classes/com/sun/java/util/jar/pack/BandStructure.java +++ b/src/share/classes/com/sun/java/util/jar/pack/BandStructure.java @@ -743,24 +743,24 @@ class BandStructure { 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); + } + try (OutputStream ds = getDumpStream(this, ".bnd")) { + bandCoding.writeArrayTo(ds, values, 0, length); } - printArrayTo(ps, values, 0, length); - ps.close(); - OutputStream ds = getDumpStream(this, ".bnd"); - bandCoding.writeArrayTo(ds, values, 0, length); - ds.close(); } /** Disburse one value. */ @@ -829,12 +829,12 @@ class BandStructure { 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 { diff --git a/src/share/classes/com/sun/java/util/jar/pack/Driver.java b/src/share/classes/com/sun/java/util/jar/pack/Driver.java index 36885a86ed2327eb639cfffbef7e0984a3bba0d0..2194a731f35f903e4354feed957374bae3efe6fd 100644 --- a/src/share/classes/com/sun/java/util/jar/pack/Driver.java +++ b/src/share/classes/com/sun/java/util/jar/pack/Driver.java @@ -150,12 +150,12 @@ class Driver { // 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 me : fileProps.entrySet()) { engProps.put((String) me.getKey(), (String) me.getValue()); } @@ -348,10 +348,10 @@ class Driver { 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 @@ class Driver { 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 @@ class Driver { } } return ""; - } finally { - in.close(); } } diff --git a/src/share/classes/com/sun/java/util/jar/pack/NativeUnpack.java b/src/share/classes/com/sun/java/util/jar/pack/NativeUnpack.java index baa70b6786309e9472e98c10c0f1054a8ec5ce2b..44792a6006508535f29027481e84c1cf2cf9eab4 100644 --- a/src/share/classes/com/sun/java/util/jar/pack/NativeUnpack.java +++ b/src/share/classes/com/sun/java/util/jar/pack/NativeUnpack.java @@ -241,9 +241,9 @@ class NativeUnpack { 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. } diff --git a/src/share/classes/com/sun/java/util/jar/pack/PackageReader.java b/src/share/classes/com/sun/java/util/jar/pack/PackageReader.java index d676ee8e6fb27f27adb4e89124e67d373de56703..be9b485d78a2ca7d466f33f1926f3632b7b1d22f 100644 --- a/src/share/classes/com/sun/java/util/jar/pack/PackageReader.java +++ b/src/share/classes/com/sun/java/util/jar/pack/PackageReader.java @@ -540,9 +540,9 @@ class PackageReader extends BandStructure { 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 @@ class PackageReader extends BandStructure { 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(); diff --git a/src/share/classes/com/sun/java/util/jar/pack/PackageWriter.java b/src/share/classes/com/sun/java/util/jar/pack/PackageWriter.java index f531fb7d999fe62dcb0288da284f98dab25cb23d..e2bad8904a10f92c37c0fcb071b8ce19dd0fdcab 100644 --- a/src/share/classes/com/sun/java/util/jar/pack/PackageWriter.java +++ b/src/share/classes/com/sun/java/util/jar/pack/PackageWriter.java @@ -458,9 +458,9 @@ class PackageWriter extends BandStructure { 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 @@ class PackageWriter extends BandStructure { } }); 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 { diff --git a/src/share/classes/com/sun/java/util/jar/pack/PropMap.java b/src/share/classes/com/sun/java/util/jar/pack/PropMap.java index afed905b274c509572a31dcb612a9271c7bce20c..9656e55c9dea21d78dc69da588ce2d433c4e0d25 100644 --- a/src/share/classes/com/sun/java/util/jar/pack/PropMap.java +++ b/src/share/classes/com/sun/java/util/jar/pack/PropMap.java @@ -122,26 +122,23 @@ final class PropMap implements SortedMap { // 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 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 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 diff --git a/src/share/classes/com/sun/java/util/jar/pack/UnpackerImpl.java b/src/share/classes/com/sun/java/util/jar/pack/UnpackerImpl.java index 06bec826ff9def504e9d977bab48592d40ca92c9..377705ccb259366b99eea5871b1dd35ce2803dd9 100644 --- a/src/share/classes/com/sun/java/util/jar/pack/UnpackerImpl.java +++ b/src/share/classes/com/sun/java/util/jar/pack/UnpackerImpl.java @@ -161,8 +161,9 @@ public class UnpackerImpl extends TLGlobals implements Pack200.Unpacker { } // 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(); } diff --git a/src/share/classes/com/sun/java/util/jar/pack/Utils.java b/src/share/classes/com/sun/java/util/jar/pack/Utils.java index 4b5c4c87817a9698596c6490205897abec698174..7cf2cef00e53d5e9f62225ad6f61ed27481812f6 100644 --- a/src/share/classes/com/sun/java/util/jar/pack/Utils.java +++ b/src/share/classes/com/sun/java/util/jar/pack/Utils.java @@ -268,18 +268,18 @@ class Utils { // 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