jdk/test/javax/imageio/plugins/png/PngForceStopWritingTest.java
author jdv
Wed, 02 Dec 2015 00:47:36 +0530
changeset 34792 9421752d717b
permissions -rw-r--r--
6967419: IndexOutOfBoundsException when drawing PNGs Reviewed-by: prr, psadhukhan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34792
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
     1
/*
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
     4
 *
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
     8
 *
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    13
 * accompanied this code).
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    14
 *
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    18
 *
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    21
 * questions.
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    22
 */
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    23
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    24
/**
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    25
 * @test
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    26
 * @bug     6967419
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    27
 * @summary Test verifies that when we force stop PNG writing to
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    28
 *          ImageOutputStream, it should not cause IndexOutOfBoundException.
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    29
 * @run     main PngForceStopWritingTest
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    30
 */
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    31
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    32
import java.awt.Color;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    33
import java.awt.GradientPaint;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    34
import java.awt.Graphics2D;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    35
import java.awt.image.BufferedImage;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    36
import java.io.IOException;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    37
import java.io.OutputStream;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    38
import javax.imageio.ImageIO;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    39
import javax.imageio.stream.ImageOutputStream;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    40
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    41
public class PngForceStopWritingTest {
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    42
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    43
    public static void main(String[] args) throws IOException {
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    44
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    45
        OutputStream outputStream = new NullOutputStream();
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    46
        ImageOutputStream imageOutputStream =
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    47
            ImageIO.createImageOutputStream(outputStream);
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    48
        try {
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    49
            ImageIO.write(createImage(2048),"PNG", imageOutputStream);
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    50
        } catch (IOException e) {
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    51
            imageOutputStream.close();
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    52
        }
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    53
    }
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    54
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    55
    private static BufferedImage createImage(int size) {
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    56
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    57
        BufferedImage image = new
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    58
            BufferedImage(size, size, BufferedImage.TYPE_3BYTE_BGR);
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    59
        Graphics2D g = image.createGraphics();
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    60
        g.setPaint(new GradientPaint(0, 0, Color.blue, size, size, Color.red));
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    61
        g.fillRect(0, 0, size, size);
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    62
        g.dispose();
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    63
        return image;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    64
    }
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    65
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    66
    static class NullOutputStream extends OutputStream {
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    67
        long count = 0;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    68
        @Override
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    69
        public void write(int b) throws IOException {
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    70
            count++;
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    71
            if (count > 30000L) {
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    72
                throw new IOException("Force stop image writing");
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    73
            }
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    74
        }
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    75
    }
9421752d717b 6967419: IndexOutOfBoundsException when drawing PNGs
jdv
parents:
diff changeset
    76
}