jdk/test/java/io/RandomAccessFile/skipBytes/SkipBytes.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5810 e83d67ad8c96
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5810
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @bug 4027717
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
   @summary Check for correct implementation of RandomAccessFile.skipBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class SkipBytes{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     * doTest attempts to skip num_to_skip bytes in raf starting from position 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * It also does a read after the skip to check if EOF has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * correctly or incorrectly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static void doTest(RandomAccessFile raf, int start, int num_to_skip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        raf.seek(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        long cur_ptr = raf.getFilePointer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        int length = (int) raf.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.err.println("\nCurrent pointer = " + cur_ptr + " length = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                           length + " num_to_skip = " + num_to_skip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        //Do the Skip test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int num_skipped = raf.skipBytes(num_to_skip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        System.err.println("After skipBytes -- no. skipped = " + num_skipped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // if num_to_skip is negative do the negative skip test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (num_to_skip <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (num_skipped != 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                System.err.println("Negative Skip Test Failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                throw new RuntimeException("Negative Skip Test Failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                System.err.println("Negative Skip Test Succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        cur_ptr = raf.getFilePointer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.err.println("Current pointer = " + cur_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // Check if skip has gone beyond EOF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (cur_ptr > length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.err.println("Past EOF Skip Test Failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new RuntimeException("Past EOF Skip Test Failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.err.println("Past EOF Skip Test Succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // do read test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int byte_read = raf.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if ( (cur_ptr == length) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
             (byte_read != -1) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.err.println("byte_read = " + byte_read +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                               " Read Test Failed ......");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new RuntimeException("Read Test Failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.err.println("byte_read = " + byte_read +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                               " Read Test Succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        RandomAccessFile raf = new RandomAccessFile("input.txt" , "rw");
5810
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
    99
        try {
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   100
            int length = (int)raf.length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
5810
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   102
            doTest(raf , 0 , 2*length);
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   103
            doTest(raf , 0 , length);
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   104
            doTest(raf , 0 , length/2);
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   105
            doTest(raf , length/2 , -2);
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   106
            doTest(raf , length , 0);
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   107
            doTest(raf , 0 , -1);
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   108
        } finally{
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   109
            raf.close();
e83d67ad8c96 6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents: 5506
diff changeset
   110
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}