Path: blob/master/test/jdk/javax/imageio/AllowSearch.java
41144 views
/*1* Copyright (c) 2001, 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 4420318 818334126* @summary Checks that an IllegalStateException is thrown by getNumImages(true)27* when seekForwardOnly is true28* @modules java.desktop/com.sun.imageio.plugins.gif29* java.desktop/com.sun.imageio.plugins.jpeg30* java.desktop/com.sun.imageio.plugins.png31*/3233import java.io.File;34import java.io.IOException;35import java.nio.file.Files;3637import javax.imageio.ImageIO;38import javax.imageio.ImageReader;39import javax.imageio.stream.ImageInputStream;4041import com.sun.imageio.plugins.gif.GIFImageReader;42import com.sun.imageio.plugins.jpeg.JPEGImageReader;43import com.sun.imageio.plugins.png.PNGImageReader;4445public class AllowSearch {46private static void test(ImageReader reader, String format)47throws IOException {48boolean gotISE = false;49File f = null;50ImageInputStream stream = null;51try {52f = File.createTempFile("imageio", ".tmp");53stream = ImageIO.createImageInputStream(f);54reader.setInput(stream, true);5556try {57int numImages = reader.getNumImages(true);58} catch (IOException ioe) {59gotISE = false;60} catch (IllegalStateException ise) {61gotISE = true;62}63} finally {64if (stream != null) {65stream.close();66}6768reader.dispose();6970if (f != null) {71Files.delete(f.toPath());72}73}7475if (!gotISE) {76throw new RuntimeException("Failed to get desired exception for " +77format + " reader!");78}79}8081public static void main(String[] args) throws IOException {82ImageReader gifReader = new GIFImageReader(null);83ImageReader jpegReader = new JPEGImageReader(null);84ImageReader pngReader = new PNGImageReader(null);8586test(gifReader, "GIF");87test(jpegReader, "JPEG");88test(pngReader, "PNG");89}90}919293