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 
  24 import java.awt.Color;
  25 import java.awt.Graphics2D;
  26 import java.awt.image.BufferedImage;
  27 import java.io.File;
  28 import java.io.FileNotFoundException;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;
  31 import java.util.Iterator;
  32 
  33 import javax.imageio.ImageIO;
  34 import javax.imageio.ImageWriter;
  35 import javax.imageio.event.IIOWriteProgressListener;
  36 import javax.imageio.spi.IIORegistry;
  37 import javax.imageio.spi.ImageWriterSpi;
  38 import javax.imageio.stream.ImageOutputStream;
  39 
  40 import static java.awt.image.BufferedImage.TYPE_BYTE_BINARY;
  41 
  42 /**
  43  * @test
  44  * @bug 4952954
  45  * @summary abortFlag must be cleared for every ImageWriter.write operation
  46  * @author Sergey Bylokhov
  47  */
  48 public final class WriteAfterAbort implements IIOWriteProgressListener {
  49 
  50     private volatile boolean abortFlag = true;
  51     private volatile boolean isAbortCalled;
  52     private volatile boolean isCompleteCalled;
  53     private volatile boolean isProgressCalled;
  54     private volatile boolean isStartedCalled;
  55     private static final int WIDTH = 100;
  56     private static final int HEIGHT = 100;
  57 
  58     private void test(final ImageWriter writer) throws IOException {
  59         // Image initialization
  60         final BufferedImage imageWrite = new BufferedImage(WIDTH, HEIGHT,
  61                                                            TYPE_BYTE_BINARY);
  62         final Graphics2D g = imageWrite.createGraphics();
  63         g.setColor(Color.WHITE);
  64         g.fillRect(0, 0, WIDTH, HEIGHT);
  65         g.dispose();
  66 
  67         // File initialization
  68         final File file = File.createTempFile("temp", ".img");
  69         file.deleteOnExit();
  70         final FileOutputStream fos = new SkipWriteOnAbortOutputStream(file);
  71         final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
  72         writer.setOutput(ios);
  73         writer.addIIOWriteProgressListener(this);
  74 
  75         // This write will be aborted, and file will not be touched
  76         writer.write(imageWrite);
  77         if (!isStartedCalled) {
  78             throw new RuntimeException("Started should be called");
  79         }
  80         if (!isProgressCalled) {
  81             throw new RuntimeException("Progress should be called");
  82         }
  83         if (!isAbortCalled) {
  84             throw new RuntimeException("Abort should be called");
  85         }
  86         if (isCompleteCalled) {
  87             throw new RuntimeException("Complete should not be called");
  88         }
  89         // Flush aborted data
  90         ios.flush();
  91 
  92         // This write should be completed successfully and the file should
  93         // contain correct image data.
  94         abortFlag = false;
  95         isAbortCalled = false;
  96         isCompleteCalled = false;
  97         isProgressCalled = false;
  98         isStartedCalled = false;
  99         writer.write(imageWrite);
 100 
 101         if (!isStartedCalled) {
 102             throw new RuntimeException("Started should be called");
 103         }
 104         if (!isProgressCalled) {
 105             throw new RuntimeException("Progress should be called");
 106         }
 107         if (isAbortCalled) {
 108             throw new RuntimeException("Abort should not be called");
 109         }
 110         if (!isCompleteCalled) {
 111             throw new RuntimeException("Complete should be called");
 112         }
 113         writer.dispose();
 114         ios.close();
 115 
 116         // Validates content of the file.
 117         final BufferedImage imageRead = ImageIO.read(file);
 118         for (int x = 0; x < WIDTH; ++x) {
 119             for (int y = 0; y < HEIGHT; ++y) {
 120                 if (imageRead.getRGB(x, y) != imageWrite.getRGB(x, y)) {
 121                     throw new RuntimeException("Test failed.");
 122                 }
 123             }
 124         }
 125     }
 126 
 127     public static void main(final String[] args) throws IOException {
 128         final IIORegistry registry = IIORegistry.getDefaultInstance();
 129         final Iterator<ImageWriterSpi> iter = registry.getServiceProviders(
 130                 ImageWriterSpi.class, provider -> true, true);
 131 
 132         // Validates all supported ImageWriters
 133         int numFailures = 0;
 134         while (iter.hasNext()) {
 135             final WriteAfterAbort writeAfterAbort = new WriteAfterAbort();
 136             final ImageWriter writer = iter.next().createWriterInstance();
 137             System.out.println("ImageWriter = " + writer);
 138             try {
 139                 writeAfterAbort.test(writer);
 140             } catch (Exception e) {
 141                 System.err.println("Test failed for \""
 142                     + writer.getOriginatingProvider().getFormatNames()[0]
 143                     + "\" format.");
 144                 numFailures++;
 145             }
 146         }
 147         if (numFailures == 0) {
 148             System.out.println("Test passed.");
 149         } else {
 150             throw new RuntimeException("Test failed.");
 151         }
 152     }
 153 
 154     // Callbacks
 155 
 156     @Override
 157     public void imageComplete(ImageWriter source) {
 158         isCompleteCalled = true;
 159     }
 160 
 161     @Override
 162     public void imageProgress(ImageWriter source, float percentageDone) {
 163         isProgressCalled = true;
 164         if (percentageDone > 50 && abortFlag) {
 165             source.abort();
 166         }
 167     }
 168 
 169     @Override
 170     public void imageStarted(ImageWriter source, int imageIndex) {
 171         isStartedCalled = true;
 172     }
 173 
 174     @Override
 175     public void writeAborted(final ImageWriter source) {
 176         isAbortCalled = true;
 177     }
 178 
 179     @Override
 180     public void thumbnailComplete(ImageWriter source) {
 181     }
 182 
 183     @Override
 184     public void thumbnailProgress(ImageWriter source, float percentageDone) {
 185     }
 186 
 187     @Override
 188     public void thumbnailStarted(ImageWriter source, int imageIndex,
 189                                  int thumbnailIndex) {
 190     }
 191 
 192     /**
 193      * We need to skip writes on abort, because content of the file after abort
 194      * is undefined.
 195      */
 196     private class SkipWriteOnAbortOutputStream extends FileOutputStream {
 197 
 198         SkipWriteOnAbortOutputStream(File file) throws FileNotFoundException {
 199             super(file);
 200         }
 201 
 202         @Override
 203         public void write(int b) throws IOException {
 204             if (!abortFlag) {
 205                 super.write(b);
 206             }
 207         }
 208 
 209         @Override
 210         public void write(byte[] b) throws IOException {
 211             if (!abortFlag) {
 212                 super.write(b);
 213             }
 214         }
 215 
 216         @Override
 217         public void write(byte[] b, int off, int len) throws IOException {
 218             if (!abortFlag) {
 219                 super.write(b, off, len);
 220             }
 221         }
 222     }
 223 }
 224