8333833: Remove the use of ByteArrayLittleEndian from UUID::toString

Reviewed-by: liach, redestad
This commit is contained in:
Shaojin Wen 2024-06-10 08:18:27 +00:00 committed by Claes Redestad
parent de55db2352
commit 8aa35cacfc
2 changed files with 31 additions and 51 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -31,7 +31,6 @@ import java.security.*;
import jdk.internal.access.JavaLangAccess; import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ByteArrayLittleEndian;
import jdk.internal.util.HexDigits; import jdk.internal.util.HexDigits;
/** /**
@ -467,38 +466,24 @@ public final class UUID implements java.io.Serializable, Comparable<UUID> {
*/ */
@Override @Override
public String toString() { public String toString() {
long lsb = leastSigBits; int i0 = (int) (mostSigBits >> 32);
long msb = mostSigBits; int i1 = (int) mostSigBits;
byte[] buf = new byte[36]; int i2 = (int) (leastSigBits >> 32);
ByteArrayLittleEndian.setLong( int i3 = (int) leastSigBits;
buf,
0,
HexDigits.packDigits((int) (msb >> 56), (int) (msb >> 48), (int) (msb >> 40), (int) (msb >> 32)));
buf[8] = '-';
ByteArrayLittleEndian.setInt(
buf,
9,
HexDigits.packDigits(((int) msb) >> 24, ((int) msb) >> 16));
buf[13] = '-';
ByteArrayLittleEndian.setInt(
buf,
14,
HexDigits.packDigits(((int) msb) >> 8, (int) msb));
buf[18] = '-';
ByteArrayLittleEndian.setInt(
buf,
19,
HexDigits.packDigits((int) (lsb >> 56), (int) (lsb >> 48)));
buf[23] = '-';
ByteArrayLittleEndian.setLong(
buf,
24,
HexDigits.packDigits((int) (lsb >> 40), (int) (lsb >> 32), ((int) lsb) >> 24, ((int) lsb) >> 16));
ByteArrayLittleEndian.setInt(
buf,
32,
HexDigits.packDigits(((int) lsb) >> 8, (int) lsb));
byte[] buf = new byte[36];
HexDigits.put4(buf, 0, i0 >> 16);
HexDigits.put4(buf, 4, i0);
buf[8] = '-';
HexDigits.put4(buf, 9, i1 >> 16);
buf[13] = '-';
HexDigits.put4(buf, 14, i1);
buf[18] = '-';
HexDigits.put4(buf, 19, i2 >> 16);
buf[23] = '-';
HexDigits.put4(buf, 24, i2);
HexDigits.put4(buf, 28, i3 >> 16);
HexDigits.put4(buf, 32, i3);
try { try {
return jla.newStringNoRepl(buf, StandardCharsets.ISO_8859_1); return jla.newStringNoRepl(buf, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) { } catch (CharacterCodingException cce) {

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -114,24 +114,19 @@ public final class HexDigits {
} }
/** /**
* Return a little-endian packed integer for the 4 ASCII bytes for an input unsigned 2-byte integer. * Insert the unsigned 2-byte integer into the buffer as 4 hexadecimal digit ASCII bytes,
* {@code b0} is the most significant byte and {@code b1} is the least significant byte. * only least significant 16 bits of {@code value} are used.
* The integer is passed byte-wise to allow reordering of execution. * @param buffer byte buffer to copy into
* @param index insert point
* @param value to convert
*/ */
public static int packDigits(int b0, int b1) { public static void put4(byte[] buffer, int index, int value) {
return DIGITS[b0 & 0xff] | (DIGITS[b1 & 0xff] << 16); // Prepare an int value so C2 generates a 4-byte write instead of two 2-byte writes
} int v = (DIGITS[value & 0xff] << 16) | DIGITS[(value >> 8) & 0xff];
buffer[index] = (byte) v;
/** buffer[index + 1] = (byte) (v >> 8);
* Return a little-endian packed long for the 8 ASCII bytes for an input unsigned 4-byte integer. buffer[index + 2] = (byte) (v >> 16);
* {@code b0} is the most significant byte and {@code b3} is the least significant byte. buffer[index + 3] = (byte) (v >> 24);
* The integer is passed byte-wise to allow reordering of execution.
*/
public static long packDigits(int b0, int b1, int b2, int b3) {
return DIGITS[b0 & 0xff]
| (DIGITS[b1 & 0xff] << 16)
| (((long) DIGITS[b2 & 0xff]) << 32)
| (((long) DIGITS[b3 & 0xff]) << 48);
} }
/** /**