1/* 2 * Copyright (c) 2002, 2017, 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 4660047 27 * @summary Tests if the JPEG reader/writer crashes the VM if certain methods 28 * are called after a call to dispose() 29 */ 30 31import java.awt.image.BufferedImage; 32import java.io.ByteArrayInputStream; 33import java.io.ByteArrayOutputStream; 34import java.io.IOException; 35import java.io.InputStream; 36import java.io.OutputStream; 37import java.util.Iterator; 38 39import javax.imageio.ImageIO; 40import javax.imageio.ImageReader; 41import javax.imageio.ImageWriter; 42import javax.imageio.stream.ImageInputStream; 43import javax.imageio.stream.ImageOutputStream; 44 45public class CrashAfterDispose { 46 47 public static void main(String[] args) throws IOException { 48 InputStream bais = new ByteArrayInputStream(new byte[100]); 49 ImageInputStream iis = ImageIO.createImageInputStream(bais); 50 51 // find the JPEG reader 52 ImageReader reader = null; 53 Iterator readers = ImageIO.getImageReadersByFormatName("jpeg"); 54 if (readers.hasNext()) { 55 reader = (ImageReader)readers.next(); 56 } else { 57 throw new RuntimeException("Unable to find a reader!"); 58 } 59 60 // dispose the reader, then poke and prod it... the reader should 61 // throw exceptions (which will be caught by this test), but it 62 // should not crash the VM 63 reader.dispose(); 64 65 try { 66 reader.setInput(iis); 67 } catch (IllegalStateException e) { 68 } 69 70 try { 71 reader.read(0); 72 } catch (IllegalStateException e) { 73 } 74 75 try { 76 reader.abort(); 77 } catch (IllegalStateException e) { 78 } 79 80 try { 81 reader.reset(); 82 } catch (IllegalStateException e) { 83 } 84 85 try { 86 reader.dispose(); 87 } catch (IllegalStateException e) { 88 } 89 90 // find the JPEG writer 91 ImageWriter writer = null; 92 Iterator writers = ImageIO.getImageWritersByFormatName("jpeg"); 93 if (writers.hasNext()) { 94 writer = (ImageWriter)writers.next(); 95 } else { 96 throw new RuntimeException("Unable to find a writer!"); 97 } 98 99 // set up output stream 100 OutputStream baos = new ByteArrayOutputStream(); 101 ImageOutputStream ios = ImageIO.createImageOutputStream(baos); 102 BufferedImage bi = new BufferedImage(10, 10, 103 BufferedImage.TYPE_INT_RGB); 104 105 // dispose the writer, then poke and prod it... the writer should 106 // throw exceptions (which will be caught by this test), but it 107 // should not crash the VM 108 writer.dispose(); 109 110 try { 111 writer.setOutput(ios); 112 } catch (IllegalStateException e) { 113 } 114 115 try { 116 writer.write(bi); 117 } catch (IllegalStateException e) { 118 } 119 120 try { 121 writer.abort(); 122 } catch (IllegalStateException e) { 123 } 124 125 try { 126 writer.reset(); 127 } catch (IllegalStateException e) { 128 } 129 130 try { 131 writer.dispose(); 132 } catch (IllegalStateException e) { 133 } 134 } 135} 136