test/java/util/zip/ZipFile/ManyZipFiles.java
Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: alanb, dholmes, sherman
@@ -49,18 +49,18 @@
return;
}
// Create some zip data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ZipOutputStream zos = new ZipOutputStream(baos);
+ 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();
- zos.close();
+ }
byte[] data = baos.toByteArray();
ZipFile zips[] = new ZipFile[numFiles];
try {
@@ -88,27 +88,28 @@
// Create and then open a large number of zip files
for (int i = 0; i < numFiles; i++) {
File f = File.createTempFile("test", ".zip", tmpdir);
f.deleteOnExit();
- FileOutputStream fos = new FileOutputStream(f);
+ try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(data, 0, data.length);
- fos.close();
+ }
try {
zips[i] = new ZipFile(f);
} catch (Throwable t) {
fail("Failed to open zip file #" + i + " named "
+ zips[i].getName());
throw t;
}
}
} 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 {
zips[i].close();
} catch (Throwable t) {