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 /**
  25  * @test
  26  * @bug     6967419
  27  * @summary Test verifies that when we force stop PNG writing to
  28  *          ImageOutputStream, it should not cause IndexOutOfBoundException.
  29  * @run     main PngForceStopWritingTest
  30  */
  31 
  32 import java.awt.Color;
  33 import java.awt.GradientPaint;
  34 import java.awt.Graphics2D;
  35 import java.awt.image.BufferedImage;
  36 import java.io.IOException;
  37 import java.io.OutputStream;
  38 import javax.imageio.ImageIO;
  39 import javax.imageio.stream.ImageOutputStream;
  40 
  41 public class PngForceStopWritingTest {
  42 
  43     public static void main(String[] args) throws IOException {
  44 
  45         OutputStream outputStream = new NullOutputStream();
  46         ImageOutputStream imageOutputStream =
  47             ImageIO.createImageOutputStream(outputStream);
  48         try {
  49             ImageIO.write(createImage(2048),"PNG", imageOutputStream);
  50         } catch (IOException e) {
  51             try {
  52                 imageOutputStream.close();
  53             } catch (IndexOutOfBoundsException ex) {
  54                 if(ex != null)
  55                     throw new
  56                         RuntimeException("Caught IndexOutOfBoundException");
  57             }
  58         }
  59     }
  60 
  61     private static BufferedImage createImage(int size) {
  62 
  63         BufferedImage image = new
  64             BufferedImage(size, size, BufferedImage.TYPE_3BYTE_BGR);
  65         Graphics2D g = image.createGraphics();
  66         g.setPaint(new GradientPaint(0, 0, Color.blue, size, size, Color.red));
  67         g.fillRect(0, 0, size, size);
  68         g.dispose();
  69         return image;
  70     }
  71 
  72     static class NullOutputStream extends OutputStream {
  73         long count = 0;
  74         @Override
  75         public void write(int b) throws IOException {
  76             count++;
  77             if (count > 30000L) {
  78                 throw new IOException("Force stop image writing");
  79             }
  80         }
  81     }
  82 }