1/*
2 * Copyright (c) 2013, 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     8022632
27 * @summary Test verifies that SPI of WBMP image reader
28 *           restores the stream position if an IOException
29 *           occurs during processing of image header.
30 * @run     main StreamResetTest
31 */
32
33
34import java.io.IOException;
35import javax.imageio.ImageIO;
36import javax.imageio.ImageReader;
37import javax.imageio.spi.ImageReaderSpi;
38import javax.imageio.stream.ImageInputStreamImpl;
39
40public class StreamResetTest {
41
42    public static void main(String[] args) {
43        IOException expectedException = null;
44        TestStream iis = new TestStream();
45
46        ImageReader wbmp = ImageIO.getImageReadersByFormatName("WBMP").next();
47        if (wbmp == null) {
48            System.out.println("No WBMP reader: skip the test");
49            return;
50        }
51
52        ImageReaderSpi spi = wbmp.getOriginatingProvider();
53
54        iis.checkPosition();
55
56        try {
57            spi.canDecodeInput(iis);
58        } catch (IOException e) {
59            expectedException = e;
60        }
61
62        if (expectedException == null) {
63            throw new RuntimeException("Test FAILED: stream was not used");
64        }
65
66        iis.checkPosition();
67
68        System.out.println("Test PASSED");
69
70    }
71
72    private static class TestStream extends ImageInputStreamImpl {
73        private final int errorPos = 1;
74
75        @Override
76        public int read() throws IOException {
77            if (streamPos == errorPos) {
78                throw new IOException("Test exception");
79            }
80            streamPos++;
81
82            return 0x03;
83        }
84
85        @Override
86        public int read(byte[] b, int off, int len) throws IOException {
87            streamPos += len;
88            return len;
89        }
90
91        public void checkPosition() {
92            if (streamPos != 0) {
93                throw new RuntimeException("Test FAILED");
94            }
95        }
96    }
97}
98