Path: blob/master/test/jdk/javax/imageio/plugins/gif/WriterReuseTest.java
41154 views
/*1* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 628308926* @summary Test verifies that abort flag is cleared by the next write() call27*/2829import java.awt.Color;30import java.awt.Graphics;31import java.awt.image.BufferedImage;32import java.io.ByteArrayOutputStream;33import java.io.IOException;3435import javax.imageio.IIOImage;36import javax.imageio.ImageIO;37import javax.imageio.ImageWriteParam;38import javax.imageio.ImageWriter;39import javax.imageio.event.IIOWriteProgressListener;40import javax.imageio.metadata.IIOMetadata;41import javax.imageio.stream.ImageOutputStream;4243public class WriterReuseTest implements IIOWriteProgressListener {4445boolean isFirst = true;46boolean isWritingCompleted = false;47boolean isWritingAborted = false;4849public static void main(String[] args) throws IOException {50doTest(false);51doTest(true);52}5354public static void doTest(boolean writeSequence) throws IOException {55String format = "GIF";56ImageWriter writer =57ImageIO.getImageWritersByFormatName(format).next();58if (writer == null) {59throw new RuntimeException("No writer available for " + format);60}6162BufferedImage img = createTestImage();63ByteArrayOutputStream baos = new ByteArrayOutputStream();64ImageOutputStream ios = ImageIO.createImageOutputStream(baos);65writer.setOutput(ios);6667WriterReuseTest t = new WriterReuseTest();68writer.addIIOWriteProgressListener(t);6970ImageWriteParam param = writer.getDefaultWriteParam();71IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(param);72IIOImage iioImg = new IIOImage(img, null, null);73if (writeSequence) {74writer.prepareWriteSequence(streamMetadata);75writer.writeToSequence(iioImg, param);76} else {77writer.write(img);78}7980if (!t.isWritingAborted || t.isWritingCompleted) {81throw new RuntimeException("Test failed.");82}83t.reset();8485// next attempt after abort86ImageOutputStream ios2 =87ImageIO.createImageOutputStream(new ByteArrayOutputStream());88writer.setOutput(ios2);89if (writeSequence) {90writer.writeToSequence(iioImg, param);91} else {92writer.write(img);93}9495if (t.isWritingAborted || !t.isWritingCompleted) {96throw new RuntimeException("Test failed.");97}98System.out.println("Test passed.");99}100101public static BufferedImage createTestImage() {102BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_INDEXED);103Graphics g = img.createGraphics();104g.setColor(Color.black);105g.fillRect(0, 0, 100, 100);106107g.setColor(Color.white);108g.fillRect(10, 10, 80, 80);109110return img;111}112113public WriterReuseTest() {114isFirst = true;115reset();116}117118public void reset() {119isWritingAborted = false;120isWritingCompleted = false;121}122123public void imageComplete(ImageWriter source) {124System.out.println("Image Completed");125this.isWritingCompleted = true;126}127128public void imageProgress(ImageWriter source, float percentageDone) {129System.out.println("Image Progress "+percentageDone);130if (percentageDone > 50 && isFirst) {131isFirst = false;132source.abort();133}134}135136public void imageStarted(ImageWriter source, int imageIndex) {137System.out.println("Image Started "+imageIndex);138}139140public void thumbnailComplete(ImageWriter source) {141System.out.println("Thubnail completed");142}143144public void thumbnailProgress(ImageWriter source, float percentageDone) {145System.out.println("Thubnail Progress " + percentageDone);146}147148public void thumbnailStarted(ImageWriter source, int imageIndex, int thumbnailIndex) {149System.out.println("Thubnail started " + imageIndex);150}151152public void writeAborted(ImageWriter source) {153System.out.println("Writing Aborted");154this.isWritingAborted = true;155}156}157158159