jdk/test/java/io/RandomAccessFile/ReadWritePrimitives.java
author duke
Wed, 05 Jul 2017 22:06:37 +0200
changeset 40471 ad45133ff99e
parent 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 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
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
   @bug 4092350
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @summary Verify that reads and writes of primitives are correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
// The bug mentioned is actually a performance bug that prompted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
// changes in the methods to write primitives
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
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class ReadWritePrimitives {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main(String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        long start, finish;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        testShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        finish = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        // System.err.println("Time taken="+(finish-start));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        testChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        finish = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        // System.err.println("Time taken="+(finish-start));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        testInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        finish = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // System.err.println("Time taken="+(finish-start));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        testLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        finish = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // System.err.println("Time taken="+(finish-start));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static void testShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        File fh = new File(System.getProperty("test.dir", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                          "x.ReadWriteGenerated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        RandomAccessFile f = new RandomAccessFile(fh,"rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for(int i = 0; i < 10000; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            f.writeShort((short)i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        f.writeShort((short)65535);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        f.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        f = new RandomAccessFile(fh,"r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        for(int i = 0; i < 10000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            short r = f.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (r != ((short)i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                System.err.println("An error occurred. Read:" + r
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                   + " i:" + ((short)i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                throw new IOException("Bad read from a writeShort");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        short rmax = f.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (rmax != ((short)65535)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.err.println("An error occurred. Read:" + rmax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new IOException("Bad read from a writeShort");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        f.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static void testChar() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        File fh = new File(System.getProperty("test.dir", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                          "x.ReadWriteGenerated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        RandomAccessFile f = new RandomAccessFile(fh,"rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for(int i = 0; i < 10000; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            f.writeChar((char)i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        f.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        f = new RandomAccessFile(fh,"r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for(int i = 0; i < 10000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            char r = f.readChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (r != ((char)i)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                System.err.println("An error occurred. Read:" + r
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                   + " i:" + ((char) i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                throw new IOException("Bad read from a writeChar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        f.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static void testInt() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        File fh = new File(System.getProperty("test.dir", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                          "x.ReadWriteGenerated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        RandomAccessFile f = new RandomAccessFile(fh,"rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        for(int i = 0; i < 10000; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            f.writeInt((short)i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        f.writeInt(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        f.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        f = new RandomAccessFile(fh, "r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for(int i = 0; i < 10000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            int r = f.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (r != i){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                System.err.println("An error occurred. Read:" + r
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                   + " i:" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                throw new IOException("Bad read from a writeInt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int rmax = f.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (rmax != Integer.MAX_VALUE){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.err.println("An error occurred. Read:" + rmax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new IOException("Bad read from a writeInt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        f.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static void testLong() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        File fh = new File(System.getProperty("test.dir", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                          "x.ReadWriteGenerated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        RandomAccessFile f = new RandomAccessFile(fh,"rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        for(int i = 0; i < 10000; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            f.writeLong(123456789L * (long)i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        f.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        f = new RandomAccessFile(fh,"r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for(int i = 0; i < 10000; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            long r = f.readLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (r != (((long) i) * 123456789L) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.err.println("An error occurred. Read:" + r
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                   + " i" + ((long) i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                throw new IOException("Bad read from a writeInt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        f.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}