Skip to content

Commit 74e1a8b

Browse files
committed
8300236: Use VarHandle access in Data(Input | Output)Stream classes
Reviewed-by: rriggs, alanb
1 parent a5d8e12 commit 74e1a8b

File tree

11 files changed

+864
-359
lines changed

11 files changed

+864
-359
lines changed

src/java.base/share/classes/java/io/Bits.java

Lines changed: 0 additions & 120 deletions
This file was deleted.

src/java.base/share/classes/java/io/DataInputStream.java

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,8 @@
2525

2626
package java.io;
2727

28+
import jdk.internal.util.ByteArray;
29+
2830
import java.util.Objects;
2931

3032
/**
@@ -54,6 +56,8 @@ public DataInputStream(InputStream in) {
5456
super(in);
5557
}
5658

59+
private final byte[] readBuffer = new byte[8];
60+
5761
/**
5862
* working arrays initialized on demand by readUTF
5963
*/
@@ -309,7 +313,8 @@ public final int readUnsignedByte() throws IOException {
309313
* @see java.io.FilterInputStream#in
310314
*/
311315
public final short readShort() throws IOException {
312-
return (short) readUnsignedShort();
316+
readFully(readBuffer, 0, 2);
317+
return ByteArray.getShort(readBuffer, 0);
313318
}
314319

315320
/**
@@ -330,12 +335,8 @@ public final short readShort() throws IOException {
330335
* @see java.io.FilterInputStream#in
331336
*/
332337
public final int readUnsignedShort() throws IOException {
333-
InputStream in = this.in;
334-
int ch1 = in.read();
335-
int ch2 = in.read();
336-
if ((ch1 | ch2) < 0)
337-
throw new EOFException();
338-
return (ch1 << 8) + (ch2 << 0);
338+
readFully(readBuffer, 0, 2);
339+
return ByteArray.getUnsignedShort(readBuffer, 0);
339340
}
340341

341342
/**
@@ -356,7 +357,8 @@ public final int readUnsignedShort() throws IOException {
356357
* @see java.io.FilterInputStream#in
357358
*/
358359
public final char readChar() throws IOException {
359-
return (char) readUnsignedShort();
360+
readFully(readBuffer, 0, 2);
361+
return ByteArray.getChar(readBuffer, 0);
360362
}
361363

362364
/**
@@ -377,18 +379,10 @@ public final char readChar() throws IOException {
377379
* @see java.io.FilterInputStream#in
378380
*/
379381
public final int readInt() throws IOException {
380-
InputStream in = this.in;
381-
int ch1 = in.read();
382-
int ch2 = in.read();
383-
int ch3 = in.read();
384-
int ch4 = in.read();
385-
if ((ch1 | ch2 | ch3 | ch4) < 0)
386-
throw new EOFException();
387-
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
382+
readFully(readBuffer, 0, 4);
383+
return ByteArray.getInt(readBuffer, 0);
388384
}
389385

390-
private final byte[] readBuffer = new byte[8];
391-
392386
/**
393387
* See the general contract of the {@code readLong}
394388
* method of {@code DataInput}.
@@ -408,14 +402,7 @@ public final int readInt() throws IOException {
408402
*/
409403
public final long readLong() throws IOException {
410404
readFully(readBuffer, 0, 8);
411-
return (((long)readBuffer[0] << 56) +
412-
((long)(readBuffer[1] & 255) << 48) +
413-
((long)(readBuffer[2] & 255) << 40) +
414-
((long)(readBuffer[3] & 255) << 32) +
415-
((long)(readBuffer[4] & 255) << 24) +
416-
((readBuffer[5] & 255) << 16) +
417-
((readBuffer[6] & 255) << 8) +
418-
((readBuffer[7] & 255) << 0));
405+
return ByteArray.getLong(readBuffer, 0);
419406
}
420407

421408
/**
@@ -437,7 +424,8 @@ public final long readLong() throws IOException {
437424
* @see java.lang.Float#intBitsToFloat(int)
438425
*/
439426
public final float readFloat() throws IOException {
440-
return Float.intBitsToFloat(readInt());
427+
readFully(readBuffer, 0, 4);
428+
return ByteArray.getFloat(readBuffer, 0);
441429
}
442430

443431
/**
@@ -459,7 +447,8 @@ public final float readFloat() throws IOException {
459447
* @see java.lang.Double#longBitsToDouble(long)
460448
*/
461449
public final double readDouble() throws IOException {
462-
return Double.longBitsToDouble(readLong());
450+
readFully(readBuffer, 0, 8);
451+
return ByteArray.getDouble(readBuffer, 0);
463452
}
464453

465454
private char[] lineBuffer;

src/java.base/share/classes/java/io/DataOutputStream.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,8 @@
2525

2626
package java.io;
2727

28+
import jdk.internal.util.ByteArray;
29+
2830
/**
2931
* A data output stream lets an application write primitive Java data
3032
* types to an output stream in a portable way. An application can
@@ -170,8 +172,7 @@ public final void writeByte(int v) throws IOException {
170172
* @see java.io.FilterOutputStream#out
171173
*/
172174
public final void writeShort(int v) throws IOException {
173-
writeBuffer[0] = (byte)(v >>> 8);
174-
writeBuffer[1] = (byte)(v >>> 0);
175+
ByteArray.setUnsignedShort(writeBuffer, 0, v);
175176
out.write(writeBuffer, 0, 2);
176177
incCount(2);
177178
}
@@ -186,8 +187,7 @@ public final void writeShort(int v) throws IOException {
186187
* @see java.io.FilterOutputStream#out
187188
*/
188189
public final void writeChar(int v) throws IOException {
189-
writeBuffer[0] = (byte)(v >>> 8);
190-
writeBuffer[1] = (byte)(v >>> 0);
190+
ByteArray.setUnsignedShort(writeBuffer, 0, v);
191191
out.write(writeBuffer, 0, 2);
192192
incCount(2);
193193
}
@@ -202,10 +202,7 @@ public final void writeChar(int v) throws IOException {
202202
* @see java.io.FilterOutputStream#out
203203
*/
204204
public final void writeInt(int v) throws IOException {
205-
writeBuffer[0] = (byte)(v >>> 24);
206-
writeBuffer[1] = (byte)(v >>> 16);
207-
writeBuffer[2] = (byte)(v >>> 8);
208-
writeBuffer[3] = (byte)(v >>> 0);
205+
ByteArray.setInt(writeBuffer, 0, v);
209206
out.write(writeBuffer, 0, 4);
210207
incCount(4);
211208
}
@@ -220,14 +217,7 @@ public final void writeInt(int v) throws IOException {
220217
* @see java.io.FilterOutputStream#out
221218
*/
222219
public final void writeLong(long v) throws IOException {
223-
writeBuffer[0] = (byte)(v >>> 56);
224-
writeBuffer[1] = (byte)(v >>> 48);
225-
writeBuffer[2] = (byte)(v >>> 40);
226-
writeBuffer[3] = (byte)(v >>> 32);
227-
writeBuffer[4] = (byte)(v >>> 24);
228-
writeBuffer[5] = (byte)(v >>> 16);
229-
writeBuffer[6] = (byte)(v >>> 8);
230-
writeBuffer[7] = (byte)(v >>> 0);
220+
ByteArray.setLong(writeBuffer, 0, v);
231221
out.write(writeBuffer, 0, 8);
232222
incCount(8);
233223
}
@@ -246,7 +236,9 @@ public final void writeLong(long v) throws IOException {
246236
* @see java.lang.Float#floatToIntBits(float)
247237
*/
248238
public final void writeFloat(float v) throws IOException {
249-
writeInt(Float.floatToIntBits(v));
239+
ByteArray.setFloat(writeBuffer, 0, v);
240+
out.write(writeBuffer, 0, 4);
241+
incCount(4);
250242
}
251243

252244
/**
@@ -263,7 +255,9 @@ public final void writeFloat(float v) throws IOException {
263255
* @see java.lang.Double#doubleToLongBits(double)
264256
*/
265257
public final void writeDouble(double v) throws IOException {
266-
writeLong(Double.doubleToLongBits(v));
258+
ByteArray.setDouble(writeBuffer, 0, v);
259+
out.write(writeBuffer, 0, 8);
260+
incCount(8);
267261
}
268262

269263
/**
@@ -301,8 +295,7 @@ public final void writeChars(String s) throws IOException {
301295
int len = s.length();
302296
for (int i = 0 ; i < len ; i++) {
303297
int v = s.charAt(i);
304-
writeBuffer[0] = (byte)(v >>> 8);
305-
writeBuffer[1] = (byte)(v >>> 0);
298+
ByteArray.setUnsignedShort(writeBuffer, 0, v);
306299
out.write(writeBuffer, 0, 2);
307300
}
308301
incCount(len * 2);
@@ -379,9 +372,8 @@ static int writeUTF(String str, DataOutput out) throws IOException {
379372
}
380373

381374
int count = 0;
382-
bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
383-
bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
384-
375+
ByteArray.setUnsignedShort(bytearr, count, utflen);
376+
count += 2;
385377
int i = 0;
386378
for (i = 0; i < strlen; i++) { // optimized for initial run of ASCII
387379
int c = str.charAt(i);

0 commit comments

Comments
 (0)