Path: blob/master/test/jdk/javax/imageio/plugins/shared/WriteAfterAbort.java
41153 views
/*1* Copyright (c) 2015, 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*/2223import java.awt.Color;24import java.awt.Graphics2D;25import java.awt.image.BufferedImage;26import java.io.File;27import java.io.FileNotFoundException;28import java.io.FileOutputStream;29import java.io.IOException;30import java.util.Iterator;31import java.nio.file.Files;3233import javax.imageio.ImageIO;34import javax.imageio.ImageWriter;35import javax.imageio.event.IIOWriteProgressListener;36import javax.imageio.spi.IIORegistry;37import javax.imageio.spi.ImageWriterSpi;38import javax.imageio.stream.ImageOutputStream;3940import static java.awt.image.BufferedImage.TYPE_BYTE_BINARY;4142/**43* @test44* @bug 4952954 818334945* @summary abortFlag must be cleared for every ImageWriter.write operation46* @run main WriteAfterAbort47*/48public final class WriteAfterAbort implements IIOWriteProgressListener {4950private volatile boolean abortFlag = true;51private volatile boolean isAbortCalled;52private volatile boolean isCompleteCalled;53private volatile boolean isProgressCalled;54private volatile boolean isStartedCalled;55private static final int WIDTH = 100;56private static final int HEIGHT = 100;57private static FileOutputStream fos;58private static File file;5960private void test(final ImageWriter writer) throws IOException {61try {62// Image initialization63final BufferedImage imageWrite =64new BufferedImage(WIDTH, HEIGHT, TYPE_BYTE_BINARY);65final Graphics2D g = imageWrite.createGraphics();66g.setColor(Color.WHITE);67g.fillRect(0, 0, WIDTH, HEIGHT);68g.dispose();6970// File initialization71file = File.createTempFile("temp", ".img");72fos = new SkipWriteOnAbortOutputStream(file);73final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);74writer.setOutput(ios);75writer.addIIOWriteProgressListener(this);7677// This write will be aborted, and file will not be touched78writer.write(imageWrite);79if (!isStartedCalled) {80throw new RuntimeException("Started should be called");81}82if (!isProgressCalled) {83throw new RuntimeException("Progress should be called");84}85if (!isAbortCalled) {86throw new RuntimeException("Abort should be called");87}88if (isCompleteCalled) {89throw new RuntimeException("Complete should not be called");90}91// Flush aborted data92ios.flush();9394/*95* This write should be completed successfully and the file should96* contain correct image data.97*/98abortFlag = false;99isAbortCalled = false;100isCompleteCalled = false;101isProgressCalled = false;102isStartedCalled = false;103writer.write(imageWrite);104105if (!isStartedCalled) {106throw new RuntimeException("Started should be called");107}108if (!isProgressCalled) {109throw new RuntimeException("Progress should be called");110}111if (isAbortCalled) {112throw new RuntimeException("Abort should not be called");113}114if (!isCompleteCalled) {115throw new RuntimeException("Complete should be called");116}117ios.close();118119// Validates content of the file.120final BufferedImage imageRead = ImageIO.read(file);121for (int x = 0; x < WIDTH; ++x) {122for (int y = 0; y < HEIGHT; ++y) {123if (imageRead.getRGB(x, y) != imageWrite.getRGB(x, y)) {124throw new RuntimeException("Test failed.");125}126}127}128} finally {129writer.dispose();130if (file != null) {131if (fos != null) {132fos.close();133}134Files.delete(file.toPath());135}136}137}138139public static void main(final String[] args) throws IOException {140final IIORegistry registry = IIORegistry.getDefaultInstance();141final Iterator<ImageWriterSpi> iter = registry.getServiceProviders(142ImageWriterSpi.class, provider -> true, true);143144// Validates all supported ImageWriters145int numFailures = 0;146while (iter.hasNext()) {147final WriteAfterAbort writeAfterAbort = new WriteAfterAbort();148final ImageWriter writer = iter.next().createWriterInstance();149System.out.println("ImageWriter = " + writer);150try {151writeAfterAbort.test(writer);152} catch (Exception e) {153System.err.println("Test failed for \""154+ writer.getOriginatingProvider().getFormatNames()[0]155+ "\" format.");156numFailures++;157}158}159if (numFailures == 0) {160System.out.println("Test passed.");161} else {162throw new RuntimeException("Test failed.");163}164}165166// Callbacks167168@Override169public void imageComplete(ImageWriter source) {170isCompleteCalled = true;171}172173@Override174public void imageProgress(ImageWriter source, float percentageDone) {175isProgressCalled = true;176if (percentageDone > 50 && abortFlag) {177source.abort();178}179}180181@Override182public void imageStarted(ImageWriter source, int imageIndex) {183isStartedCalled = true;184}185186@Override187public void writeAborted(final ImageWriter source) {188isAbortCalled = true;189}190191@Override192public void thumbnailComplete(ImageWriter source) {193}194195@Override196public void thumbnailProgress(ImageWriter source, float percentageDone) {197}198199@Override200public void thumbnailStarted(ImageWriter source, int imageIndex,201int thumbnailIndex) {202}203204/**205* We need to skip writes on abort, because content of the file after abort206* is undefined.207*/208private class SkipWriteOnAbortOutputStream extends FileOutputStream {209210SkipWriteOnAbortOutputStream(File file) throws FileNotFoundException {211super(file);212}213214@Override215public void write(int b) throws IOException {216if (!abortFlag) {217super.write(b);218}219}220221@Override222public void write(byte[] b) throws IOException {223if (!abortFlag) {224super.write(b);225}226}227228@Override229public void write(byte[] b, int off, int len) throws IOException {230if (!abortFlag) {231super.write(b, off, len);232}233}234}235}236237238239