Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/CrashAfterDispose.java
41152 views
/*1* Copyright (c) 2002, 2017, 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 466004726* @summary Tests if the JPEG reader/writer crashes the VM if certain methods27* are called after a call to dispose()28*/2930import java.awt.image.BufferedImage;31import java.io.ByteArrayInputStream;32import java.io.ByteArrayOutputStream;33import java.io.IOException;34import java.io.InputStream;35import java.io.OutputStream;36import java.util.Iterator;3738import javax.imageio.ImageIO;39import javax.imageio.ImageReader;40import javax.imageio.ImageWriter;41import javax.imageio.stream.ImageInputStream;42import javax.imageio.stream.ImageOutputStream;4344public class CrashAfterDispose {4546public static void main(String[] args) throws IOException {47InputStream bais = new ByteArrayInputStream(new byte[100]);48ImageInputStream iis = ImageIO.createImageInputStream(bais);4950// find the JPEG reader51ImageReader reader = null;52Iterator readers = ImageIO.getImageReadersByFormatName("jpeg");53if (readers.hasNext()) {54reader = (ImageReader)readers.next();55} else {56throw new RuntimeException("Unable to find a reader!");57}5859// dispose the reader, then poke and prod it... the reader should60// throw exceptions (which will be caught by this test), but it61// should not crash the VM62reader.dispose();6364try {65reader.setInput(iis);66} catch (IllegalStateException e) {67}6869try {70reader.read(0);71} catch (IllegalStateException e) {72}7374try {75reader.abort();76} catch (IllegalStateException e) {77}7879try {80reader.reset();81} catch (IllegalStateException e) {82}8384try {85reader.dispose();86} catch (IllegalStateException e) {87}8889// find the JPEG writer90ImageWriter writer = null;91Iterator writers = ImageIO.getImageWritersByFormatName("jpeg");92if (writers.hasNext()) {93writer = (ImageWriter)writers.next();94} else {95throw new RuntimeException("Unable to find a writer!");96}9798// set up output stream99OutputStream baos = new ByteArrayOutputStream();100ImageOutputStream ios = ImageIO.createImageOutputStream(baos);101BufferedImage bi = new BufferedImage(10, 10,102BufferedImage.TYPE_INT_RGB);103104// dispose the writer, then poke and prod it... the writer should105// throw exceptions (which will be caught by this test), but it106// should not crash the VM107writer.dispose();108109try {110writer.setOutput(ios);111} catch (IllegalStateException e) {112}113114try {115writer.write(bi);116} catch (IllegalStateException e) {117}118119try {120writer.abort();121} catch (IllegalStateException e) {122}123124try {125writer.reset();126} catch (IllegalStateException e) {127}128129try {130writer.dispose();131} catch (IllegalStateException e) {132}133}134}135136137