mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8298639: Perform I/O operations in bulk for RandomAccessFile
Co-authored-by: Sergey Tsypanov <stsypanov@openjdk.org> Reviewed-by: alanb, bpb
This commit is contained in:
parent
bfa921ae6c
commit
7938f8c32a
3 changed files with 159 additions and 57 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue