changeset 17722:b0d7bf900075

8183341: Better cleanup for javax/imageio/AllowSearch.java Reviewed-by: prr, jdv, pnarayanan Contributed-by: shashidhara.veerabhadraiah@oracle.com
author aghaisas
date Mon, 24 Jul 2017 11:54:57 +0530
parents e282991b6970
children 971e17807496
files test/javax/imageio/AllowSearch.java
diffstat 1 files changed, 26 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/test/javax/imageio/AllowSearch.java	Fri Jul 21 16:27:35 2017 -0700
+++ b/test/javax/imageio/AllowSearch.java	Mon Jul 24 11:54:57 2017 +0530
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 4420318
+ * @bug 4420318 8183341
  * @summary Checks that an IllegalStateException is thrown by getNumImages(true)
  *          when seekForwardOnly is true
  * @modules java.desktop/com.sun.imageio.plugins.gif
@@ -33,6 +33,7 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
 
 import javax.imageio.ImageIO;
 import javax.imageio.ImageReader;
@@ -43,20 +44,33 @@
 import com.sun.imageio.plugins.png.PNGImageReader;
 
 public class AllowSearch {
-
     private static void test(ImageReader reader, String format)
         throws IOException {
-        File f = File.createTempFile("imageio", ".tmp");
-        ImageInputStream stream = ImageIO.createImageInputStream(f);
-        reader.setInput(stream, true);
+        boolean gotISE = false;
+        File f = null;
+        ImageInputStream stream = null;
+        try {
+            f = File.createTempFile("imageio", ".tmp");
+            stream = ImageIO.createImageInputStream(f);
+            reader.setInput(stream, true);
 
-        boolean gotISE = false;
-        try {
-            int numImages = reader.getNumImages(true);
-        } catch (IOException ioe) {
-            gotISE = false;
-        } catch (IllegalStateException ise) {
-            gotISE = true;
+            try {
+                int numImages = reader.getNumImages(true);
+            } catch (IOException ioe) {
+                gotISE = false;
+            } catch (IllegalStateException ise) {
+                gotISE = true;
+            }
+        } finally {
+            if (stream != null) {
+                stream.close();
+            }
+
+            reader.dispose();
+
+            if (f != null) {
+                Files.delete(f.toPath());
+            }
         }
 
         if (!gotISE) {