0N/A/*
3261N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/A/* @test
0N/A @bug 4027717
0N/A @summary Check for correct implementation of RandomAccessFile.skipBytes
0N/A */
0N/A
0N/A
0N/Aimport java.io.*;
0N/A
0N/Apublic class SkipBytes{
0N/A
0N/A
0N/A /*
0N/A * doTest attempts to skip num_to_skip bytes in raf starting from position 0.
0N/A * It also does a read after the skip to check if EOF has been reached
0N/A * correctly or incorrectly.
0N/A */
0N/A
0N/A
0N/A private static void doTest(RandomAccessFile raf, int start, int num_to_skip)
0N/A throws Exception
0N/A {
0N/A
0N/A raf.seek(start);
0N/A
0N/A long cur_ptr = raf.getFilePointer();
0N/A int length = (int) raf.length();
0N/A System.err.println("\nCurrent pointer = " + cur_ptr + " length = " +
0N/A length + " num_to_skip = " + num_to_skip);
0N/A
0N/A //Do the Skip test
0N/A int num_skipped = raf.skipBytes(num_to_skip);
0N/A System.err.println("After skipBytes -- no. skipped = " + num_skipped);
0N/A
0N/A // if num_to_skip is negative do the negative skip test
0N/A if (num_to_skip <= 0) {
0N/A if (num_skipped != 0){
0N/A System.err.println("Negative Skip Test Failed");
0N/A throw new RuntimeException("Negative Skip Test Failed");
0N/A }
0N/A else {
0N/A System.err.println("Negative Skip Test Succeeded");
0N/A }
0N/A }
0N/A
0N/A cur_ptr = raf.getFilePointer();
0N/A System.err.println("Current pointer = " + cur_ptr);
0N/A
0N/A // Check if skip has gone beyond EOF.
0N/A if (cur_ptr > length) {
0N/A System.err.println("Past EOF Skip Test Failed");
0N/A throw new RuntimeException("Past EOF Skip Test Failed");
0N/A }
0N/A else {
0N/A System.err.println("Past EOF Skip Test Succeeded");
0N/A }
0N/A
0N/A // do read test
0N/A int byte_read = raf.read();
0N/A if ( (cur_ptr == length) &&
0N/A (byte_read != -1) ) {
0N/A System.err.println("byte_read = " + byte_read +
0N/A " Read Test Failed ......");
0N/A throw new RuntimeException("Read Test Failed");
0N/A }
0N/A else {
0N/A System.err.println("byte_read = " + byte_read +
0N/A " Read Test Succeeded");
0N/A }
0N/A
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A RandomAccessFile raf = new RandomAccessFile("input.txt" , "rw");
2497N/A try {
2497N/A int length = (int)raf.length();
0N/A
2497N/A doTest(raf , 0 , 2*length);
2497N/A doTest(raf , 0 , length);
2497N/A doTest(raf , 0 , length/2);
2497N/A doTest(raf , length/2 , -2);
2497N/A doTest(raf , length , 0);
2497N/A doTest(raf , 0 , -1);
2497N/A } finally{
2497N/A raf.close();
2497N/A }
0N/A
0N/A }
0N/A
0N/A}