--- old/src/share/classes/com/sun/java/util/jar/pack/BandStructure.java 2011-02-23 19:49:30.179752892 -0800 +++ new/src/share/classes/com/sun/java/util/jar/pack/BandStructure.java 2011-02-23 19:49:29.823725421 -0800 @@ -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); + } + 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 @@ 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 { --- old/src/share/classes/com/sun/java/util/jar/pack/Driver.java 2011-02-23 19:49:31.811751269 -0800 +++ new/src/share/classes/com/sun/java/util/jar/pack/Driver.java 2011-02-23 19:49:31.483725119 -0800 @@ -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 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(); } } --- old/src/share/classes/com/sun/java/util/jar/pack/NativeUnpack.java 2011-02-23 19:49:33.355949076 -0800 +++ new/src/share/classes/com/sun/java/util/jar/pack/NativeUnpack.java 2011-02-23 19:49:33.035725037 -0800 @@ -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. } --- old/src/share/classes/com/sun/java/util/jar/pack/PackageReader.java 2011-02-23 19:49:34.940155047 -0800 +++ new/src/share/classes/com/sun/java/util/jar/pack/PackageReader.java 2011-02-23 19:49:34.619726524 -0800 @@ -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(); + } // TODO TWR: this relies on new null treatment for TWR's close() attr_definition_headers.doneDisbursing(); attr_definition_name.doneDisbursing(); attr_definition_layout.doneDisbursing(); --- old/src/share/classes/com/sun/java/util/jar/pack/PackageWriter.java 2011-02-23 19:49:36.488617360 -0800 +++ new/src/share/classes/com/sun/java/util/jar/pack/PackageWriter.java 2011-02-23 19:49:36.179760432 -0800 @@ -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(); + } // TODO TWR: relies on new null handling behavior of TWR close() } void writeAttrCounts() throws IOException { --- old/src/share/classes/com/sun/java/util/jar/pack/PropMap.java 2011-02-23 19:49:38.032375447 -0800 +++ new/src/share/classes/com/sun/java/util/jar/pack/PropMap.java 2011-02-23 19:49:37.719727257 -0800 @@ -122,26 +122,26 @@ // 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"; + + // 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) { - throw new RuntimeException(ee); - } finally { - try { - if (propStr != null) { - propStr.close(); - } - } catch (IOException ignore) {} + // ignore exception if it came from the close() + if (!propsLoaded) { + throw new RuntimeException(ee); + } + } + + 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 --- old/src/share/classes/com/sun/java/util/jar/pack/UnpackerImpl.java 2011-02-23 19:49:39.523954262 -0800 +++ new/src/share/classes/com/sun/java/util/jar/pack/UnpackerImpl.java 2011-02-23 19:49:39.207762605 -0800 @@ -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(); } --- old/src/share/classes/com/sun/java/util/jar/pack/Utils.java 2011-02-23 19:49:41.000426685 -0800 +++ new/src/share/classes/com/sun/java/util/jar/pack/Utils.java 2011-02-23 19:49:40.688279675 -0800 @@ -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 --- old/src/share/classes/java/util/jar/JarFile.java 2011-02-23 19:49:42.504087486 -0800 +++ new/src/share/classes/java/util/jar/JarFile.java 2011-02-23 19:49:42.183735521 -0800 @@ -376,9 +376,9 @@ */ private byte[] getBytes(ZipEntry ze) throws IOException { byte[] b = new byte[(int)ze.getSize()]; - DataInputStream is = new DataInputStream(super.getInputStream(ze)); - is.readFully(b, 0, b.length); - is.close(); + try (DataInputStream is = new DataInputStream(super.getInputStream(ze))) { + is.readFully(b, 0, b.length); + } return b; } @@ -480,10 +480,10 @@ JarEntry manEntry = getManEntry(); if (manEntry != null) { byte[] b = new byte[(int)manEntry.getSize()]; - DataInputStream dis = new DataInputStream( - super.getInputStream(manEntry)); - dis.readFully(b, 0, b.length); - dis.close(); + try (DataInputStream dis = new DataInputStream( + super.getInputStream(manEntry))) { + dis.readFully(b, 0, b.length); + } int last = b.length - src.length; int i = 0; --- old/test/java/util/jar/JarEntry/GetMethodsReturnClones.java 2011-02-23 19:49:44.051860892 -0800 +++ new/test/java/util/jar/JarEntry/GetMethodsReturnClones.java 2011-02-23 19:49:43.723738942 -0800 @@ -40,22 +40,21 @@ System.getProperty("file.separator"); public static void main(String[] args) throws Exception { - JarFile jf = new JarFile(BASE + "test.jar", true); - - byte[] buffer = new byte[8192]; - Enumeration e = jf.entries(); List entries = new ArrayList(); - while (e.hasMoreElements()) { - JarEntry je = e.nextElement(); - entries.add(je); - InputStream is = jf.getInputStream(je); - while (is.read(buffer, 0, buffer.length) != -1) { - // we just read. this will throw a SecurityException - // if a signature/digest check fails. + try (JarFile jf = new JarFile(BASE + "test.jar", true)) { + byte[] buffer = new byte[8192]; + Enumeration e = jf.entries(); + while (e.hasMoreElements()) { + JarEntry je = e.nextElement(); + entries.add(je); + try (InputStream is = jf.getInputStream(je)) { + while (is.read(buffer, 0, buffer.length) != -1) { + // we just read. this will throw a SecurityException + // if a signature/digest check fails. + } + } } - is.close(); } - jf.close(); for (JarEntry je : entries) { Certificate[] certs = je.getCertificates(); --- old/test/java/util/jar/JarFile/ScanSignedJar.java 2011-02-23 19:49:45.556443879 -0800 +++ new/test/java/util/jar/JarFile/ScanSignedJar.java 2011-02-23 19:49:45.227723807 -0800 @@ -37,25 +37,25 @@ public class ScanSignedJar { public static void main(String[] args) throws Exception { - JarFile file = new JarFile(new File(System.getProperty("test.src","."), - "bogus-signerinfo-attr.jar")); - byte[] buffer = new byte[8192]; boolean isSigned = false; + try (JarFile file = new JarFile(new File(System.getProperty("test.src","."), + "bogus-signerinfo-attr.jar"))) { + byte[] buffer = new byte[8192]; - for (Enumeration entries = file.entries(); entries.hasMoreElements();) { - JarEntry entry = (JarEntry) entries.nextElement(); - InputStream jis = file.getInputStream(entry); - while (jis.read(buffer, 0, buffer.length) != -1) { - // read the jar entry + for (Enumeration entries = file.entries(); entries.hasMoreElements();) { + JarEntry entry = (JarEntry) entries.nextElement(); + try (InputStream jis = file.getInputStream(entry)) { + while (jis.read(buffer, 0, buffer.length) != -1) { + // read the jar entry + } + } + if (entry.getCertificates() != null) { + isSigned = true; + } + System.out.println((isSigned ? "[signed] " : "\t ") + + entry.getName()); } - jis.close(); - if (entry.getCertificates() != null) { - isSigned = true; - } - System.out.println((isSigned ? "[signed] " : "\t ") + - entry.getName()); } - file.close(); if (isSigned) { System.out.println("\nJAR file has signed entries"); --- old/test/java/util/zip/Available.java 2011-02-23 19:49:47.048110694 -0800 +++ new/test/java/util/zip/Available.java 2011-02-23 19:49:46.727725483 -0800 @@ -44,14 +44,14 @@ File f = new File(System.getProperty("test.src", "."), "input.jar"); // test ZipInputStream - ZipInputStream z = new ZipInputStream(new FileInputStream(f)); - z.getNextEntry(); - tryAvail(z); + try (ZipInputStream z = new ZipInputStream(new FileInputStream(f))) { + z.getNextEntry(); + tryAvail(z); + } // test InflaterInputStream ZipFile zfile = new ZipFile(f); tryAvail(zfile.getInputStream(zfile.getEntry("Available.java"))); - z.close(); } static void tryAvail(InputStream in) throws Exception { --- old/test/java/util/zip/FileBuilder.java 2011-02-23 19:49:48.548131087 -0800 +++ new/test/java/util/zip/FileBuilder.java 2011-02-23 19:49:48.224016072 -0800 @@ -53,25 +53,24 @@ filetype.equals("SlightlyCompressible"))) usageError(); - RandomAccessFile raf = new RandomAccessFile(filename, "rw"); - - if (filetype.equals("SlightlyCompressible")) { - byte[] randomBytes = new byte[16384]; - byte[] nullBytes = new byte[randomBytes.length/10]; - Random rand = new Random(); - for (int i = 0; raf.length() < filesize; ++i) { - rand.nextBytes(randomBytes); - raf.write(nullBytes); - raf.write(randomBytes); + try (RandomAccessFile raf = new RandomAccessFile(filename, "rw")) { + if (filetype.equals("SlightlyCompressible")) { + byte[] randomBytes = new byte[16384]; + byte[] nullBytes = new byte[randomBytes.length/10]; + Random rand = new Random(); + for (int i = 0; raf.length() < filesize; ++i) { + rand.nextBytes(randomBytes); + raf.write(nullBytes); + raf.write(randomBytes); + } } - } - // Make sure file is exactly the requested size, and that - // a unique identifying trailer is written. - byte[] filenameBytes = filename.getBytes("UTF8"); - raf.seek(filesize-filenameBytes.length); - raf.write(filenameBytes); - raf.setLength(filesize); - raf.close(); + // Make sure file is exactly the requested size, and that + // a unique identifying trailer is written. + byte[] filenameBytes = filename.getBytes("UTF8"); + raf.seek(filesize-filenameBytes.length); + raf.write(filenameBytes); + raf.setLength(filesize); + } } } --- old/test/java/util/zip/GZIP/Accordion.java 2011-02-23 19:49:50.216022874 -0800 +++ new/test/java/util/zip/GZIP/Accordion.java 2011-02-23 19:49:49.888049809 -0800 @@ -64,16 +64,13 @@ System.out.println("count="+count); Thread compressor = new Thread() { public void run() { - try { - final GZIPOutputStream s = new GZIPOutputStream(out); + try (GZIPOutputStream s = new GZIPOutputStream(out)) { for (long i = 0; i < count; i++) s.write(data, 0, data.length); - s.close(); } catch (Throwable t) { trouble = t; }}}; Thread uncompressor = new Thread() { public void run() { - try { - final GZIPInputStream s = new GZIPInputStream(in); + try (GZIPInputStream s = new GZIPInputStream(in)) { final byte[] maybeBytes = new byte[data.length]; for (long i = 0; i < count; i++) { readFully(s, maybeBytes); @@ -82,7 +79,6 @@ } if (s.read(maybeBytes, 0, 1) > 0) throw new Exception("Unexpected NON-EOF"); - s.close(); } catch (Throwable t) { trouble = t; }}}; compressor.start(); uncompressor.start(); --- old/test/java/util/zip/GZIP/GZIPInputStreamRead.java 2011-02-23 19:49:51.712357882 -0800 +++ new/test/java/util/zip/GZIP/GZIPInputStreamRead.java 2011-02-23 19:49:51.383723841 -0800 @@ -44,9 +44,9 @@ rnd.nextBytes(src); srcBAOS.write(src); - GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS); - gzos.write(src); - gzos.close(); + try (GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS)) { + gzos.write(src); + } } byte[] srcBytes = srcBAOS.toByteArray(); byte[] dstBytes = dstBAOS.toByteArray(); @@ -75,26 +75,27 @@ int readBufSize, int gzisBufSize) throws Throwable { - GZIPInputStream gzis = new GZIPInputStream( - new ByteArrayInputStream(dst), - gzisBufSize); - byte[] result = new byte[src.length + 10]; - byte[] buf = new byte[readBufSize]; - int n = 0; - int off = 0; + try (GZIPInputStream gzis = new GZIPInputStream( + new ByteArrayInputStream(dst), + gzisBufSize)) + { + byte[] result = new byte[src.length + 10]; + byte[] buf = new byte[readBufSize]; + int n = 0; + int off = 0; - while ((n = gzis.read(buf, 0, buf.length)) != -1) { - System.arraycopy(buf, 0, result, off, n); - off += n; - // no range check, if overflow, let it fail - } - if (off != src.length || gzis.available() != 0 || - !Arrays.equals(src, Arrays.copyOf(result, off))) { - throw new RuntimeException( - "GZIPInputStream reading failed! " + - ", src.len=" + src.length + - ", read=" + off); + while ((n = gzis.read(buf, 0, buf.length)) != -1) { + System.arraycopy(buf, 0, result, off, n); + off += n; + // no range check, if overflow, let it fail + } + if (off != src.length || gzis.available() != 0 || + !Arrays.equals(src, Arrays.copyOf(result, off))) { + throw new RuntimeException( + "GZIPInputStream reading failed! " + + ", src.len=" + src.length + + ", read=" + off); + } } - gzis.close(); } } --- old/test/java/util/zip/InflateIn_DeflateOut.java 2011-02-23 19:49:53.228188111 -0800 +++ new/test/java/util/zip/InflateIn_DeflateOut.java 2011-02-23 19:49:52.887724071 -0800 @@ -134,14 +134,14 @@ PairedOutputStream pos = new PairedOutputStream(pis); pis.setPairedOutputStream(pos); - DeflaterOutputStream dos = new DeflaterOutputStream(pos, true); byte[] data = new byte[random.nextInt(1024 * 1024)]; byte[] buf = new byte[data.length]; random.nextBytes(data); - dos.write(data); - dos.close(); + try (DeflaterOutputStream dos = new DeflaterOutputStream(pos, true)) { + dos.write(data); + } check(readFully(iis, buf, buf.length)); check(Arrays.equals(data, buf)); } --- old/test/java/util/zip/InfoZip.java 2011-02-23 19:49:54.715746023 -0800 +++ new/test/java/util/zip/InfoZip.java 2011-02-23 19:49:54.387724071 -0800 @@ -85,41 +85,37 @@ //---------------------------------------------------------------- File f = new File("InfoZip.zip"); - OutputStream os = new FileOutputStream(f); - os.write(new byte[] - {'P', 'K', 3, 4, 10, 0, 0, 0, 0, 0, -68, 8, 'k', - '2', 'V', -7, 'm', 9, 20, 0, 0, 0, 20, 0, 0, 0, - 8, 0, 21, 0, 's', 'o', 'm', 'e', 'F', 'i', 'l', 'e', 'U', - 'T', 9, 0, 3, 't', '_', '1', 'B', 't', '_', '1', 'B', 'U', - 'x', 4, 0, -14, 'v', 26, 4, 'M', 'e', 's', 's', 'a', 'g', - 'e', ' ', 'i', 'n', ' ', 'a', ' ', 'B', 'o', 't', 't', 'l', 'e', - 10, 'P', 'K', 1, 2, 23, 3, 10, 0, 0, 0, 0, 0, - -68, 8, 'k', '2', 'V', -7, 'm', 9, 20, 0, 0, 0, 20, - 0, 0, 0, 8, 0, 13, 0, 0, 0, 0, 0, 1, 0, - 0, 0, -92, -127, 0, 0, 0, 0, 's', 'o', 'm', 'e', 'F', - 'i', 'l', 'e', 'U', 'T', 5, 0, 3, 't', '_', '1', 'B', 'U', - 'x', 0, 0, 'P', 'K', 5, 6, 0, 0, 0, 0, 1, 0, - 1, 0, 'C', 0, 0, 0, 'O', 0, 0, 0, 0, 0, }); - os.close(); + try (OutputStream os = new FileOutputStream(f)) { + os.write(new byte[] + {'P', 'K', 3, 4, 10, 0, 0, 0, 0, 0, -68, 8, 'k', + '2', 'V', -7, 'm', 9, 20, 0, 0, 0, 20, 0, 0, 0, + 8, 0, 21, 0, 's', 'o', 'm', 'e', 'F', 'i', 'l', 'e', 'U', + 'T', 9, 0, 3, 't', '_', '1', 'B', 't', '_', '1', 'B', 'U', + 'x', 4, 0, -14, 'v', 26, 4, 'M', 'e', 's', 's', 'a', 'g', + 'e', ' ', 'i', 'n', ' ', 'a', ' ', 'B', 'o', 't', 't', 'l', 'e', + 10, 'P', 'K', 1, 2, 23, 3, 10, 0, 0, 0, 0, 0, + -68, 8, 'k', '2', 'V', -7, 'm', 9, 20, 0, 0, 0, 20, + 0, 0, 0, 8, 0, 13, 0, 0, 0, 0, 0, 1, 0, + 0, 0, -92, -127, 0, 0, 0, 0, 's', 'o', 'm', 'e', 'F', + 'i', 'l', 'e', 'U', 'T', 5, 0, 3, 't', '_', '1', 'B', 'U', + 'x', 0, 0, 'P', 'K', 5, 6, 0, 0, 0, 0, 1, 0, + 1, 0, 'C', 0, 0, 0, 'O', 0, 0, 0, 0, 0, }); + } - ZipFile zf = new ZipFile(f); ZipEntry ze = null; - try { + try (ZipFile zf = new ZipFile(f)) { Enumeration entries = zf.entries(); ze = entries.nextElement(); check(! entries.hasMoreElements()); checkZipEntry(ze, contents(zf, ze)); - } finally { - zf.close(); } - ZipInputStream is = new ZipInputStream(new FileInputStream(f)); - try { + try (FileInputStream fis = new FileInputStream(f); + ZipInputStream is = new ZipInputStream(fis)) + { ze = is.getNextEntry(); checkZipEntry(ze, contents(is)); check(is.getNextEntry() == null); - } finally { - is.close(); } f.delete(); System.out.printf("passed = %d, failed = %d%n", passed, failed); --- old/test/java/util/zip/LargeZip.java 2011-02-23 19:49:56.195996859 -0800 +++ new/test/java/util/zip/LargeZip.java 2011-02-23 19:49:55.867724877 -0800 @@ -98,19 +98,21 @@ } data = baos.toByteArray(); - ZipOutputStream zos = new ZipOutputStream( - new BufferedOutputStream(new FileOutputStream(largeFile))); - long length = 0; - while (length < fileSize) { - ZipEntry ze = new ZipEntry("entry-" + length); - lastEntryName = ze.getName(); - zos.putNextEntry(ze); - zos.write(data, 0, data.length); - zos.closeEntry(); - length = largeFile.length(); + try (FileOutputStream fos = new FileOutputStream(largeFile); + BufferedOutputStream bos = new BufferedOutputStream(fos); + ZipOutputStream zos = new ZipOutputStream(bos)) + { + long length = 0; + while (length < fileSize) { + ZipEntry ze = new ZipEntry("entry-" + length); + lastEntryName = ze.getName(); + zos.putNextEntry(ze); + zos.write(data, 0, data.length); + zos.closeEntry(); + length = largeFile.length(); + } + System.out.println("Last entry written is " + lastEntryName); } - System.out.println("Last entry written is " + lastEntryName); - zos.close(); } static void readLargeZip1() throws Throwable { @@ -143,33 +145,35 @@ static void readLargeZip2() throws Throwable { - ZipInputStream zis = new ZipInputStream( - new BufferedInputStream(new FileInputStream(largeFile))); - ZipEntry entry = null; - String entryName = null; - int count = 0; - while ((entry = zis.getNextEntry()) != null) { - entryName = entry.getName(); - if (entryName.equals(lastEntryName)) { - break; + try (FileInputStream fis = new FileInputStream(largeFile); + BufferedInputStream bis = new BufferedInputStream(fis); + ZipInputStream zis = new ZipInputStream(bis)) + { + ZipEntry entry = null; + String entryName = null; + int count = 0; + while ((entry = zis.getNextEntry()) != null) { + entryName = entry.getName(); + if (entryName.equals(lastEntryName)) { + break; + } + count++; } - count++; - } - System.out.println("Number of entries read: " + count); - System.out.println("Last entry read is " + entryName); - check(!entry.isDirectory()); + System.out.println("Number of entries read: " + count); + System.out.println("Last entry read is " + entryName); + check(!entry.isDirectory()); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte buf[] = new byte[4096]; - int len; - while ((len = zis.read(buf)) >= 0) { - baos.write(buf, 0, len); + byte buf[] = new byte[4096]; + int len; + while ((len = zis.read(buf)) >= 0) { + baos.write(buf, 0, len); + } + baos.close(); + check(Arrays.equals(data, baos.toByteArray())); + check(zis.getNextEntry() == null); } - baos.close(); - check(Arrays.equals(data, baos.toByteArray())); - check(zis.getNextEntry() == null); - zis.close(); } --- old/test/java/util/zip/TestEmptyZip.java 2011-02-23 19:49:57.688471319 -0800 +++ new/test/java/util/zip/TestEmptyZip.java 2011-02-23 19:49:57.351727309 -0800 @@ -78,67 +78,49 @@ pass(); } } - ZipInputStream zis = null; - try { - zis = new ZipInputStream(new FileInputStream(f)); + try (FileInputStream fis = new FileInputStream(f); + ZipInputStream zis = new ZipInputStream(fis)) + { ZipEntry ze = zis.getNextEntry(); check(ze == null); } catch (IOException ex) { unexpected(ex); - } finally { - if (zis != null) zis.close(); } } static void write(File f) throws Exception { - ZipOutputStream zos = null; - try { - zos = new ZipOutputStream(new FileOutputStream(f)); + try (FileOutputStream fis = new FileOutputStream(f); + ZipOutputStream zos = new ZipOutputStream(fis)) + { zos.finish(); - zos.close(); pass(); } catch (Exception ex) { unexpected(ex); - } finally { - if (zos != null) { - zos.close(); - } } } static void readFile(File f) throws Exception { - ZipFile zf = null; - try { - zf = new ZipFile(f); + try (ZipFile zf = new ZipFile(f)) { Enumeration e = zf.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry) e.nextElement(); fail(); } - zf.close(); pass(); } catch (Exception ex) { unexpected(ex); - } finally { - if (zf != null) { - zf.close(); - } } } static void readStream(File f) throws Exception { - ZipInputStream zis = null; - try { - zis = new ZipInputStream(new FileInputStream(f)); + try (FileInputStream fis = new FileInputStream(f); + ZipInputStream zis = new ZipInputStream(fis)) + { ZipEntry ze = zis.getNextEntry(); check(ze == null); byte[] buf = new byte[1024]; check(zis.read(buf, 0, 1024) == -1); - } finally { - if (zis != null) { - zis.close(); - } } } --- old/test/java/util/zip/ZipCoding.java 2011-02-23 19:49:59.216094114 -0800 +++ new/test/java/util/zip/ZipCoding.java 2011-02-23 19:49:58.887727144 -0800 @@ -57,59 +57,58 @@ String name, String comment, byte[] bb) throws Exception { - ZipInputStream zis = new ZipInputStream(is, cs); - ZipEntry e = zis.getNextEntry(); - if (e == null || ! name.equals(e.getName())) - throw new RuntimeException("ZipIS name doesn't match!"); - byte[] bBuf = new byte[bb.length << 1]; - int n = zis.read(bBuf, 0, bBuf.length); - if (n != bb.length || - !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) { - throw new RuntimeException("ZipIS content doesn't match!"); + try (ZipInputStream zis = new ZipInputStream(is, cs)) { + ZipEntry e = zis.getNextEntry(); + if (e == null || ! name.equals(e.getName())) + throw new RuntimeException("ZipIS name doesn't match!"); + byte[] bBuf = new byte[bb.length << 1]; + int n = zis.read(bBuf, 0, bBuf.length); + if (n != bb.length || + !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) { + throw new RuntimeException("ZipIS content doesn't match!"); + } } - zis.close(); } static void testZipFile(File f, Charset cs, String name, String comment, byte[] bb) throws Exception { - ZipFile zf = new ZipFile(f, cs); - Enumeration zes = zf.entries(); - ZipEntry e = (ZipEntry)zes.nextElement(); - if (! name.equals(e.getName()) || - ! comment.equals(e.getComment())) - throw new RuntimeException("ZipFile: name/comment doesn't match!"); - InputStream is = zf.getInputStream(e); - if (is == null) - throw new RuntimeException("ZipFile: getIS failed!"); - byte[] bBuf = new byte[bb.length << 1]; - int n = 0; - int nn =0; - while ((nn = is.read(bBuf, n, bBuf.length-n)) != -1) { - n += nn; - } - if (n != bb.length || - !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) { - throw new RuntimeException("ZipFile content doesn't match!"); + try (ZipFile zf = new ZipFile(f, cs)) { + Enumeration zes = zf.entries(); + ZipEntry e = (ZipEntry)zes.nextElement(); + if (! name.equals(e.getName()) || + ! comment.equals(e.getComment())) + throw new RuntimeException("ZipFile: name/comment doesn't match!"); + InputStream is = zf.getInputStream(e); + if (is == null) + throw new RuntimeException("ZipFile: getIS failed!"); + byte[] bBuf = new byte[bb.length << 1]; + int n = 0; + int nn =0; + while ((nn = is.read(bBuf, n, bBuf.length-n)) != -1) { + n += nn; + } + if (n != bb.length || + !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) { + throw new RuntimeException("ZipFile content doesn't match!"); + } } - zf.close(); } static void test(String csn, String name, String comment) throws Exception { - byte[] bb = "This is the conent of the zipfile".getBytes("ISO-8859-1"); + byte[] bb = "This is the content of the zipfile".getBytes("ISO-8859-1"); Charset cs = Charset.forName(csn); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream(baos, cs); - - ZipEntry e = new ZipEntry(name); - e.setComment(comment); - zos.putNextEntry(e); - zos.write(bb, 0, bb.length); - zos.closeEntry(); - zos.close(); + try (ZipOutputStream zos = new ZipOutputStream(baos, cs)) { + ZipEntry e = new ZipEntry(name); + e.setComment(comment); + zos.putNextEntry(e); + zos.write(bb, 0, bb.length); + zos.closeEntry(); + } ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); testZipInputStream(bis, cs, name, comment, bb); @@ -121,9 +120,9 @@ File f = new File(new File(System.getProperty("test.dir", ".")), "zfcoding.zip"); - FileOutputStream fos = new FileOutputStream(f); - baos.writeTo(fos); - fos.close(); + try (FileOutputStream fos = new FileOutputStream(f)) { + baos.writeTo(fos); + } testZipFile(f, cs, name, comment, bb); if ("utf-8".equals(csn)) { testZipFile(f, Charset.forName("MS932"), name, comment, bb); --- old/test/java/util/zip/ZipFile/Assortment.java 2011-02-23 19:50:00.715789047 -0800 +++ new/test/java/util/zip/ZipFile/Assortment.java 2011-02-23 19:50:00.392091668 -0800 @@ -201,13 +201,12 @@ //---------------------------------------------------------------- // Write zip file using ZipOutputStream //---------------------------------------------------------------- - ZipOutputStream zos = new ZipOutputStream( - new FileOutputStream(zipName)); - - for (Entry e : entries) - e.write(zos); - - zos.close(); + try (FileOutputStream fos = new FileOutputStream(zipName); + ZipOutputStream zos = new ZipOutputStream(fos)) + { + for (Entry e : entries) + e.write(zos); + } //---------------------------------------------------------------- // Verify zip file contents using JarFile class --- old/test/java/util/zip/ZipFile/Comment.java 2011-02-23 19:50:02.207765399 -0800 +++ new/test/java/util/zip/ZipFile/Comment.java 2011-02-23 19:50:01.879804349 -0800 @@ -57,16 +57,15 @@ private static void writeZipFile(String name, String comment) throws IOException { - ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(name)); - try { + try (FileOutputStream fos = new FileOutputStream(name); + ZipOutputStream zos = new ZipOutputStream(fos)) + { zos.setComment(comment); ZipEntry ze = new ZipEntry(entryName); ze.setMethod(ZipEntry.DEFLATED); zos.putNextEntry(ze); new DataOutputStream(zos).writeUTF(entryContents); zos.closeEntry(); - } finally { - zos.close(); } } @@ -74,30 +73,30 @@ throws Exception { // Check that Zip entry was correctly written. - ZipFile zipFile = new ZipFile(name); - ZipEntry zipEntry = zipFile.getEntry(entryName); - InputStream is = zipFile.getInputStream(zipEntry); - String result = new DataInputStream(is).readUTF(); - if (!result.equals(entryContents)) - throw new Exception("Entry contents corrupted"); + try (ZipFile zipFile = new ZipFile(name)) { + ZipEntry zipEntry = zipFile.getEntry(entryName); + InputStream is = zipFile.getInputStream(zipEntry); + String result = new DataInputStream(is).readUTF(); + if (!result.equals(entryContents)) + throw new Exception("Entry contents corrupted"); + } - // Check that comment length was correctly written. - RandomAccessFile file = new RandomAccessFile(name, "r"); - file.seek(file.length() - comment.length() - - ZipFile.ENDHDR + ZipFile.ENDCOM); - int b1 = file.readUnsignedByte(); - int b2 = file.readUnsignedByte(); - if (b1 + (b2 << 8) != comment.length()) - throw new Exception("Zip file comment length corrupted"); + try (RandomAccessFile file = new RandomAccessFile(name, "r")) { + // Check that comment length was correctly written. + file.seek(file.length() - comment.length() + - ZipFile.ENDHDR + ZipFile.ENDCOM); + int b1 = file.readUnsignedByte(); + int b2 = file.readUnsignedByte(); + if (b1 + (b2 << 8) != comment.length()) + throw new Exception("Zip file comment length corrupted"); - // Check that comment was correctly written. - file.seek(file.length() - comment.length()); - byte [] bytes = new byte [comment.length()]; - file.readFully(bytes); - zipFile.close(); - file.close(); - if (! comment.equals(new String(bytes, "UTF8"))) - throw new Exception("Zip file comment corrupted"); + // Check that comment was correctly written. + file.seek(file.length() - comment.length()); + byte [] bytes = new byte [comment.length()]; + file.readFully(bytes); + if (! comment.equals(new String(bytes, "UTF8"))) + throw new Exception("Zip file comment corrupted"); + } } private static String buildComment(int length) { --- old/test/java/util/zip/ZipFile/CopyJar.java 2011-02-23 19:50:03.712421299 -0800 +++ new/test/java/util/zip/ZipFile/CopyJar.java 2011-02-23 19:50:03.380222279 -0800 @@ -31,18 +31,18 @@ public class CopyJar { public static void main(String args[]) throws Exception { - ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), - "input.jar")); - ZipEntry ze = zf.getEntry("ReleaseInflater.java"); - ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream()); - InputStream in = zf.getInputStream(ze); - byte[] b = new byte[128]; - int n; - zos.putNextEntry(ze); - while((n = in.read(b)) != -1) { - zos.write(b, 0, n); + try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), + "input.jar"))) { + ZipEntry ze = zf.getEntry("ReleaseInflater.java"); + ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream()); + InputStream in = zf.getInputStream(ze); + byte[] b = new byte[128]; + int n; + zos.putNextEntry(ze); + while((n = in.read(b)) != -1) { + zos.write(b, 0, n); + } + zos.close(); } - zos.close(); - zf.close(); } } --- old/test/java/util/zip/ZipFile/CorruptedZipFiles.java 2011-02-23 19:50:05.236450337 -0800 +++ new/test/java/util/zip/ZipFile/CorruptedZipFiles.java 2011-02-23 19:50:04.911739699 -0800 @@ -47,21 +47,19 @@ } public static void main(String[] args) throws Exception { - ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("x.zip")); - try { + try (FileOutputStream fos = new FileOutputStream("x.zip"); + ZipOutputStream zos = new ZipOutputStream(fos)) + { ZipEntry e = new ZipEntry("x"); zos.putNextEntry(e); zos.write((int)'x'); - } finally { - zos.close(); } int len = (int)(new File("x.zip").length()); byte[] good = new byte[len]; - FileInputStream fis = new FileInputStream("x.zip"); - fis.read(good); - fis.close(); - fis = null; + try (FileInputStream fis = new FileInputStream("x.zip")) { + fis.read(good); + } new File("x.zip").delete(); int endpos = len - ENDHDR; @@ -150,17 +148,14 @@ boolean getInputStream) { String zipName = "bad" + (uniquifier++) + ".zip"; try { - FileOutputStream fos = new FileOutputStream(zipName); - fos.write(data); - fos.close(); - ZipFile zf = new ZipFile(zipName); - try { + try (FileOutputStream fos = new FileOutputStream(zipName)) { + fos.write(data); + } + try (ZipFile zf = new ZipFile(zipName)) { if (getInputStream) { InputStream is = zf.getInputStream(new ZipEntry("x")); is.read(); } - } finally { - zf.close(); } fail("Failed to throw expected ZipException"); } catch (ZipException e) { @@ -170,8 +165,7 @@ unexpected(e); } catch (Throwable t) { unexpected(t); - } - finally { + } finally { new File(zipName).delete(); } } --- old/test/java/util/zip/ZipFile/DeleteTempJar.java 2011-02-23 19:50:06.692094687 -0800 +++ new/test/java/util/zip/ZipFile/DeleteTempJar.java 2011-02-23 19:50:06.399729511 -0800 @@ -53,34 +53,34 @@ { final File zf = File.createTempFile("deletetemp", ".jar"); zf.deleteOnExit(); - JarOutputStream jos = new JarOutputStream( - new FileOutputStream(zf)); - JarEntry je = new JarEntry("entry"); - jos.putNextEntry(je); - jos.write("hello, world".getBytes("ASCII")); - jos.close(); + try (FileOutputStream fos = new FileOutputStream(zf); + JarOutputStream jos = new JarOutputStream(fos)) + { + JarEntry je = new JarEntry("entry"); + jos.putNextEntry(je); + jos.write("hello, world".getBytes("ASCII")); + } HttpServer server = HttpServer.create( new InetSocketAddress((InetAddress) null, 0), 0); HttpContext context = server.createContext("/", new HttpHandler() { public void handle(HttpExchange e) { - try { - FileInputStream fis = new FileInputStream(zf); - e.sendResponseHeaders(200, zf.length()); - OutputStream os = e.getResponseBody(); - byte[] buf = new byte[1024]; - int count = 0; - while ((count = fis.read(buf)) != -1) { - os.write(buf, 0, count); + try (FileInputStream fis = new FileInputStream(zf)) { + e.sendResponseHeaders(200, zf.length()); + OutputStream os = e.getResponseBody(); + byte[] buf = new byte[1024]; + int count = 0; + while ((count = fis.read(buf)) != -1) { + os.write(buf, 0, count); + } + } catch (Exception ex) { + unexpected(ex); + } finally { + e.close(); } - fis.close(); - e.close(); - } catch (Exception ex) { - unexpected(ex); } - } - }); + }); server.start(); URL url = new URL("jar:http://localhost:" --- old/test/java/util/zip/ZipFile/EnumAfterClose.java 2011-02-23 19:50:08.211900562 -0800 +++ new/test/java/util/zip/ZipFile/EnumAfterClose.java 2011-02-23 19:50:07.860156316 -0800 @@ -33,10 +33,12 @@ public class EnumAfterClose { public static void main(String args[]) throws Exception { - ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), - "input.zip")); - Enumeration e = zf.entries(); - zf.close(); + Enumeration e; + try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), + "input.zip"))) { + e = zf.entries(); + } + // ensure that the ZipFile is closed before checking the Enumeration try { if (e.hasMoreElements()) { ZipEntry ze = (ZipEntry)e.nextElement(); --- old/test/java/util/zip/ZipFile/GetDirEntry.java 2011-02-23 19:50:09.628016968 -0800 +++ new/test/java/util/zip/ZipFile/GetDirEntry.java 2011-02-23 19:50:09.335724713 -0800 @@ -32,12 +32,12 @@ public class GetDirEntry { public static void main(String args[]) throws Exception { - ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), - "input.jar")); - ZipEntry ze = zf.getEntry("META-INF"); - if (ze == null) { - throw new Exception("failed to find a directory entry"); + try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), + "input.jar"))) { + ZipEntry ze = zf.getEntry("META-INF"); + if (ze == null) { + throw new Exception("failed to find a directory entry"); + } } - zf.close(); } } --- old/test/java/util/zip/ZipFile/LargeZipFile.java 2011-02-23 19:50:11.123812801 -0800 +++ new/test/java/util/zip/ZipFile/LargeZipFile.java 2011-02-23 19:50:10.803730695 -0800 @@ -93,51 +93,50 @@ baos.write(bb.array(), 0, DATA_SIZE); } data = baos.toByteArray(); - - ZipOutputStream zos = new ZipOutputStream( - new BufferedOutputStream(new FileOutputStream(largeFile))); - long length = 0; - while (length < fileSize) { - ZipEntry ze = new ZipEntry("entry-" + length); - lastEntryName = ze.getName(); - zos.putNextEntry(ze); - zos.write(data, 0, data.length); - zos.closeEntry(); - length = largeFile.length(); + try (FileOutputStream fos = new FileOutputStream(largeFile); + BufferedOutputStream bos = new BufferedOutputStream(fos); + ZipOutputStream zos = new ZipOutputStream(bos)) + { + long length = 0; + while (length < fileSize) { + ZipEntry ze = new ZipEntry("entry-" + length); + lastEntryName = ze.getName(); + zos.putNextEntry(ze); + zos.write(data, 0, data.length); + zos.closeEntry(); + length = largeFile.length(); + } + System.out.println("Last entry written is " + lastEntryName); } - System.out.println("Last entry written is " + lastEntryName); - zos.close(); } static void readLargeZip() throws Throwable { - ZipFile zipFile = new ZipFile(largeFile); - ZipEntry entry = null; - String entryName = null; - int count = 0; - Enumeration entries = zipFile.entries(); - while (entries.hasMoreElements()) { - entry = entries.nextElement(); - entryName = entry.getName(); - count++; - } - System.out.println("Number of entries read: " + count); - System.out.println("Last entry read is " + entryName); - check(!entry.isDirectory()); - if (check(entryName.equals(lastEntryName))) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - InputStream is = zipFile.getInputStream(entry); - byte buf[] = new byte[4096]; - int len; - while ((len = is.read(buf)) >= 0) { - baos.write(buf, 0, len); + try (ZipFile zipFile = new ZipFile(largeFile)) { + ZipEntry entry = null; + String entryName = null; + int count = 0; + Enumeration entries = zipFile.entries(); + while (entries.hasMoreElements()) { + entry = entries.nextElement(); + entryName = entry.getName(); + count++; + } + System.out.println("Number of entries read: " + count); + System.out.println("Last entry read is " + entryName); + check(!entry.isDirectory()); + if (check(entryName.equals(lastEntryName))) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + InputStream is = zipFile.getInputStream(entry); + byte buf[] = new byte[4096]; + int len; + while ((len = is.read(buf)) >= 0) { + baos.write(buf, 0, len); + } + baos.close(); + is.close(); + check(Arrays.equals(data, baos.toByteArray())); } - baos.close(); - is.close(); - check(Arrays.equals(data, baos.toByteArray())); } - try { - zipFile.close(); - } catch (IOException ioe) {/* what can you do */ } } //--------------------- Infrastructure --------------------------- --- old/test/java/util/zip/ZipFile/ManyEntries.java 2011-02-23 19:50:12.535760516 -0800 +++ new/test/java/util/zip/ZipFile/ManyEntries.java 2011-02-23 19:50:12.247724399 -0800 @@ -55,10 +55,10 @@ File zipFile = new File(++uniquifier + ".zip"); try { zipFile.delete(); - ZipOutputStream zos = new ZipOutputStream( - new BufferedOutputStream( - new FileOutputStream(zipFile))); - try { + try (FileOutputStream fos = new FileOutputStream(zipFile); + BufferedOutputStream bos = new BufferedOutputStream(fos); + ZipOutputStream zos = new ZipOutputStream(bos)) + { for (int i = 0; i < N; i++) { ZipEntry e = new ZipEntry("DIR/"+i); e.setMethod(method); @@ -75,13 +75,9 @@ zos.putNextEntry(e); zos.write(i); } - } finally { - zos.close(); - zos = null; } - ZipFile zip = zip = new ZipFile(zipFile); - try { + try (ZipFile zip = new ZipFile(zipFile)) { if (! (zip.size() == N)) throw new Exception("Bad ZipFile size: " + zip.size()); Enumeration entries = zip.entries(); @@ -104,11 +100,8 @@ } if (entries.hasMoreElements()) throw new Exception("too many elements"); - } finally { - zip.close(); } - } - finally { + } finally { zipFile.delete(); } } --- old/test/java/util/zip/ZipFile/ManyZipFiles.java 2011-02-23 19:50:14.047769868 -0800 +++ new/test/java/util/zip/ZipFile/ManyZipFiles.java 2011-02-23 19:50:13.751724909 -0800 @@ -51,14 +51,14 @@ // Create some zip data ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream(baos); - ZipEntry ze = new ZipEntry("test"); - zos.putNextEntry(ze); - byte[] hello = "hello, world".getBytes("ASCII"); - zos.write(hello, 0, hello.length); - zos.closeEntry(); - zos.finish(); - zos.close(); + try (ZipOutputStream zos = new ZipOutputStream(baos)) { + ZipEntry ze = new ZipEntry("test"); + zos.putNextEntry(ze); + byte[] hello = "hello, world".getBytes("ASCII"); + zos.write(hello, 0, hello.length); + zos.closeEntry(); + zos.finish(); + } byte[] data = baos.toByteArray(); ZipFile zips[] = new ZipFile[numFiles]; @@ -90,9 +90,9 @@ for (int i = 0; i < numFiles; i++) { File f = File.createTempFile("test", ".zip", tmpdir); f.deleteOnExit(); - FileOutputStream fos = new FileOutputStream(f); - fos.write(data, 0, data.length); - fos.close(); + try (FileOutputStream fos = new FileOutputStream(f)) { + fos.write(data, 0, data.length); + } try { zips[i] = new ZipFile(f); } catch (Throwable t) { @@ -102,11 +102,12 @@ } } } finally { - // This finally block is due to bug 4171239. On windows, if the + // This finally block is due to bug 4171239. On Windows, if the // file is still open at the end of the VM, deleteOnExit won't // take place. "new ZipFile(...)" opens the zip file, so we have - // to explicity close those opened above. This finally block can - // be removed when 4171239 is fixed. + // to explicitly close those opened above. This finally block can + // be removed when 4171239 is fixed. See also 6357433, against which + // 4171239 was closed as a duplicate. for (int i = 0; i < numFiles; i++) { if (zips[i] != null) { try { --- old/test/java/util/zip/ZipFile/ReadAfterClose.java 2011-02-23 19:50:15.552161769 -0800 +++ new/test/java/util/zip/ZipFile/ReadAfterClose.java 2011-02-23 19:50:15.247731661 -0800 @@ -34,10 +34,13 @@ public class ReadAfterClose { public static void main(String[] argv) throws Exception { - ZipFile zf = new ZipFile(new File(System.getProperty("test.src","."),"crash.jar")); - ZipEntry zent = zf.getEntry("Test.java"); - InputStream in = zf.getInputStream(zent); - zf.close(); + InputStream in; + try (ZipFile zf = new ZipFile( + new File(System.getProperty("test.src","."),"crash.jar"))) { + ZipEntry zent = zf.getEntry("Test.java"); + in = zf.getInputStream(zent); + } + // ensure zf is closed at this point try { in.read(); } catch (IOException e) { --- old/test/java/util/zip/ZipFile/ReadZip.java 2011-02-23 19:50:16.987790720 -0800 +++ new/test/java/util/zip/ZipFile/ReadZip.java 2011-02-23 19:50:16.691726204 -0800 @@ -27,6 +27,9 @@ */ import java.io.*; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; import java.util.zip.*; public class ReadZip { @@ -38,71 +41,62 @@ } public static void main(String args[]) throws Exception { - ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), - "input.zip")); + try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."), + "input.zip"))) { + // Make sure we throw NPE on null objects + try { unreached (zf.getEntry(null)); } + catch (NullPointerException e) {} - // Make sure we throw NPE on null objects - try { unreached (zf.getEntry(null)); } - catch (NullPointerException e) {} - - try { unreached (zf.getInputStream(null)); } - catch (NullPointerException e) {} - - ZipEntry ze = zf.getEntry("ReadZip.java"); - if (ze == null) { - throw new Exception("cannot read from zip file"); + try { unreached (zf.getInputStream(null)); } + catch (NullPointerException e) {} + + ZipEntry ze = zf.getEntry("ReadZip.java"); + if (ze == null) { + throw new Exception("cannot read from zip file"); + } } - zf.close(); // Make sure we can read the zip file that has some garbage // bytes padded at the end. - FileInputStream fis = new FileInputStream( - new File(System.getProperty("test.src", "."), - "input.zip")); - File newZip = new File(System.getProperty("test.dir", "."), - "input2.zip"); - FileOutputStream fos = new FileOutputStream(newZip); - - byte[] buf = new byte[1024]; - int n = 0; - while ((n = fis.read(buf)) != -1) { - fos.write(buf, 0, n); - } - fis.close(); + File newZip = new File(System.getProperty("test.dir", "."), "input2.zip"); + Files.copy(new File(System.getProperty("test.src", "."), "input.zip").toPath(), + newZip.toPath(), StandardCopyOption.REPLACE_EXISTING); + // pad some bytes - fos.write(1); fos.write(3); fos.write(5); fos.write(7); - fos.close(); - try { - zf = new ZipFile(newZip); - ze = zf.getEntry("ReadZip.java"); + try (OutputStream os = Files.newOutputStream(newZip.toPath(), + StandardOpenOption.APPEND)) { + os.write(1); os.write(3); os.write(5); os.write(7); + } + + try (ZipFile zf = new ZipFile(newZip)) { + ZipEntry ze = zf.getEntry("ReadZip.java"); if (ze == null) { throw new Exception("cannot read from zip file"); } } finally { - zf.close(); newZip.delete(); } // Read zip file comment try { + try (FileOutputStream fos = new FileOutputStream(newZip); + ZipOutputStream zos = new ZipOutputStream(fos)) + { + ZipEntry ze = new ZipEntry("ZipEntry"); + zos.putNextEntry(ze); + zos.write(1); zos.write(2); zos.write(3); zos.write(4); + zos.closeEntry(); + zos.setComment("This is the comment for testing"); + } - ZipOutputStream zos = new ZipOutputStream( - new FileOutputStream(newZip)); - ze = new ZipEntry("ZipEntry"); - zos.putNextEntry(ze); - zos.write(1); zos.write(2); zos.write(3); zos.write(4); - zos.closeEntry(); - zos.setComment("This is the comment for testing"); - zos.close(); - - zf = new ZipFile(newZip); - ze = zf.getEntry("ZipEntry"); - if (ze == null) - throw new Exception("cannot read entry from zip file"); - if (!"This is the comment for testing".equals(zf.getComment())) - throw new Exception("cannot read comment from zip file"); + try (ZipFile zf = new ZipFile(newZip)) { + ZipEntry ze = zf.getEntry("ZipEntry"); + if (ze == null) + throw new Exception("cannot read entry from zip file"); + if (!"This is the comment for testing".equals(zf.getComment())) + throw new Exception("cannot read comment from zip file"); + } } finally { - zf.close(); newZip.delete(); } --- old/test/java/util/zip/ZipFile/ShortRead.java 2011-02-23 19:50:18.399883162 -0800 +++ new/test/java/util/zip/ZipFile/ShortRead.java 2011-02-23 19:50:18.111728021 -0800 @@ -38,27 +38,29 @@ try { final String entryName = "abc"; final String data = "Data disponible"; - final ZipOutputStream zos = - new ZipOutputStream(new FileOutputStream(zFile)); - zos.putNextEntry(new ZipEntry(entryName)); - zos.write(data.getBytes("ASCII")); - zos.closeEntry(); - zos.close(); + try (FileOutputStream fos = new FileOutputStream(zFile); + ZipOutputStream zos = new ZipOutputStream(fos)) + { + zos.putNextEntry(new ZipEntry(entryName)); + zos.write(data.getBytes("ASCII")); + zos.closeEntry(); + } - final ZipFile zipFile = new ZipFile(zFile); - final ZipEntry zentry = zipFile.getEntry(entryName); - final InputStream inputStream = zipFile.getInputStream(zentry); - System.out.printf("size=%d csize=%d available=%d%n", - zentry.getSize(), - zentry.getCompressedSize(), - inputStream.available()); - byte[] buf = new byte[data.length()]; - final int count = inputStream.read(buf); - if (! new String(buf, "ASCII").equals(data) || - count != data.length()) - throw new Exception("short read?"); - zipFile.close(); + try (ZipFile zipFile = new ZipFile(zFile)) { + final ZipEntry zentry = zipFile.getEntry(entryName); + final InputStream inputStream = zipFile.getInputStream(zentry); + System.out.printf("size=%d csize=%d available=%d%n", + zentry.getSize(), + zentry.getCompressedSize(), + inputStream.available()); + byte[] buf = new byte[data.length()]; + final int count = inputStream.read(buf); + if (! new String(buf, "ASCII").equals(data) || + count != data.length()) + throw new Exception("short read?"); + } + } finally { + zFile.delete(); } - finally { zFile.delete(); } } } --- old/test/java/util/zip/zip.java 2011-02-23 19:50:19.855742905 -0800 +++ new/test/java/util/zip/zip.java 2011-02-23 19:50:19.532160895 -0800 @@ -322,57 +322,57 @@ void create(OutputStream out) throws IOException { - ZipOutputStream zos = new ZipOutputStream(out, cs); - if (flag0) { - zos.setMethod(ZipOutputStream.STORED); - } - for (File file: entries) { - addFile(zos, file); + try (ZipOutputStream zos = new ZipOutputStream(out, cs)) { + if (flag0) { + zos.setMethod(ZipOutputStream.STORED); + } + for (File file: entries) { + addFile(zos, file); + } } - zos.close(); } boolean update(InputStream in, OutputStream out) throws IOException { - ZipInputStream zis = new ZipInputStream(in, cs); - ZipOutputStream zos = new ZipOutputStream(out, cs); - ZipEntry e = null; - byte[] buf = new byte[1024]; - int n = 0; - boolean updateOk = true; + try (ZipInputStream zis = new ZipInputStream(in, cs); + ZipOutputStream zos = new ZipOutputStream(out, cs)) + { + ZipEntry e = null; + byte[] buf = new byte[1024]; + int n = 0; + boolean updateOk = true; - // put the old entries first, replace if necessary - while ((e = zis.getNextEntry()) != null) { - String name = e.getName(); - if (!entryMap.containsKey(name)) { // copy the old stuff - // do our own compression - ZipEntry e2 = new ZipEntry(name); - e2.setMethod(e.getMethod()); - e2.setTime(e.getTime()); - e2.setComment(e.getComment()); - e2.setExtra(e.getExtra()); - if (e.getMethod() == ZipEntry.STORED) { - e2.setSize(e.getSize()); - e2.setCrc(e.getCrc()); - } - zos.putNextEntry(e2); - while ((n = zis.read(buf, 0, buf.length)) != -1) { - zos.write(buf, 0, n); + // put the old entries first, replace if necessary + while ((e = zis.getNextEntry()) != null) { + String name = e.getName(); + if (!entryMap.containsKey(name)) { // copy the old stuff + // do our own compression + ZipEntry e2 = new ZipEntry(name); + e2.setMethod(e.getMethod()); + e2.setTime(e.getTime()); + e2.setComment(e.getComment()); + e2.setExtra(e.getExtra()); + if (e.getMethod() == ZipEntry.STORED) { + e2.setSize(e.getSize()); + e2.setCrc(e.getCrc()); + } + zos.putNextEntry(e2); + while ((n = zis.read(buf, 0, buf.length)) != -1) { + zos.write(buf, 0, n); + } + } else { // replace with the new files + File f = entryMap.get(name); + addFile(zos, f); + entryMap.remove(name); + entries.remove(f); } - } else { // replace with the new files - File f = entryMap.get(name); - addFile(zos, f); - entryMap.remove(name); - entries.remove(f); } - } - // add the remaining new files - for (File f: entries) { - addFile(zos, f); + // add the remaining new files + for (File f: entries) { + addFile(zos, f); + } } - zis.close(); - zos.close(); return updateOk; } @@ -517,25 +517,25 @@ } void extract(String fname, String files[]) throws IOException { - ZipFile zf = new ZipFile(fname, cs); - Set dirs = newDirSet(); - Enumeration zes = zf.entries(); - while (zes.hasMoreElements()) { - ZipEntry e = zes.nextElement(); - InputStream is; - if (files == null) { - dirs.add(extractFile(zf.getInputStream(e), e)); - } else { - String name = e.getName(); - for (String file : files) { - if (name.startsWith(file)) { - dirs.add(extractFile(zf.getInputStream(e), e)); - break; + try (ZipFile zf = new ZipFile(fname, cs)) { + Set dirs = newDirSet(); + Enumeration zes = zf.entries(); + while (zes.hasMoreElements()) { + ZipEntry e = zes.nextElement(); + InputStream is; + if (files == null) { + dirs.add(extractFile(zf.getInputStream(e), e)); + } else { + String name = e.getName(); + for (String file : files) { + if (name.startsWith(file)) { + dirs.add(extractFile(zf.getInputStream(e), e)); + break; + } } } } } - zf.close(); updateLastModifiedTime(dirs); } @@ -607,12 +607,12 @@ } void list(String fname, String files[]) throws IOException { - ZipFile zf = new ZipFile(fname, cs); - Enumeration zes = zf.entries(); - while (zes.hasMoreElements()) { - printEntry(zes.nextElement(), files); + try (ZipFile zf = new ZipFile(fname, cs)) { + Enumeration zes = zf.entries(); + while (zes.hasMoreElements()) { + printEntry(zes.nextElement(), files); + } } - zf.close(); } void printEntry(ZipEntry e, String[] files) throws IOException {