1/*
2 * Copyright (c) 2015, 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
24import java.io.File;
25import java.io.FileOutputStream;
26import java.util.Iterator;
27
28import javax.imageio.ImageIO;
29import javax.imageio.ImageWriter;
30import javax.imageio.metadata.IIOMetadata;
31import javax.imageio.spi.IIORegistry;
32import javax.imageio.spi.ImageWriterSpi;
33import javax.imageio.stream.ImageOutputStream;
34
35/**
36 * @test
37 * @bug 4958064
38 * @author Sergey Bylokhov
39 */
40public final class CanWriteSequence {
41
42    public static void main(final String[] args) throws Exception {
43        final IIORegistry registry = IIORegistry.getDefaultInstance();
44        final Iterator<ImageWriterSpi> iter =
45                registry.getServiceProviders(ImageWriterSpi.class,
46                        provider -> true, true);
47        // Validates all supported ImageWriters
48        while (iter.hasNext()) {
49            final ImageWriter writer = iter.next().createWriterInstance();
50            System.out.println("ImageWriter = " + writer);
51            test(writer);
52        }
53        System.out.println("Test passed");
54    }
55
56    private static void test(final ImageWriter writer) throws Exception {
57        final File file = File.createTempFile("temp", ".img");
58        file.deleteOnExit();
59        final FileOutputStream fos = new FileOutputStream(file);
60        final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
61        writer.setOutput(ios);
62        final IIOMetadata data = writer.getDefaultStreamMetadata(null);
63
64        if (writer.canWriteSequence()) {
65            writer.prepareWriteSequence(data);
66        } else {
67            try {
68                writer.prepareWriteSequence(data);
69                throw new RuntimeException(
70                        "UnsupportedOperationException was not thrown");
71            } catch (final UnsupportedOperationException ignored) {
72                // expected
73            }
74        }
75        writer.dispose();
76        ios.close();
77    }
78}
79