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