Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 14 additions & 29 deletions src/java.base/share/classes/java/util/UUID.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -31,7 +31,6 @@

import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ByteArrayLittleEndian;
import jdk.internal.util.HexDigits;

/**
Expand Down Expand Up @@ -467,38 +466,24 @@ public long node() {
*/
@Override
public String toString() {
long lsb = leastSigBits;
long msb = mostSigBits;
int i0 = (int) (mostSigBits >> 32);
int i1 = (int) mostSigBits;
int i2 = (int) (leastSigBits >> 32);
int i3 = (int) leastSigBits;

byte[] buf = new byte[36];
ByteArrayLittleEndian.setLong(
buf,
0,
HexDigits.packDigits((int) (msb >> 56), (int) (msb >> 48), (int) (msb >> 40), (int) (msb >> 32)));
HexDigits.put4(buf, 0, i0 >> 16);
HexDigits.put4(buf, 4, i0);
buf[8] = '-';
ByteArrayLittleEndian.setInt(
buf,
9,
HexDigits.packDigits(((int) msb) >> 24, ((int) msb) >> 16));
HexDigits.put4(buf, 9, i1 >> 16);
buf[13] = '-';
ByteArrayLittleEndian.setInt(
buf,
14,
HexDigits.packDigits(((int) msb) >> 8, (int) msb));
HexDigits.put4(buf, 14, i1);
buf[18] = '-';
ByteArrayLittleEndian.setInt(
buf,
19,
HexDigits.packDigits((int) (lsb >> 56), (int) (lsb >> 48)));
HexDigits.put4(buf, 19, i2 >> 16);
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));

HexDigits.put4(buf, 24, i2);
HexDigits.put4(buf, 28, i3 >> 16);
HexDigits.put4(buf, 32, i3);
try {
return jla.newStringNoRepl(buf, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
Expand Down
31 changes: 13 additions & 18 deletions src/java.base/share/classes/jdk/internal/util/HexDigits.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -114,24 +114,19 @@ public static short digitPair(int i, boolean ucase) {
}

/**
* Return a little-endian packed integer for the 4 ASCII bytes for an input unsigned 2-byte integer.
* {@code b0} is the most significant byte and {@code b1} is the least significant byte.
* The integer is passed byte-wise to allow reordering of execution.
* Insert the unsigned 2-byte integer into the buffer as 4 hexadecimal digit ASCII bytes,
* only least significant 16 bits of {@code value} are used.
* @param buffer byte buffer to copy into
* @param index insert point
* @param value to convert
*/
public static int packDigits(int b0, int b1) {
return DIGITS[b0 & 0xff] | (DIGITS[b1 & 0xff] << 16);
}

/**
* Return a little-endian packed long for the 8 ASCII bytes for an input unsigned 4-byte integer.
* {@code b0} is the most significant byte and {@code b3} is the least significant byte.
* 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);
public static void put4(byte[] buffer, int index, int value) {
// 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);
buffer[index + 2] = (byte) (v >> 16);
buffer[index + 3] = (byte) (v >> 24);
}

/**
Expand Down