1/*
2 * Copyright (c) 2005, 2017, 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/*
25 * @test
26 * @bug 6283089
27 * @summary Test verifies that abort flag is cleared by the next write() call
28 */
29
30import java.awt.Color;
31import java.awt.Graphics;
32import java.awt.image.BufferedImage;
33import java.io.ByteArrayOutputStream;
34import java.io.IOException;
35
36import javax.imageio.IIOImage;
37import javax.imageio.ImageIO;
38import javax.imageio.ImageWriteParam;
39import javax.imageio.ImageWriter;
40import javax.imageio.event.IIOWriteProgressListener;
41import javax.imageio.metadata.IIOMetadata;
42import javax.imageio.stream.ImageOutputStream;
43
44public class WriterReuseTest implements IIOWriteProgressListener {
45
46    boolean isFirst = true;
47    boolean isWritingCompleted = false;
48    boolean isWritingAborted = false;
49
50    public static void main(String[] args) throws IOException {
51        doTest(false);
52        doTest(true);
53    }
54
55    public static void doTest(boolean writeSequence) throws IOException {
56        String format = "GIF";
57        ImageWriter writer =
58                ImageIO.getImageWritersByFormatName(format).next();
59        if (writer == null) {
60            throw new RuntimeException("No writer available for " + format);
61        }
62
63        BufferedImage img = createTestImage();
64        ByteArrayOutputStream baos = new ByteArrayOutputStream();
65        ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
66        writer.setOutput(ios);
67
68        WriterReuseTest t = new WriterReuseTest();
69        writer.addIIOWriteProgressListener(t);
70
71        ImageWriteParam param = writer.getDefaultWriteParam();
72        IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(param);
73        IIOImage iioImg = new IIOImage(img, null, null);
74        if (writeSequence) {
75            writer.prepareWriteSequence(streamMetadata);
76            writer.writeToSequence(iioImg, param);
77        } else {
78            writer.write(img);
79        }
80
81        if (!t.isWritingAborted || t.isWritingCompleted) {
82            throw new RuntimeException("Test failed.");
83        }
84        t.reset();
85
86        // next attempt after abort
87        ImageOutputStream ios2 =
88             ImageIO.createImageOutputStream(new ByteArrayOutputStream());
89        writer.setOutput(ios2);
90        if (writeSequence) {
91            writer.writeToSequence(iioImg, param);
92        } else {
93            writer.write(img);
94        }
95
96        if (t.isWritingAborted || !t.isWritingCompleted) {
97            throw new RuntimeException("Test failed.");
98        }
99        System.out.println("Test passed.");
100    }
101
102    public static BufferedImage createTestImage() {
103        BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_INDEXED);
104        Graphics g = img.createGraphics();
105        g.setColor(Color.black);
106        g.fillRect(0, 0, 100, 100);
107
108        g.setColor(Color.white);
109        g.fillRect(10, 10, 80, 80);
110
111        return img;
112    }
113
114    public WriterReuseTest() {
115        isFirst = true;
116        reset();
117    }
118
119    public void reset() {
120        isWritingAborted = false;
121        isWritingCompleted = false;
122    }
123
124    public void imageComplete(ImageWriter source) {
125        System.out.println("Image Completed");
126        this.isWritingCompleted = true;
127    }
128
129    public void imageProgress(ImageWriter source, float percentageDone) {
130        System.out.println("Image Progress "+percentageDone);
131        if (percentageDone > 50 && isFirst) {
132            isFirst = false;
133            source.abort();
134        }
135    }
136
137    public void imageStarted(ImageWriter source, int imageIndex) {
138        System.out.println("Image Started "+imageIndex);
139    }
140
141    public void thumbnailComplete(ImageWriter source)  {
142        System.out.println("Thubnail completed");
143    }
144
145    public void thumbnailProgress(ImageWriter source, float percentageDone) {
146        System.out.println("Thubnail Progress " + percentageDone);
147    }
148
149    public void thumbnailStarted(ImageWriter source, int imageIndex, int thumbnailIndex) {
150        System.out.println("Thubnail started " + imageIndex);
151    }
152
153    public void writeAborted(ImageWriter source) {
154        System.out.println("Writing Aborted");
155        this.isWritingAborted = true;
156    }
157}
158