diff --git a/src/java.base/share/classes/java/io/RandomAccessFile.java b/src/java.base/share/classes/java/io/RandomAccessFile.java index ffe6e6f3c14..7cbc06fd3b4 100644 --- a/src/java.base/share/classes/java/io/RandomAccessFile.java +++ b/src/java.base/share/classes/java/io/RandomAccessFile.java @@ -26,7 +26,6 @@ package java.io; import java.nio.channels.FileChannel; -import java.util.Objects; import jdk.internal.access.JavaIORandomAccessFileAccess; import jdk.internal.access.SharedSecrets; @@ -80,6 +79,13 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { private final Object closeLock = new Object(); + /** + * A local buffer that allows reading and writing of + * longer primitive parameters (e.g. long) to be performed + * using bulk operations rather than on a per-byte basis. + */ + private final byte[] buffer = new byte[Long.BYTES]; + private volatile FileChannel channel; private volatile boolean closed; @@ -737,10 +743,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final boolean readBoolean() throws IOException { - int ch = this.read(); - if (ch < 0) - throw new EOFException(); - return (ch != 0); + return readUnsignedByte() != 0; } /** @@ -762,10 +765,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final byte readByte() throws IOException { - int ch = this.read(); - if (ch < 0) - throw new EOFException(); - return (byte)(ch); + return (byte) readUnsignedByte(); } /** @@ -809,11 +809,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final short readShort() throws IOException { - int ch1 = this.read(); - int ch2 = this.read(); - if ((ch1 | ch2) < 0) - throw new EOFException(); - return (short)((ch1 << 8) + (ch2 << 0)); + return (short) readUnsignedShort(); } /** @@ -837,11 +833,9 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final int readUnsignedShort() throws IOException { - int ch1 = this.read(); - int ch2 = this.read(); - if ((ch1 | ch2) < 0) - throw new EOFException(); - return (ch1 << 8) + (ch2 << 0); + readFully(buffer, 0, Short.BYTES); + return ((buffer[1] & 0xff) ) + + ((buffer[0] & 0xff) << 8); } /** @@ -865,11 +859,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final char readChar() throws IOException { - int ch1 = this.read(); - int ch2 = this.read(); - if ((ch1 | ch2) < 0) - throw new EOFException(); - return (char)((ch1 << 8) + (ch2 << 0)); + return (char) readUnsignedShort(); } /** @@ -893,13 +883,8 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final int readInt() throws IOException { - int ch1 = this.read(); - int ch2 = this.read(); - int ch3 = this.read(); - int ch4 = this.read(); - if ((ch1 | ch2 | ch3 | ch4) < 0) - throw new EOFException(); - return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); + readFully(buffer, 0, Integer.BYTES); + return Bits.getInt(buffer, 0); } /** @@ -931,7 +916,8 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final long readLong() throws IOException { - return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL); + readFully(buffer, 0, Long.BYTES); + return Bits.getLong(buffer, 0); } /** @@ -954,7 +940,8 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @see java.lang.Float#intBitsToFloat(int) */ public final float readFloat() throws IOException { - return Float.intBitsToFloat(readInt()); + readFully(buffer, 0, Float.BYTES); + return Bits.getFloat(buffer, 0); } /** @@ -977,7 +964,8 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @see java.lang.Double#longBitsToDouble(long) */ public final double readDouble() throws IOException { - return Double.longBitsToDouble(readLong()); + readFully(buffer, 0, Double.BYTES); + return Bits.getDouble(buffer, 0); } /** @@ -1070,7 +1058,6 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { */ public final void writeBoolean(boolean v) throws IOException { write(v ? 1 : 0); - //written++; } /** @@ -1082,7 +1069,6 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { */ public final void writeByte(int v) throws IOException { write(v); - //written++; } /** @@ -1093,9 +1079,9 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final void writeShort(int v) throws IOException { - write((v >>> 8) & 0xFF); - write((v >>> 0) & 0xFF); - //written += 2; + buffer[1] = (byte)(v ); + buffer[0] = (byte)(v >>> 8); + write(buffer, 0, Short.BYTES); } /** @@ -1107,9 +1093,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final void writeChar(int v) throws IOException { - write((v >>> 8) & 0xFF); - write((v >>> 0) & 0xFF); - //written += 2; + writeShort(v); } /** @@ -1120,10 +1104,8 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final void writeInt(int v) throws IOException { - write((v >>> 24) & 0xFF); - write((v >>> 16) & 0xFF); - write((v >>> 8) & 0xFF); - write((v >>> 0) & 0xFF); + Bits.putInt(buffer, 0, v); + write(buffer, 0, Integer.BYTES); //written += 4; } @@ -1135,15 +1117,8 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable { * @throws IOException if an I/O error occurs. */ public final void writeLong(long v) throws IOException { - write((int)(v >>> 56) & 0xFF); - write((int)(v >>> 48) & 0xFF); - write((int)(v >>> 40) & 0xFF); - write((int)(v >>> 32) & 0xFF); - write((int)(v >>> 24) & 0xFF); - write((int)(v >>> 16) & 0xFF); - write((int)(v >>> 8) & 0xFF); - write((int)(v >>> 0) & 0xFF); - //written += 8; + Bits.putLong(buffer, 0, v); + write(buffer, 0, Long.BYTES); } /** diff --git a/test/jdk/java/io/File/Basic.java b/test/jdk/java/io/File/Basic.java index e54606c6b6d..603249c08f8 100644 --- a/test/jdk/java/io/File/Basic.java +++ b/test/jdk/java/io/File/Basic.java @@ -29,10 +29,8 @@ */ import java.io.FileOutputStream; -import java.io.IOException; import java.io.File; import java.io.PrintStream; -import java.io.RandomAccessFile; public class Basic { diff --git a/test/micro/org/openjdk/bench/java/io/RandomAccessFileBenchmark.java b/test/micro/org/openjdk/bench/java/io/RandomAccessFileBenchmark.java new file mode 100644 index 00000000000..68738a42b59 --- /dev/null +++ b/test/micro/org/openjdk/bench/java/io/RandomAccessFileBenchmark.java @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package micro.org.openjdk.bench.java.io; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; + +import java.io.*; +import java.nio.file.Files; +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; + +@Fork(2) +@State(Scope.Thread) +@Warmup(iterations = 5, time = 1) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Measurement(iterations = 5, time = 2) + +public class RandomAccessFileBenchmark { + + @Param({"1", "5"}) + private int kiloBytes; + private int size; + private File file; + + private RandomAccessFile raf; + + private short[] shorts; + private int[] ints; + private long[] longs; + + @Setup(Level.Iteration) + public void beforeRun() throws IOException { + Random rnd = ThreadLocalRandom.current(); + size = kiloBytes << 10; + var bytes = new byte[size]; + rnd.nextBytes(bytes); + file = File.createTempFile(getClass().getName(), ".txt"); + Files.write(file.toPath(), bytes); + shorts = new short[size]; + for (int i = 0; i < size / Short.BYTES; i++) { + shorts[i] = (short) rnd.nextInt(); + } + ints = rnd.ints(size / Integer.BYTES).toArray(); + longs = rnd.longs(size / Long.BYTES).toArray(); + raf = new RandomAccessFile(file, "rw"); + // Make it more likely file content is directly available + for (int i = 0; i < size / Integer.BYTES; i++) { + raf.readInt(); + } + } + + @TearDown(Level.Iteration) + public void afterRun() throws IOException { + raf.close(); + file.delete(); + } + + @Benchmark + public void readShort(Blackhole bh) throws IOException { + raf.seek(0); + for (int i = 0; i < size / Short.BYTES; i++) { + bh.consume(raf.readShort()); + } + } + + @Benchmark + public void readInt(Blackhole bh) throws IOException { + raf.seek(0); + for (int i = 0; i < size / Integer.BYTES; i++) { + bh.consume(raf.readInt()); + } + } + + @Benchmark + public void readLong(Blackhole bh) throws IOException { + raf.seek(0); + for (int i = 0; i < size / Long.BYTES; i++) { + bh.consume(raf.readLong()); + } + } + + @Benchmark + public void writeShort() throws IOException { + raf.seek(0); + for (int i = 0; i < size / Short.BYTES; i++) { + raf.writeShort(shorts[i]); + } + } + + @Benchmark + public void writeInt() throws IOException { + raf.seek(0); + for (int i = 0; i < size / Integer.BYTES; i++) { + raf.writeInt(ints[i]); + } + } + + @Benchmark + public void writeLong() throws IOException { + raf.seek(0); + for (int i = 0; i < size / Long.BYTES; i++) { + raf.writeLong(longs[i]); + } + } + +}