Path: blob/master/test/jdk/javax/imageio/plugins/png/PngForceStopWritingTest.java
41155 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 696741926* @summary Test verifies that when we force stop PNG writing to27* ImageOutputStream, it should not cause IndexOutOfBoundException.28* @run main PngForceStopWritingTest29*/3031import java.awt.Color;32import java.awt.GradientPaint;33import java.awt.Graphics2D;34import java.awt.image.BufferedImage;35import java.io.IOException;36import java.io.OutputStream;37import javax.imageio.ImageIO;38import javax.imageio.stream.ImageOutputStream;3940public class PngForceStopWritingTest {4142public static void main(String[] args) throws IOException {4344OutputStream outputStream = new NullOutputStream();45ImageOutputStream imageOutputStream =46ImageIO.createImageOutputStream(outputStream);47try {48ImageIO.write(createImage(2048),"PNG", imageOutputStream);49} catch (IOException e) {50imageOutputStream.close();51}52}5354private static BufferedImage createImage(int size) {5556BufferedImage image = new57BufferedImage(size, size, BufferedImage.TYPE_3BYTE_BGR);58Graphics2D g = image.createGraphics();59g.setPaint(new GradientPaint(0, 0, Color.blue, size, size, Color.red));60g.fillRect(0, 0, size, size);61g.dispose();62return image;63}6465static class NullOutputStream extends OutputStream {66long count = 0;67@Override68public void write(int b) throws IOException {69count++;70if (count > 30000L) {71throw new IOException("Force stop image writing");72}73}74}75}767778