jdk/test/java/io/readBytes/ReadBytesBounds.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1768 27f2d52ea088
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
  @bug 4017728 4079849
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
  @summary Check for correct Array Bounds check in read of FileInputStream and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
  RandomAccessFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The test calls the read(byte buf[] , int off , int len) of FileInputStream with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * different values of off and len to see if the ArrayOutOfBoundsException is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * thrown according to the JLS1.0 specification.  The read(...) method calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * readBytes(...) in native code(io_util.c).  The read(...) method in RandomAccessFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * also calls the same native method.  So one should see similar results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class ReadBytesBounds {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static void main(String argv[]) throws Exception{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        int num_test_cases = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        int off[] =     {-1 , -1 ,  0 , 0  , 33 , 33 , 0  , 32 , 32 , 4  , 1  , 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int len[] =     {-1 ,  0 , -1 , 33 , 0  , 4  , 32 , 0  , 4  , 16 , 31 , 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        boolean results[] = { false ,  false ,  false , false  , false  , false  ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                              true  , true  , false  , true  , true  , true};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        FileInputStream fis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        RandomAccessFile raf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        byte b[] = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        int num_good = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int num_bad = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        String dir = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        File testFile = new File(dir, "input.txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        fis = new FileInputStream(testFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for(int i = 0; i < num_test_cases; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                int bytes_read = fis.read(b , off[i] , len[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            } catch(IndexOutOfBoundsException aiobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                if (results[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    throw new RuntimeException("Unexpected result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    num_good++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (results[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                num_good++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                throw new RuntimeException("Unexpected result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        System.out.println("Results for FileInputStream.read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.out.println("\nTotal number of test cases = " + num_test_cases +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                           "\nNumber succeded = " + num_good +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                           "\nNumber failed   = " + num_bad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        num_good = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        num_bad = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        raf = new RandomAccessFile(testFile , "r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        for(int i = 0; i < num_test_cases; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                int bytes_read = raf.read(b , off[i] , len[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            } catch(IndexOutOfBoundsException aiobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (results[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    throw new RuntimeException("Unexpected result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    num_good++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (results[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                num_good++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                throw new RuntimeException("Unexpected result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println("Results for RandomAccessFile.read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.out.println("\nTotal number of test cases = " + num_test_cases +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                           "\nNumber succeded = " + num_good +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                           "\nNumber failed   = " + num_bad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}