test/java/util/zip/LargeZip.java

Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: alanb, dholmes, sherman

@@ -96,23 +96,25 @@
             bb.putDouble(0, Math.random());
             baos.write(bb.array(), 0, DATA_SIZE);
         }
         data = baos.toByteArray();
 
-        ZipOutputStream zos = new ZipOutputStream(
-            new BufferedOutputStream(new FileOutputStream(largeFile)));
+        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);
-        zos.close();
+        }
     }
 
     static void readLargeZip1() throws Throwable {
         ZipFile zipFile = new ZipFile(largeFile);
         ZipEntry entry = null;

@@ -141,12 +143,14 @@
         }
     }
 
 
     static void readLargeZip2() throws Throwable {
-        ZipInputStream zis = new ZipInputStream(
-            new BufferedInputStream(new FileInputStream(largeFile)));
+        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();

@@ -167,11 +171,11 @@
             baos.write(buf, 0, len);
         }
         baos.close();
         check(Arrays.equals(data, baos.toByteArray()));
         check(zis.getNextEntry() == null);
-        zis.close();
+        }
     }
 
 
     //--------------------- Infrastructure ---------------------------
     static volatile int passed = 0, failed = 0;