Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8300236: Use VarHandle access in Data(Input | Output)Stream classes #12076

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
120 changes: 0 additions & 120 deletions src/java.base/share/classes/java/io/Bits.java

This file was deleted.

47 changes: 18 additions & 29 deletions src/java.base/share/classes/java/io/DataInputStream.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2023, 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
Expand All @@ -25,6 +25,8 @@

package java.io;

import jdk.internal.util.ByteArray;

import java.util.Objects;

/**
Expand Down Expand Up @@ -54,6 +56,8 @@ public DataInputStream(InputStream in) {
super(in);
}

private final byte[] readBuffer = new byte[8];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No objection to the readBuffer but it does make it wonder if we should re-visit bytearr and chararr as they are only need for reading modified UTF-8 strings and shouldn't need to be eagerly created.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/**
* working arrays initialized on demand by readUTF
*/
Expand Down Expand Up @@ -309,7 +313,8 @@ public final int readUnsignedByte() throws IOException {
* @see java.io.FilterInputStream#in
*/
public final short readShort() throws IOException {
return (short) readUnsignedShort();
readFully(readBuffer, 0, 2);
return ByteArray.getShort(readBuffer, 0);
}

/**
Expand All @@ -330,12 +335,8 @@ public final short readShort() throws IOException {
* @see java.io.FilterInputStream#in
*/
public final int readUnsignedShort() throws IOException {
InputStream in = this.in;
int ch1 = in.read();
int ch2 = in.read();
if ((ch1 | ch2) < 0)
throw new EOFException();
return (ch1 << 8) + (ch2 << 0);
readFully(readBuffer, 0, 2);
return ByteArray.getUnsignedShort(readBuffer, 0);
}

/**
Expand All @@ -356,7 +357,8 @@ public final int readUnsignedShort() throws IOException {
* @see java.io.FilterInputStream#in
*/
public final char readChar() throws IOException {
return (char) readUnsignedShort();
readFully(readBuffer, 0, 2);
return ByteArray.getChar(readBuffer, 0);
}

/**
Expand All @@ -377,18 +379,10 @@ public final char readChar() throws IOException {
* @see java.io.FilterInputStream#in
*/
public final int readInt() throws IOException {
InputStream in = this.in;
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
readFully(readBuffer, 0, 4);
return ByteArray.getInt(readBuffer, 0);
}

private final byte[] readBuffer = new byte[8];

/**
* See the general contract of the {@code readLong}
* method of {@code DataInput}.
Expand All @@ -408,14 +402,7 @@ public final int readInt() throws IOException {
*/
public final long readLong() throws IOException {
readFully(readBuffer, 0, 8);
return (((long)readBuffer[0] << 56) +
((long)(readBuffer[1] & 255) << 48) +
((long)(readBuffer[2] & 255) << 40) +
((long)(readBuffer[3] & 255) << 32) +
((long)(readBuffer[4] & 255) << 24) +
((readBuffer[5] & 255) << 16) +
((readBuffer[6] & 255) << 8) +
((readBuffer[7] & 255) << 0));
return ByteArray.getLong(readBuffer, 0);
}

/**
Expand All @@ -437,7 +424,8 @@ public final long readLong() throws IOException {
* @see java.lang.Float#intBitsToFloat(int)
*/
public final float readFloat() throws IOException {
return Float.intBitsToFloat(readInt());
readFully(readBuffer, 0, 4);
return ByteArray.getFloat(readBuffer, 0);
}

/**
Expand All @@ -459,7 +447,8 @@ public final float readFloat() throws IOException {
* @see java.lang.Double#longBitsToDouble(long)
*/
public final double readDouble() throws IOException {
return Double.longBitsToDouble(readLong());
readFully(readBuffer, 0, 8);
return ByteArray.getDouble(readBuffer, 0);
}

private char[] lineBuffer;
Expand Down
40 changes: 16 additions & 24 deletions src/java.base/share/classes/java/io/DataOutputStream.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2023, 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
Expand All @@ -25,6 +25,8 @@

package java.io;

import jdk.internal.util.ByteArray;

/**
* A data output stream lets an application write primitive Java data
* types to an output stream in a portable way. An application can
Expand Down Expand Up @@ -170,8 +172,7 @@ public final void writeByte(int v) throws IOException {
* @see java.io.FilterOutputStream#out
*/
public final void writeShort(int v) throws IOException {
writeBuffer[0] = (byte)(v >>> 8);
writeBuffer[1] = (byte)(v >>> 0);
ByteArray.setUnsignedShort(writeBuffer, 0, v);
out.write(writeBuffer, 0, 2);
incCount(2);
}
Expand All @@ -186,8 +187,7 @@ public final void writeShort(int v) throws IOException {
* @see java.io.FilterOutputStream#out
*/
public final void writeChar(int v) throws IOException {
writeBuffer[0] = (byte)(v >>> 8);
writeBuffer[1] = (byte)(v >>> 0);
ByteArray.setUnsignedShort(writeBuffer, 0, v);
out.write(writeBuffer, 0, 2);
incCount(2);
}
Expand All @@ -202,10 +202,7 @@ public final void writeChar(int v) throws IOException {
* @see java.io.FilterOutputStream#out
*/
public final void writeInt(int v) throws IOException {
writeBuffer[0] = (byte)(v >>> 24);
writeBuffer[1] = (byte)(v >>> 16);
writeBuffer[2] = (byte)(v >>> 8);
writeBuffer[3] = (byte)(v >>> 0);
ByteArray.setInt(writeBuffer, 0, v);
out.write(writeBuffer, 0, 4);
incCount(4);
}
Expand All @@ -220,14 +217,7 @@ public final void writeInt(int v) throws IOException {
* @see java.io.FilterOutputStream#out
*/
public final void writeLong(long v) throws IOException {
writeBuffer[0] = (byte)(v >>> 56);
writeBuffer[1] = (byte)(v >>> 48);
writeBuffer[2] = (byte)(v >>> 40);
writeBuffer[3] = (byte)(v >>> 32);
writeBuffer[4] = (byte)(v >>> 24);
writeBuffer[5] = (byte)(v >>> 16);
writeBuffer[6] = (byte)(v >>> 8);
writeBuffer[7] = (byte)(v >>> 0);
ByteArray.setLong(writeBuffer, 0, v);
out.write(writeBuffer, 0, 8);
incCount(8);
}
Expand All @@ -246,7 +236,9 @@ public final void writeLong(long v) throws IOException {
* @see java.lang.Float#floatToIntBits(float)
*/
public final void writeFloat(float v) throws IOException {
writeInt(Float.floatToIntBits(v));
ByteArray.setFloat(writeBuffer, 0, v);
out.write(writeBuffer, 0, 4);
incCount(4);
}

/**
Expand All @@ -263,7 +255,9 @@ public final void writeFloat(float v) throws IOException {
* @see java.lang.Double#doubleToLongBits(double)
*/
public final void writeDouble(double v) throws IOException {
writeLong(Double.doubleToLongBits(v));
ByteArray.setDouble(writeBuffer, 0, v);
out.write(writeBuffer, 0, 8);
incCount(8);
}

/**
Expand Down Expand Up @@ -301,8 +295,7 @@ public final void writeChars(String s) throws IOException {
int len = s.length();
for (int i = 0 ; i < len ; i++) {
int v = s.charAt(i);
writeBuffer[0] = (byte)(v >>> 8);
writeBuffer[1] = (byte)(v >>> 0);
ByteArray.setUnsignedShort(writeBuffer, 0, v);
out.write(writeBuffer, 0, 2);
}
incCount(len * 2);
Expand Down Expand Up @@ -379,9 +372,8 @@ static int writeUTF(String str, DataOutput out) throws IOException {
}

int count = 0;
bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);

ByteArray.setUnsignedShort(bytearr, count, utflen);
count += 2;
int i = 0;
for (i = 0; i < strlen; i++) { // optimized for initial run of ASCII
int c = str.charAt(i);
Expand Down