Path: blob/master/test/jdk/java/io/RandomAccessFile/ReadWritePrimitives.java
41152 views
/*1* Copyright (c) 1998, 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*/2223/* @test24@bug 409235025@summary Verify that reads and writes of primitives are correct26*/2728// The bug mentioned is actually a performance bug that prompted29// changes in the methods to write primitives30import java.io.*;3132import java.io.*;3334public class ReadWritePrimitives {3536public static void main(String args[]) throws IOException {37long start, finish;38start = System.currentTimeMillis();39testShort();40finish = System.currentTimeMillis();41// System.err.println("Time taken="+(finish-start));42start = System.currentTimeMillis();43testChar();44finish = System.currentTimeMillis();45// System.err.println("Time taken="+(finish-start));46start = System.currentTimeMillis();47testInt();48finish = System.currentTimeMillis();49// System.err.println("Time taken="+(finish-start));50start = System.currentTimeMillis();51testLong();52finish = System.currentTimeMillis();53// System.err.println("Time taken="+(finish-start));54}5556private static void testShort() throws IOException {57File fh = new File(System.getProperty("test.dir", "."),58"x.ReadWriteGenerated");59RandomAccessFile f = new RandomAccessFile(fh,"rw");60for(int i = 0; i < 10000; i++){61f.writeShort((short)i);62}63f.writeShort((short)65535);64f.close();65f = new RandomAccessFile(fh,"r");66for(int i = 0; i < 10000; i++) {67short r = f.readShort();68if (r != ((short)i)) {69System.err.println("An error occurred. Read:" + r70+ " i:" + ((short)i));71throw new IOException("Bad read from a writeShort");72}73}74short rmax = f.readShort();75if (rmax != ((short)65535)) {76System.err.println("An error occurred. Read:" + rmax);77throw new IOException("Bad read from a writeShort");78}79f.close();80}8182private static void testChar() throws IOException {83File fh = new File(System.getProperty("test.dir", "."),84"x.ReadWriteGenerated");85RandomAccessFile f = new RandomAccessFile(fh,"rw");86for(int i = 0; i < 10000; i++){87f.writeChar((char)i);88}89f.close();90f = new RandomAccessFile(fh,"r");91for(int i = 0; i < 10000; i++) {92char r = f.readChar();93if (r != ((char)i)){94System.err.println("An error occurred. Read:" + r95+ " i:" + ((char) i));96throw new IOException("Bad read from a writeChar");97}98}99f.close();100}101102103private static void testInt() throws IOException {104File fh = new File(System.getProperty("test.dir", "."),105"x.ReadWriteGenerated");106RandomAccessFile f = new RandomAccessFile(fh,"rw");107for(int i = 0; i < 10000; i++){108f.writeInt((short)i);109}110f.writeInt(Integer.MAX_VALUE);111f.close();112f = new RandomAccessFile(fh, "r");113for(int i = 0; i < 10000; i++) {114int r = f.readInt();115if (r != i){116System.err.println("An error occurred. Read:" + r117+ " i:" + i);118throw new IOException("Bad read from a writeInt");119}120}121int rmax = f.readInt();122if (rmax != Integer.MAX_VALUE){123System.err.println("An error occurred. Read:" + rmax);124throw new IOException("Bad read from a writeInt");125}126f.close();127}128129private static void testLong() throws IOException {130File fh = new File(System.getProperty("test.dir", "."),131"x.ReadWriteGenerated");132RandomAccessFile f = new RandomAccessFile(fh,"rw");133for(int i = 0; i < 10000; i++){134f.writeLong(123456789L * (long)i);135}136f.close();137f = new RandomAccessFile(fh,"r");138for(int i = 0; i < 10000; i++){139long r = f.readLong();140if (r != (((long) i) * 123456789L) ) {141System.err.println("An error occurred. Read:" + r142+ " i" + ((long) i));143144throw new IOException("Bad read from a writeInt");145}146}147f.close();148149}150151}152153154