1 /*
2  * Copyright (c) 2010, 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 6720170
27  * @summary check for ByteArrayInputStream.skip
28  */
29 
30 import java.io.*;
31 
32 public class Skip {
dotest(InputStream in, int curpos, long total, long toskip, long expected)33     private static void dotest(InputStream in, int curpos, long total,
34                                long toskip, long expected)
35         throws Exception
36     {
37         System.err.println("\nCurrently at pos = " + curpos +
38                            "\nTotal bytes in the stream = " + total +
39                            "\nNumber of bytes to skip = " + toskip +
40                            "\nNumber of bytes that should be skipped = " +
41                            expected);
42 
43         // position to curpos; EOF if negative
44         in.reset();
45         int avail = curpos >= 0 ? curpos : in.available();
46         long n = in.skip(avail);
47         if (n != avail) {
48             throw new RuntimeException("Unexpected number of bytes skipped = " + n);
49         }
50 
51         long skipped = in.skip(toskip);
52         System.err.println("actual number skipped: "+ skipped);
53 
54         if (skipped != expected) {
55             throw new RuntimeException("Unexpected number of bytes skipped = " + skipped);
56         }
57     }
58 
main(String argv[])59     public static void main(String argv[]) throws Exception {
60         int total = 1024;
61         ByteArrayInputStream in = new ByteArrayInputStream(new byte[total]);
62 
63         /* test for skip */
64         dotest(in,  0, total, 23, 23);
65         dotest(in,  10, total, 23, 23);
66 
67         /* test for negative skip */
68         dotest(in,  0, total, -23,  0);
69 
70         /* check for skip after EOF */
71         dotest(in, -1, total,  20,  0);
72 
73         /* check for skip beyond EOF starting from before EOF */
74         dotest(in,  0, total, total+20, total);
75 
76         /* check for skip if the pos + toskip causes integer overflow */
77         dotest(in, 10, total, Long.MAX_VALUE, total-10);
78     }
79 }
80