Path: blob/master/test/jdk/javax/imageio/ImageReaderReadAll.java
41145 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 445031926* @summary Checks that ImageReader.readAll(int, ImageReadParam) makes use of27* the ImageReadParam object28*/2930import java.awt.image.BufferedImage;31import java.io.ByteArrayInputStream;32import java.io.IOException;33import java.util.Iterator;34import java.util.Vector;3536import javax.imageio.IIOImage;37import javax.imageio.ImageReadParam;38import javax.imageio.ImageReader;39import javax.imageio.ImageTypeSpecifier;40import javax.imageio.metadata.IIOMetadata;41import javax.imageio.spi.ImageReaderSpi;42import javax.imageio.stream.MemoryCacheImageInputStream;4344public class ImageReaderReadAll {4546private final static byte[] ba = {};4748public static void main(String argv[]) {49ImageReader ireader;50ImageReadParam irp;51IIOImage image;52BufferedImage bi;53BufferedImage bi_1;54BufferedImage bi_2;5556ireader = new DummyImageReaderImpl(null);57MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream58(new ByteArrayInputStream(ba));59ireader.setInput(mciis);6061irp = new ImageReadParam();62irp.setDestination(new BufferedImage(10, 10,63BufferedImage.TYPE_3BYTE_BGR));64try {65image = ireader.readAll(0, irp);66bi_1 = ireader.read(0, irp);67bi_2 = ireader.read(0);68} catch (java.io.IOException ee) {69throw new RuntimeException("Unexpected exception: " + ee);70}7172bi = (BufferedImage)image.getRenderedImage();73if (bi.getType() != bi_1.getType()) {74throw new RuntimeException("Images have different type!");75}76}777879public static class DummyImageReaderImpl extends ImageReader {8081public DummyImageReaderImpl(ImageReaderSpi originatingProvider) {82super(originatingProvider);83}8485public BufferedImage read(int imageIndex, ImageReadParam param)86throws IOException {87if (input == null)88throw new IllegalStateException();89if (imageIndex >= 1 || imageIndex < 0)90throw new IndexOutOfBoundsException();91if (seekForwardOnly) {92if (imageIndex < minIndex)93throw new IndexOutOfBoundsException();94minIndex = imageIndex;95}9697return getDestination(param, getImageTypes(imageIndex), 10, 15);98}99100public Iterator getImageTypes(int imageIndex) throws IOException {101if (input == null)102throw new IllegalStateException();103if (imageIndex >= 1 || imageIndex < 0)104throw new IndexOutOfBoundsException();105106Vector imageTypes = new Vector();107imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType108(BufferedImage.TYPE_BYTE_GRAY ));109return imageTypes.iterator();110}111112public int getNumImages(boolean allowSearch) throws IOException {return 1;}113public int getWidth(int imageIndex) throws IOException {return 1;}114public int getHeight(int imageIndex) throws IOException {return 1;}115public IIOMetadata getStreamMetadata() throws IOException {return null;}116public IIOMetadata getImageMetadata(int imageIndex)117throws IOException {return null;}118}119}120121122