Path: blob/master/test/jdk/javax/imageio/plugins/wbmp/StreamResetTest.java
41153 views
/*1* Copyright (c) 2013, 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 802263226* @summary Test verifies that SPI of WBMP image reader27* restores the stream position if an IOException28* occurs during processing of image header.29* @run main StreamResetTest30*/313233import java.io.IOException;34import javax.imageio.ImageIO;35import javax.imageio.ImageReader;36import javax.imageio.spi.ImageReaderSpi;37import javax.imageio.stream.ImageInputStreamImpl;3839public class StreamResetTest {4041public static void main(String[] args) {42IOException expectedException = null;43TestStream iis = new TestStream();4445ImageReader wbmp = ImageIO.getImageReadersByFormatName("WBMP").next();46if (wbmp == null) {47System.out.println("No WBMP reader: skip the test");48return;49}5051ImageReaderSpi spi = wbmp.getOriginatingProvider();5253iis.checkPosition();5455try {56spi.canDecodeInput(iis);57} catch (IOException e) {58expectedException = e;59}6061if (expectedException == null) {62throw new RuntimeException("Test FAILED: stream was not used");63}6465iis.checkPosition();6667System.out.println("Test PASSED");6869}7071private static class TestStream extends ImageInputStreamImpl {72private final int errorPos = 1;7374@Override75public int read() throws IOException {76if (streamPos == errorPos) {77throw new IOException("Test exception");78}79streamPos++;8081return 0x03;82}8384@Override85public int read(byte[] b, int off, int len) throws IOException {86streamPos += len;87return len;88}8990public void checkPosition() {91if (streamPos != 0) {92throw new RuntimeException("Test FAILED");93}94}95}96}979899