Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/imageio/plugins/shared/WriteAfterAbort.java
38853 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 ImageWriters145while (iter.hasNext()) {146final WriteAfterAbort writeAfterAbort = new WriteAfterAbort();147final ImageWriter writer = iter.next().createWriterInstance();148System.out.println("ImageWriter = " + writer);149writeAfterAbort.test(writer);150}151System.out.println("Test passed");152}153154// Callbacks155156@Override157public void imageComplete(ImageWriter source) {158isCompleteCalled = true;159}160161@Override162public void imageProgress(ImageWriter source, float percentageDone) {163isProgressCalled = true;164if (percentageDone > 50 && abortFlag) {165source.abort();166}167}168169@Override170public void imageStarted(ImageWriter source, int imageIndex) {171isStartedCalled = true;172}173174@Override175public void writeAborted(final ImageWriter source) {176isAbortCalled = true;177}178179@Override180public void thumbnailComplete(ImageWriter source) {181}182183@Override184public void thumbnailProgress(ImageWriter source, float percentageDone) {185}186187@Override188public void thumbnailStarted(ImageWriter source, int imageIndex,189int thumbnailIndex) {190}191192/**193* We need to skip writes on abort, because content of the file after abort194* is undefined.195*/196private class SkipWriteOnAbortOutputStream extends FileOutputStream {197198SkipWriteOnAbortOutputStream(File file) throws FileNotFoundException {199super(file);200}201202@Override203public void write(int b) throws IOException {204if (!abortFlag) {205super.write(b);206}207}208209@Override210public void write(byte[] b) throws IOException {211if (!abortFlag) {212super.write(b);213}214}215216@Override217public void write(byte[] b, int off, int len) throws IOException {218if (!abortFlag) {219super.write(b, off, len);220}221}222}223}224225226227