test/jdk/java/io/OutputStream/WriteParams.java
author tschatzl
Wed, 24 Jul 2019 11:49:39 +0200
changeset 57508 28ab01c06755
parent 48461 6a1c3a5e04f3
permissions -rw-r--r--
8228388: Add information about dirty/skipped card for Merge HCC in G1 log Summary: Collect and print informatio about the number of processed cards during the Merge HCC phase to improve log output. Reviewed-by: kbarrett, sangheki
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48461
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2018, 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
48461
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 47216
diff changeset
    26
 * @bug 1267039 1267043 4193729 4358774
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check for correct handling of parameters to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          XXXXOutputStream.write(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.DeflaterOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class WriteParams {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    /* check for correct handling of different values of off and len */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public static void doTest(OutputStream out) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        int off[] = {-1, -1,  0, 0, 33, 33, 0, 32, 32, 4, 1, 0,  -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                     Integer.MAX_VALUE, 1, Integer.MIN_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                     Integer.MIN_VALUE, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        int len[] = {-1,  0, -1, 33, 0, 4, 32, 0, 4, 16, 31, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                     Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                     1, -1, Integer.MIN_VALUE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        boolean results[] = { false,  false,  false, false, false, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                              true, true, false, true, true, true,  false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                              false, false, false, false, false};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int numCases = off.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        byte b[] = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        int numBad = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        for(int i = 0; i < numCases; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                out.write(b , off[i] , len[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            } catch (IndexOutOfBoundsException aiobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                if (results[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    System.err.println("Error:IndexOutOfBoundsException thrown"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                       " for write(b, " + off[i] + " " + len[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                       " ) on " + out + "\nb.length = 32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    numBad++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    /* System.err.println("PassE: " + off[i] + " " + len[i]); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            } catch (OutOfMemoryError ome) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                System.err.println("Error: OutOfMemoryError in write(b, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                   off[i] + " " + len[i] + " ) on " + out +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                   "\nb.length = 32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                numBad++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (!results[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                System.err.println("Error:No IndexOutOfBoundsException thrown"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                   " for write(b, " + off[i] + " " + len[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                   " ) on " + out + "\nb.length = 32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                numBad++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                /* System.err.println("Pass: " + off[i] + " " + len[i]); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (numBad > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new RuntimeException(out + " Failed " + numBad + " cases");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            System.err.println("Successfully completed bounds tests on " + out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* check for correct handling of null b */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static void doTest1(OutputStream out) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        byte b[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            out.write(b, 0, 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            System.err.println("SuccessFully completed null b test on " + out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        throw new RuntimeException(out + " Failed null b test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static void main(String args[]) throws Exception{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        /* initialise stuff here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        File fn = new File("x.WriteBounds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        FileOutputStream fout = new FileOutputStream(fn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        for (int i = 0; i < 32; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            fout.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        fout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        byte b[] = new byte[64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for(int i = 0; i < 64; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            b[i] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        /* test for different output streams */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        FileOutputStream fos = new FileOutputStream(fn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        doTest(fos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        doTest1(fos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        ObjectOutputStream oos = new ObjectOutputStream(new MyOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        doTest(oos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        doTest1(oos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        oos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        BufferedOutputStream bos =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            new BufferedOutputStream(new MyOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        doTest(bos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        doTest1(bos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        bos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        doTest(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        doTest1(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        baos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        DataOutputStream dos = new DataOutputStream(new MyOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        doTest(dos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        doTest1(dos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        PipedInputStream pis = new PipedInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        PipedOutputStream pos = new PipedOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        pos.connect(pis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        doTest(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        doTest1(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        pos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        DeflaterOutputStream dfos = new DeflaterOutputStream(new MyOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        doTest(dfos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        doTest1(dfos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        dfos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
48461
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 47216
diff changeset
   155
        OutputStream nos = OutputStream.nullOutputStream();
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 47216
diff changeset
   156
        doTest(nos);
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 47216
diff changeset
   157
        doTest1(nos);
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 47216
diff changeset
   158
        nos.close();
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 47216
diff changeset
   159
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        /* cleanup */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        fn.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
/* An OutputStream class used in the above tests */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
class MyOutputStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public MyOutputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}