jdk/test/java/io/InputStream/ReadParams.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5810 e83d67ad8c96
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5810
diff changeset
     2
 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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 4008296 4008293 4190090 4193729
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check for correct handling of parameters to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          XXXXInputStream.read(b, off, len).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.zip.ZipInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.zip.InflaterInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.zip.DeflaterOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ReadParams {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /* check for correct handling of different values of off and len */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void doTest(InputStream in) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        int off[] = {-1, -1,  0, 0, 33, 33, 0, 32, 32, 4, 1, 0,  -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                     Integer.MAX_VALUE, 1, Integer.MIN_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                     Integer.MIN_VALUE, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        int len[] = {-1,  0, -1, 33, 0, 4, 32, 0, 4, 16, 31, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                     Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                     1, -1, Integer.MIN_VALUE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        boolean results[] = { false,  false,  false, false, false, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                              true, true, false, true, true, true,  false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                              false, false, false, false, false};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        int numCases = off.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        byte b[] = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int numBad = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        for(int i = 0; i < numCases; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                in.read(b , off[i] , len[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            } catch (IndexOutOfBoundsException aiobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                if (results[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    System.err.println("Error:IndexOutOfBoundsException thrown"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                       " for read(b, " + off[i] + " " + len[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                       " ) on " + in + "\nb.length = 32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    numBad++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    /* System.err.println("PassE: " + off[i] + " " + len[i]); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            } catch (OutOfMemoryError ome) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                System.err.println("Error: OutOfMemoryError in read(b, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                   off[i] + " " + len[i] + " ) on " + in +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                   "\nb.length = 32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                numBad++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (!results[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                System.err.println("Error:No IndexOutOfBoundsException thrown"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                   " for read(b, " + off[i] + " " + len[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                   " ) on " + in + "\nb.length = 32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                numBad++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                /* System.err.println("Pass: " + off[i] + " " + len[i]); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (numBad > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new RuntimeException(in + " Failed " + numBad + " cases");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            System.err.println("Successfully completed bounds tests on " + in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /* check for correct handling of null b */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static void doTest1(InputStream in) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        byte b[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            in.read(b, 0, 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.err.println("SuccessFully completed null b test on " + in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        throw new RuntimeException(in + " Failed null b test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static void main(String args[]) throws Exception{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /* initialise stuff */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        File fn = new File("x.ReadBounds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        FileOutputStream fout = new FileOutputStream(fn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        for (int i = 0; i < 32; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            fout.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        fout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        byte b[] = new byte[64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for(int i = 0; i < 64; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            b[i] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        /* test all input streams */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        FileInputStream fis = new FileInputStream(fn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        doTest(fis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        doTest1(fis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        BufferedInputStream bis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            new BufferedInputStream(new MyInputStream(1024));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        doTest(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        doTest1(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        bis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        ByteArrayInputStream bais = new ByteArrayInputStream(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        doTest(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        doTest1(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        FileOutputStream fos = new FileOutputStream(fn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        ObjectOutputStream oos = new ObjectOutputStream(fos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        oos.writeInt(12345);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        oos.writeObject("Today");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        oos.writeObject(new Integer(32));
5810
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   140
        oos.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        doTest(ois);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        doTest1(ois);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ois.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        DataInputStream dis = new DataInputStream(new MyInputStream(1024));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        doTest(dis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        doTest1(dis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        LineNumberInputStream lis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            new LineNumberInputStream(new MyInputStream(1024));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        doTest(lis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        doTest1(lis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        lis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        PipedOutputStream pos = new PipedOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        PipedInputStream pis = new PipedInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        pos.connect(pis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        pos.write(b, 0, 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        doTest(pis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        doTest1(pis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        pis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        PushbackInputStream pbis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            new PushbackInputStream(new MyInputStream(1024));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        doTest(pbis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        doTest1(pbis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        pbis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        StringBufferInputStream sbis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            new StringBufferInputStream(new String(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        doTest(sbis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        doTest1(sbis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        sbis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        SequenceInputStream sis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            new SequenceInputStream(new MyInputStream(1024),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                    new MyInputStream(1024));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        doTest(sis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        doTest1(sis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        sis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        ZipInputStream zis = new ZipInputStream(new FileInputStream(fn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        doTest(zis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        doTest1(zis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        zis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        byte[] data = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        DeflaterOutputStream dos = new DeflaterOutputStream(bos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        dos.write(data, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        InflaterInputStream ifs = new InflaterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            (new ByteArrayInputStream(bos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        doTest(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        doTest1(ifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        ifs.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        /* cleanup */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        fn.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
/* An InputStream class used in the above tests */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
class MyInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private int readctr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private long endoffile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public MyInputStream(long endoffile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        this.endoffile = endoffile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public int read() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (readctr == endoffile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            readctr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public int available() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}