1/*
2 * Copyright (c) 1998, 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 4127657
27 * @summary Check for correct handling of parameters to
28 *          XXXXReader.read(b, off, len).
29 *
30 */
31
32import java.io.*;
33
34public class ReadParams {
35    static int values[] = {Integer.MIN_VALUE, -1, 0, 1, 4, 16, 31,
36                           32, 33, Integer.MAX_VALUE};
37    static char b[][] = {null, new char[32]};
38
39    static void test(Reader rdr) throws Exception {
40        int i = 0, j = 0, k = 0;
41        boolean nullPtr = false, indexOutBnd = false;
42
43        for (i = 0; i < b.length; i++) {
44            for ( j = 0; j < values.length; j++) {
45                for ( k = 0; k < values.length; k++) {
46
47                    nullPtr = (b[i] == null);
48
49                    int bufLen = nullPtr ? 0 : b[i].length;
50                    indexOutBnd = ((values[j] + values[k]) < 0)
51                        ||(values[j] < 0)
52                        || (values[j] > bufLen)
53                        || (values[k] < 0)
54                        || ((values[j] + values[k]) > bufLen);
55
56                    try {
57                        rdr.read(b[i], values[j], values[k]);
58                    } catch (NullPointerException e) {
59                        if (!nullPtr) {
60                            throw new Exception
61                                ("should not throw NullPointerException" + i + " " +j + " " + k);
62                        }
63                        continue;
64                    } catch (IndexOutOfBoundsException e) {
65                        if (!indexOutBnd) {
66                            throw new Exception
67                                ("should not throw IndexOutOfBoundsException");
68                        }
69                        continue;
70                    }
71
72                    if (nullPtr || indexOutBnd) {
73                        throw new Exception("Should have thrown an exception");
74                    }
75                }
76            }
77        }
78    }
79
80    public static void main(String args[]) throws Exception{
81        StringReader sr = new StringReader(new String(new byte[512]));
82        test(sr);
83
84        test(new BufferedReader(sr));
85
86        test(new CharArrayReader(new char[8]));
87
88        InputStreamReader ir = new InputStreamReader
89            (new ByteArrayInputStream(new byte[512]));
90        test(ir);
91
92        test(new PushbackReader(sr, 2));
93
94        PipedWriter pw = new PipedWriter();
95        PipedReader pir = new PipedReader(pw);
96        pw.write(new char[512], 0, 512);
97        test(pir);
98    }
99}
100