8183341: Better cleanup for javax/imageio/AllowSearch.java
authoraghaisas
Mon, 24 Jul 2017 11:54:57 +0530
changeset 47161 fbf657458100
parent 47160 ac5434728c3b
child 47162 9e7eb47ba09c
8183341: Better cleanup for javax/imageio/AllowSearch.java Reviewed-by: prr, jdv, pnarayanan Contributed-by: shashidhara.veerabhadraiah@oracle.com
jdk/test/javax/imageio/AllowSearch.java
--- a/jdk/test/javax/imageio/AllowSearch.java	Fri Jul 21 16:27:35 2017 -0700
+++ b/jdk/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 {
-            int numImages = reader.getNumImages(true);
-        } catch (IOException ioe) {
-            gotISE = false;
-        } catch (IllegalStateException ise) {
-            gotISE = true;
+            f = File.createTempFile("imageio", ".tmp");
+            stream = ImageIO.createImageInputStream(f);
+            reader.setInput(stream, 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) {