jdk/test/java/io/InputStream/Skip.java
author jbachorik
Tue, 21 Jan 2014 13:04:55 +0100
changeset 22353 d09e3ff5fd63
parent 5506 202f599c92aa
permissions -rw-r--r--
8032377: test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java still fails intermittently Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 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 4016710
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
   @summary check for correct implementation of InputStream.skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class Skip{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static void dotest(InputStream in , int curpos ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
                               long total , long toskip , long expected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            System.err.println("\n\nCurrently at pos = " + curpos +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                               "\nTotal bytes in the Stream = " + total +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                               "\nNumber of bytes to skip = " + toskip +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                               "\nNumber of bytes that should be skipped = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                               expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            long skipped = in.skip(toskip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            System.err.println("actual number skipped: "+ skipped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            if ((skipped < 0) || (skipped > expected)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                throw new RuntimeException("Unexpected number of bytes skipped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            System.err.println("IOException is thrown - possible result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throw new RuntimeException("Unexpected "+e+" is thrown!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static void main( String argv[] ) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        MyInputStream in = new MyInputStream(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        /* test for negative skip */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        dotest(in,  0, 11, -23,  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        /* check for skip beyond EOF starting from before EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        dotest(in,  0, 11,  20, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        /* check for skip after EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        dotest(in, -1, 11,  20,  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        in = new MyInputStream(9000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        /* check for skip equal to the read chunk size in InputStream.java */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        dotest(in,  0, 9000, 2048, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        /* check for skip greater than the read chunk size in InputStream.java */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        dotest(in, 2048, 9000, 5000, 5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        /* check for skip beyond EOF starting from before EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        dotest(in, 7048, 9000, 5000, 1952);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        in = new MyInputStream(5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        /* check for multiple chunk reads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        dotest(in, 0, 5000, 6000, 5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * check for skip larger than Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * (Takes about 2 hrs on a sparc ultra-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         * long total = (long)Integer.MAX_VALUE + (long)10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         * long toskip = total - (long)6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * in = new MyInputStream(total);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * dotest(in, 0, total, toskip, toskip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
class MyInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private int readctr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private long endoffile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public MyInputStream(long endoffile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.endoffile = endoffile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public int read() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (readctr == endoffile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            readctr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public int available() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}