Path: blob/master/test/jdk/java/io/RandomAccessFile/skipBytes/SkipBytes.java
41154 views
/*1* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/* @test25@bug 402771726@summary Check for correct implementation of RandomAccessFile.skipBytes27*/282930import java.io.*;3132public class SkipBytes{333435/*36* doTest attempts to skip num_to_skip bytes in raf starting from position 0.37* It also does a read after the skip to check if EOF has been reached38* correctly or incorrectly.39*/404142private static void doTest(RandomAccessFile raf, int start, int num_to_skip)43throws Exception44{4546raf.seek(start);4748long cur_ptr = raf.getFilePointer();49int length = (int) raf.length();50System.err.println("\nCurrent pointer = " + cur_ptr + " length = " +51length + " num_to_skip = " + num_to_skip);5253//Do the Skip test54int num_skipped = raf.skipBytes(num_to_skip);55System.err.println("After skipBytes -- no. skipped = " + num_skipped);5657// if num_to_skip is negative do the negative skip test58if (num_to_skip <= 0) {59if (num_skipped != 0){60System.err.println("Negative Skip Test Failed");61throw new RuntimeException("Negative Skip Test Failed");62}63else {64System.err.println("Negative Skip Test Succeeded");65}66}6768cur_ptr = raf.getFilePointer();69System.err.println("Current pointer = " + cur_ptr);7071// Check if skip has gone beyond EOF.72if (cur_ptr > length) {73System.err.println("Past EOF Skip Test Failed");74throw new RuntimeException("Past EOF Skip Test Failed");75}76else {77System.err.println("Past EOF Skip Test Succeeded");78}7980// do read test81int byte_read = raf.read();82if ( (cur_ptr == length) &&83(byte_read != -1) ) {84System.err.println("byte_read = " + byte_read +85" Read Test Failed ......");86throw new RuntimeException("Read Test Failed");87}88else {89System.err.println("byte_read = " + byte_read +90" Read Test Succeeded");91}9293}9495public static void main(String[] args) throws Exception {9697RandomAccessFile raf = new RandomAccessFile("input.txt" , "rw");98try {99int length = (int)raf.length();100101doTest(raf , 0 , 2*length);102doTest(raf , 0 , length);103doTest(raf , 0 , length/2);104doTest(raf , length/2 , -2);105doTest(raf , length , 0);106doTest(raf , 0 , -1);107} finally{108raf.close();109}110111}112113}114115116