6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open
Summary: Close zipfile and io stream when done
Reviewed-by: alanb
--- a/jdk/test/ProblemList.txt Thu Jun 17 17:49:59 2010 +0100
+++ b/jdk/test/ProblemList.txt Thu Jun 17 13:21:46 2010 -0700
@@ -1205,17 +1205,5 @@
# Need to be marked othervm, or changed to be samevm safe
java/util/WeakHashMap/GCDuringIteration.java generic-all
-# Possible missing input stream close()? Causes samevm issues on windows
-java/util/zip/InfoZip.java generic-all
-
-# Missing a close() on file Test0.zip? windows samevm cascade problem
-java/util/zip/ZipFile/Comment.java generic-all
-
-# Suspect missing close() on bad*.zip files, windows cascade errors with samevm
-java/util/zip/ZipFile/CorruptedZipFiles.java generic-all
-
-# Should be samevm but causes problems with samevm, no details:
-java/util/zip/ZipFile/ManyEntries.java generic-all
-
############################################################################
--- a/jdk/test/java/util/zip/InfoZip.java Thu Jun 17 17:49:59 2010 +0100
+++ b/jdk/test/java/util/zip/InfoZip.java Thu Jun 17 13:21:46 2010 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -103,19 +103,25 @@
os.close();
ZipFile zf = new ZipFile(f);
- Enumeration<? extends ZipEntry> entries = zf.entries();
- ZipEntry ze = entries.nextElement();
- check(! entries.hasMoreElements());
- checkZipEntry(ze, contents(zf, ze));
- zf.close();
+ ZipEntry ze = null;
+ try {
+ Enumeration<? extends ZipEntry> entries = zf.entries();
+ ze = entries.nextElement();
+ check(! entries.hasMoreElements());
+ checkZipEntry(ze, contents(zf, ze));
+ } finally {
+ zf.close();
+ }
ZipInputStream is = new ZipInputStream(new FileInputStream(f));
- ze = is.getNextEntry();
- checkZipEntry(ze, contents(is));
- check(is.getNextEntry() == null);
-
+ try {
+ 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);
if (failed > 0) throw new Exception("Some tests failed");
}
--- a/jdk/test/java/util/zip/ZipFile/Comment.java Thu Jun 17 17:49:59 2010 +0100
+++ b/jdk/test/java/util/zip/ZipFile/Comment.java Thu Jun 17 13:21:46 2010 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -58,13 +58,16 @@
throws IOException
{
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(name));
- zos.setComment(comment);
- ZipEntry ze = new ZipEntry(entryName);
- ze.setMethod(ZipEntry.DEFLATED);
- zos.putNextEntry(ze);
- new DataOutputStream(zos).writeUTF(entryContents);
- zos.closeEntry();
- zos.close();
+ try {
+ 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();
+ }
}
private static void verifyZipFile(String name, String comment)
@@ -91,6 +94,8 @@
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");
}
--- a/jdk/test/java/util/zip/ZipFile/CorruptedZipFiles.java Thu Jun 17 17:49:59 2010 +0100
+++ b/jdk/test/java/util/zip/ZipFile/CorruptedZipFiles.java Thu Jun 17 13:21:46 2010 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -47,13 +47,14 @@
}
public static void main(String[] args) throws Exception {
- ZipOutputStream zos = new ZipOutputStream(
- new FileOutputStream("x.zip"));
- ZipEntry e = new ZipEntry("x");
- zos.putNextEntry(e);
- zos.write((int)'x');
- zos.close();
- zos = null;
+ ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("x.zip"));
+ try {
+ 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];
@@ -153,12 +154,15 @@
fos.write(data);
fos.close();
ZipFile zf = new ZipFile(zipName);
- if (getInputStream) {
- InputStream is = zf.getInputStream(new ZipEntry("x"));
- is.read();
+ try {
+ if (getInputStream) {
+ InputStream is = zf.getInputStream(new ZipEntry("x"));
+ is.read();
+ }
+ } finally {
+ zf.close();
}
fail("Failed to throw expected ZipException");
- zf.close();
} catch (ZipException e) {
if (e.getMessage().matches(msgPattern))
passed++;
--- a/jdk/test/java/util/zip/ZipFile/ManyEntries.java Thu Jun 17 17:49:59 2010 +0100
+++ b/jdk/test/java/util/zip/ZipFile/ManyEntries.java Thu Jun 17 13:21:46 2010 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -55,52 +55,58 @@
File zipFile = new File(++uniquifier + ".zip");
try {
zipFile.delete();
- ZipOutputStream zos =
- new ZipOutputStream(
- new BufferedOutputStream(
- new FileOutputStream(zipFile)));
- for (int i = 0; i < N; i++) {
- ZipEntry e = new ZipEntry("DIR/"+i);
- e.setMethod(method);
- e.setTime(time);
- if (method == ZipEntry.STORED) {
- e.setSize(1);
- crc32.reset();
- crc32.update((byte)i);
- e.setCrc(crc32.getValue());
- } else {
- e.setSize(0);
- e.setCrc(0);
+ ZipOutputStream zos = new ZipOutputStream(
+ new BufferedOutputStream(
+ new FileOutputStream(zipFile)));
+ try {
+ for (int i = 0; i < N; i++) {
+ ZipEntry e = new ZipEntry("DIR/"+i);
+ e.setMethod(method);
+ e.setTime(time);
+ if (method == ZipEntry.STORED) {
+ e.setSize(1);
+ crc32.reset();
+ crc32.update((byte)i);
+ e.setCrc(crc32.getValue());
+ } else {
+ e.setSize(0);
+ e.setCrc(0);
+ }
+ zos.putNextEntry(e);
+ zos.write(i);
}
- zos.putNextEntry(e);
- zos.write(i);
+ } finally {
+ zos.close();
+ zos = null;
}
- zos.close();
- zos = null;
- ZipFile zip = new ZipFile(zipFile);
- if (! (zip.size() == N))
- throw new Exception("Bad ZipFile size: " + zip.size());
- Enumeration entries = zip.entries();
+ ZipFile zip = zip = new ZipFile(zipFile);
+ try {
+ if (! (zip.size() == N))
+ throw new Exception("Bad ZipFile size: " + zip.size());
+ Enumeration entries = zip.entries();
- for (int i = 0; i < N; i++) {
- if (i % 1000 == 0) {System.gc(); System.runFinalization();}
- if (! (entries.hasMoreElements()))
- throw new Exception("only " + i + " elements");
- ZipEntry e = (ZipEntry)entries.nextElement();
- if (! (e.getSize() == 1))
- throw new Exception("bad size: " + e.getSize());
- if (! (e.getName().equals("DIR/" + i)))
- throw new Exception("bad name: " + i);
- if (! (e.getMethod() == method))
- throw new Exception("getMethod="+e.getMethod()+", method=" + method);
- if (! (e.getTime() == time))
- throw new Exception("getTime="+e.getTime()+", time=" + time);
- if (! (zip.getInputStream(e).read() == (i & 0xff)))
- throw new Exception("Bad file contents: " + i);
+ for (int i = 0; i < N; i++) {
+ if (i % 1000 == 0) {System.gc(); System.runFinalization();}
+ if (! (entries.hasMoreElements()))
+ throw new Exception("only " + i + " elements");
+ ZipEntry e = (ZipEntry)entries.nextElement();
+ if (! (e.getSize() == 1))
+ throw new Exception("bad size: " + e.getSize());
+ if (! (e.getName().equals("DIR/" + i)))
+ throw new Exception("bad name: " + i);
+ if (! (e.getMethod() == method))
+ throw new Exception("getMethod="+e.getMethod()+", method=" + method);
+ if (! (e.getTime() == time))
+ throw new Exception("getTime="+e.getTime()+", time=" + time);
+ if (! (zip.getInputStream(e).read() == (i & 0xff)))
+ throw new Exception("Bad file contents: " + i);
+ }
+ if (entries.hasMoreElements())
+ throw new Exception("too many elements");
+ } finally {
+ zip.close();
}
- if (entries.hasMoreElements())
- throw new Exception("too many elements");
}
finally {
zipFile.delete();