< prev index next >

test/javax/imageio/plugins/png/ITXtTest.java

Print this page

        

@@ -43,15 +43,16 @@
 import javax.imageio.ImageWriter;
 import javax.imageio.metadata.IIOMetadata;
 import javax.imageio.metadata.IIOMetadataNode;
 import javax.imageio.stream.ImageOutputStream;
 import javax.imageio.stream.ImageInputStream;
+import java.io.IOException;
 
 import org.w3c.dom.Node;
 
 public class ITXtTest {
-    static public void main(String args[]) {
+    static public void main(String args[]) throws IOException {
         ITXtTest t_en = new ITXtTest();
         t_en.description = "xml - en";
         t_en.keyword = "XML:com.adobe.xmp";
         t_en.isCompressed = false;
         t_en.compression = 0;

@@ -149,30 +150,33 @@
         return true;
     }
 
 
 
-    private static void doTest(ITXtTest src) {
+    private static void doTest(ITXtTest src) throws IOException {
 
         System.out.println("Test: " + src.description);
 
         File file = new File("test.png");
 
         writeTo(file, src);
         ITXtTest dst = readFrom(file);
 
         if (dst == null || !dst.equals(src)) {
+            file.delete();
             throw new RuntimeException("Test failed.");
-        }
-
+        } else {
+            file.delete();
         System.out.println("Test passed.");
     }
+    }
 
-    private static void writeTo(File f, ITXtTest t) {
+    private static void writeTo(File f, ITXtTest t) throws IOException {
         BufferedImage src = createBufferedImage();
+        ImageOutputStream imageOutputStream = null;
         try {
-            ImageOutputStream imageOutputStream =
+            imageOutputStream =
                 ImageIO.createImageOutputStream(f);
 
             ImageTypeSpecifier imageTypeSpecifier =
                 new ImageTypeSpecifier(src);
             ImageWriter imageWriter =

@@ -192,17 +196,22 @@
 
             imageWriter.write(new IIOImage(src, null, m));
             imageOutputStream.close();
             System.out.println("Writing done.");
         } catch (Throwable e) {
+            if (imageOutputStream != null) {
+                imageOutputStream.close();
+            }
+            f.delete();
             throw new RuntimeException("Writing test failed.", e);
         }
     }
 
-    private static ITXtTest readFrom(File f) {
+    private static ITXtTest readFrom(File f) throws IOException {
+        ImageInputStream iis = null;
         try {
-            ImageInputStream iis = ImageIO.createImageInputStream(f);
+            iis = ImageIO.createImageInputStream(f);
             ImageReader r = ImageIO.getImageReaders(iis).next();
             r.setInput(iis);
 
             IIOImage dst = r.readAll(0, null);
 

@@ -215,12 +224,17 @@
             }
             if (n == null) {
                 throw new RuntimeException("No iTXt node!");
             }
             ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
+            iis.close();
             return t;
         } catch (Throwable e) {
+            if (iis != null) {
+                iis.close();
+            }
+            f.delete();
             throw new RuntimeException("Reading test failed.", e);
         }
     }
 
     private static BufferedImage createBufferedImage() {
< prev index next >