|
1 /* |
|
2 * Copyright 1997 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 |
|
25 /* @test |
|
26 @bug 4027717 |
|
27 @summary Check for correct implementation of RandomAccessFile.skipBytes |
|
28 */ |
|
29 |
|
30 |
|
31 import java.io.*; |
|
32 |
|
33 public class SkipBytes{ |
|
34 |
|
35 |
|
36 /* |
|
37 * doTest attempts to skip num_to_skip bytes in raf starting from position 0. |
|
38 * It also does a read after the skip to check if EOF has been reached |
|
39 * correctly or incorrectly. |
|
40 */ |
|
41 |
|
42 |
|
43 private static void doTest(RandomAccessFile raf, int start, int num_to_skip) |
|
44 throws Exception |
|
45 { |
|
46 |
|
47 raf.seek(start); |
|
48 |
|
49 long cur_ptr = raf.getFilePointer(); |
|
50 int length = (int) raf.length(); |
|
51 System.err.println("\nCurrent pointer = " + cur_ptr + " length = " + |
|
52 length + " num_to_skip = " + num_to_skip); |
|
53 |
|
54 //Do the Skip test |
|
55 int num_skipped = raf.skipBytes(num_to_skip); |
|
56 System.err.println("After skipBytes -- no. skipped = " + num_skipped); |
|
57 |
|
58 // if num_to_skip is negative do the negative skip test |
|
59 if (num_to_skip <= 0) { |
|
60 if (num_skipped != 0){ |
|
61 System.err.println("Negative Skip Test Failed"); |
|
62 throw new RuntimeException("Negative Skip Test Failed"); |
|
63 } |
|
64 else { |
|
65 System.err.println("Negative Skip Test Succeeded"); |
|
66 } |
|
67 } |
|
68 |
|
69 cur_ptr = raf.getFilePointer(); |
|
70 System.err.println("Current pointer = " + cur_ptr); |
|
71 |
|
72 // Check if skip has gone beyond EOF. |
|
73 if (cur_ptr > length) { |
|
74 System.err.println("Past EOF Skip Test Failed"); |
|
75 throw new RuntimeException("Past EOF Skip Test Failed"); |
|
76 } |
|
77 else { |
|
78 System.err.println("Past EOF Skip Test Succeeded"); |
|
79 } |
|
80 |
|
81 // do read test |
|
82 int byte_read = raf.read(); |
|
83 if ( (cur_ptr == length) && |
|
84 (byte_read != -1) ) { |
|
85 System.err.println("byte_read = " + byte_read + |
|
86 " Read Test Failed ......"); |
|
87 throw new RuntimeException("Read Test Failed"); |
|
88 } |
|
89 else { |
|
90 System.err.println("byte_read = " + byte_read + |
|
91 " Read Test Succeeded"); |
|
92 } |
|
93 |
|
94 } |
|
95 |
|
96 public static void main(String[] args) throws Exception { |
|
97 |
|
98 RandomAccessFile raf = new RandomAccessFile("input.txt" , "rw"); |
|
99 int length = (int)raf.length(); |
|
100 |
|
101 doTest(raf , 0 , 2*length); |
|
102 doTest(raf , 0 , length); |
|
103 doTest(raf , 0 , length/2); |
|
104 doTest(raf , length/2 , -2); |
|
105 doTest(raf , length , 0); |
|
106 doTest(raf , 0 , -1); |
|
107 |
|
108 } |
|
109 |
|
110 } |