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