1 /*
   2  * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.FileOutputStream;
  26 import java.nio.file.Files;
  27 import java.util.Iterator;
  28 
  29 import javax.imageio.ImageIO;
  30 import javax.imageio.ImageWriter;
  31 import javax.imageio.metadata.IIOMetadata;
  32 import javax.imageio.spi.IIORegistry;
  33 import javax.imageio.spi.ImageWriterSpi;
  34 import javax.imageio.stream.ImageOutputStream;
  35 
  36 /**
  37  * @test
  38  * @bug     4958064 8183349
  39  * @summary Test verifies that when we try to forcefully run
  40  *          prepareWriteSequence() where it is not supported
  41  *          will ImageIO throws an UnsupportedOperationException
  42  *          or not.
  43  * @run     main CanWriteSequence
  44  */
  45 public final class CanWriteSequence {
  46 
  47     private static File file;
  48     private static FileOutputStream fos;
  49     private static ImageOutputStream ios;
  50 
  51     public static void main(final String[] args) throws Exception {
  52         final IIORegistry registry = IIORegistry.getDefaultInstance();
  53         final Iterator<ImageWriterSpi> iter =
  54                 registry.getServiceProviders(ImageWriterSpi.class,
  55                         provider -> true, true);
  56         // Validates all supported ImageWriters
  57         while (iter.hasNext()) {
  58             final ImageWriter writer = iter.next().createWriterInstance();
  59             System.out.println("ImageWriter = " + writer);
  60             test(writer);
  61         }
  62         System.out.println("Test passed");
  63     }
  64 
  65     private static void test(final ImageWriter writer) throws Exception {
  66         try {
  67             file = File.createTempFile("temp", ".img");
  68             fos = new FileOutputStream(file);
  69             ios = ImageIO.createImageOutputStream(fos);
  70             writer.setOutput(ios);
  71             final IIOMetadata data = writer.getDefaultStreamMetadata(null);
  72 
  73             if (writer.canWriteSequence()) {
  74                 writer.prepareWriteSequence(data);
  75             } else {
  76                 try {
  77                     writer.prepareWriteSequence(data);
  78                     throw new RuntimeException(
  79                             "UnsupportedOperationException was not thrown");
  80                 } catch (final UnsupportedOperationException ignored) {
  81                 // expected
  82                 }
  83             }
  84         } finally {
  85             writer.dispose();
  86             if (file != null) {
  87                 if (ios != null) {
  88                     ios.close();
  89                 }
  90                 if (fos != null) {
  91                     fos.close();
  92                 }
  93                 Files.delete(file.toPath());
  94             }
  95         }
  96     }
  97 }

--- EOF ---