jdk/test/javax/imageio/stream/ShortStreamTest.java
author prr
Thu, 18 Dec 2014 10:45:45 -0800
changeset 29908 83e2c403fefd
parent 29733 70809c7bf9e5
permissions -rw-r--r--
8067050: Better font consistency checking Reviewed-by: bae, srl, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29733
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
     1
/*
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
     4
 *
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
     7
 * published by the Free Software Foundation.
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
     8
 *
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    13
 * accompanied this code).
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    14
 *
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    18
 *
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    21
 * questions.
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    22
 */
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    23
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    24
/**
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    25
 * @test
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    26
 * @bug     8074954
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    27
 * @summary Test verifies that an IOException is triggered if input stream
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    28
 *          does not contain enough data to read a multi-byte type.
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    29
 *
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    30
 * @run     main ShortStreamTest
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    31
 */
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    32
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    33
import javax.imageio.ImageIO;
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    34
import javax.imageio.stream.ImageInputStream;
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    35
import java.io.ByteArrayInputStream;
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    36
import java.io.IOException;
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    37
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    38
public class ShortStreamTest {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    39
    public static void main(String[] args) throws IOException {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    40
        TestCase[]  tests = createTests();
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    41
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    42
        for (TestCase t : tests) {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    43
            t.test();
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    44
        }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    45
    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    46
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    47
    private static abstract class TestCase {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    48
        abstract void testRead(ImageInputStream iis) throws IOException;
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    49
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    50
        public void test() {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    51
            boolean gotException = false;
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    52
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    53
            ImageInputStream iis = createShortStream();
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    54
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    55
            try {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    56
                testRead(iis);
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    57
            } catch (IOException e) {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    58
                e.printStackTrace(System.out);
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    59
                gotException = true;
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    60
            }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    61
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    62
            if (!gotException) {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    63
                throw new RuntimeException("Test failed.");
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    64
            }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    65
            System.out.println("Test PASSED");
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    66
        }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    67
    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    68
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    69
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    70
    private static ImageInputStream createShortStream() {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    71
        try {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    72
            byte[] integerTestArray = new byte[] { 80 };
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    73
            ByteArrayInputStream bais = new ByteArrayInputStream(integerTestArray);
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    74
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    75
            return ImageIO.createImageInputStream(bais);
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    76
        } catch (IOException e) {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    77
            return null;
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    78
        }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    79
    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    80
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    81
    private static TestCase[] createTests() {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    82
        return new TestCase[]{
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    83
                new TestCase() {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    84
                    @Override
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    85
                    void testRead(ImageInputStream iis) throws IOException {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    86
                        iis.readInt();
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    87
                    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    88
                },
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    89
                new TestCase() {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    90
                    @Override
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    91
                    void testRead(ImageInputStream iis) throws IOException {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    92
                        iis.readShort();
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    93
                    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    94
                },
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    95
                new TestCase() {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    96
                    @Override
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    97
                    void testRead(ImageInputStream iis) throws IOException {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    98
                        iis.readDouble();
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
    99
                    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   100
                },
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   101
                new TestCase() {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   102
                    @Override
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   103
                    void testRead(ImageInputStream iis) throws IOException {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   104
                        iis.readFloat();
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   105
                    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   106
                },
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   107
                new TestCase() {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   108
                    @Override
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   109
                    void testRead(ImageInputStream iis) throws IOException {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   110
                        iis.readLong();
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   111
                    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   112
                },
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   113
                new TestCase() {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   114
                    @Override
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   115
                    void testRead(ImageInputStream iis) throws IOException {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   116
                        iis.readUnsignedInt();
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   117
                    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   118
                },
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   119
                new TestCase() {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   120
                    @Override
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   121
                    void testRead(ImageInputStream iis) throws IOException {
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   122
                        iis.readUnsignedShort();
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   123
                    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   124
                }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   125
        };
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   126
    }
70809c7bf9e5 8074954: ImageInputStreamImpl.readShort/readInt do not behave correctly at EOF
bae
parents:
diff changeset
   127
}