jdk/test/javax/imageio/plugins/png/PngForceStopWritingTest.java
changeset 34792 9421752d717b
equal deleted inserted replaced
34791:ba6621e4e133 34792:9421752d717b
       
     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             imageOutputStream.close();
       
    52         }
       
    53     }
       
    54 
       
    55     private static BufferedImage createImage(int size) {
       
    56 
       
    57         BufferedImage image = new
       
    58             BufferedImage(size, size, BufferedImage.TYPE_3BYTE_BGR);
       
    59         Graphics2D g = image.createGraphics();
       
    60         g.setPaint(new GradientPaint(0, 0, Color.blue, size, size, Color.red));
       
    61         g.fillRect(0, 0, size, size);
       
    62         g.dispose();
       
    63         return image;
       
    64     }
       
    65 
       
    66     static class NullOutputStream extends OutputStream {
       
    67         long count = 0;
       
    68         @Override
       
    69         public void write(int b) throws IOException {
       
    70             count++;
       
    71             if (count > 30000L) {
       
    72                 throw new IOException("Force stop image writing");
       
    73             }
       
    74         }
       
    75     }
       
    76 }