提交 d58c230b 编写于 作者: S sherman

6962067: TEST_BUG: Tests in java/util/zip/ZipFile leave file open

Summary: Close zipfile and io stream when done
Reviewed-by: alanb
上级 a830a508
......@@ -1205,17 +1205,5 @@ java/util/Locale/LocaleTest.java generic-all
# 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
############################################################################
/*
* 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 @@ public class InfoZip {
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");
}
......
/*
* 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 @@ public class Comment {
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 @@ public class Comment {
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");
}
......
/*
* 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 class CorruptedZipFiles {
}
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 @@ public class CorruptedZipFiles {
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++;
......
/*
* 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 @@ public class ManyEntries {
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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册