diff --git a/src/java.base/share/classes/java/lang/Long.java b/src/java.base/share/classes/java/lang/Long.java index fb5e4cecca8..813837020fb 100644 --- a/src/java.base/share/classes/java/lang/Long.java +++ b/src/java.base/share/classes/java/lang/Long.java @@ -445,39 +445,6 @@ public final class Long extends Number } while (charPos > offset); } - static String fastUUID(long lsb, long msb) { - if (COMPACT_STRINGS) { - byte[] buf = new byte[36]; - formatUnsignedLong0(lsb, 4, buf, 24, 12); - formatUnsignedLong0(lsb >>> 48, 4, buf, 19, 4); - formatUnsignedLong0(msb, 4, buf, 14, 4); - formatUnsignedLong0(msb >>> 16, 4, buf, 9, 4); - formatUnsignedLong0(msb >>> 32, 4, buf, 0, 8); - - buf[23] = '-'; - buf[18] = '-'; - buf[13] = '-'; - buf[8] = '-'; - - return new String(buf, LATIN1); - } else { - byte[] buf = new byte[72]; - - formatUnsignedLong0UTF16(lsb, 4, buf, 24, 12); - formatUnsignedLong0UTF16(lsb >>> 48, 4, buf, 19, 4); - formatUnsignedLong0UTF16(msb, 4, buf, 14, 4); - formatUnsignedLong0UTF16(msb >>> 16, 4, buf, 9, 4); - formatUnsignedLong0UTF16(msb >>> 32, 4, buf, 0, 8); - - StringUTF16.putChar(buf, 23, '-'); - StringUTF16.putChar(buf, 18, '-'); - StringUTF16.putChar(buf, 13, '-'); - StringUTF16.putChar(buf, 8, '-'); - - return new String(buf, UTF16); - } - } - /** * Returns a {@code String} object representing the specified * {@code long}. The argument is converted to signed decimal diff --git a/src/java.base/share/classes/java/lang/System.java b/src/java.base/share/classes/java/lang/System.java index 9d9dbf886e6..31a830782c3 100644 --- a/src/java.base/share/classes/java/lang/System.java +++ b/src/java.base/share/classes/java/lang/System.java @@ -2395,9 +2395,6 @@ public final class System { public Package definePackage(ClassLoader cl, String name, Module module) { return cl.definePackage(name, module); } - public String fastUUID(long lsb, long msb) { - return Long.fastUUID(lsb, msb); - } @SuppressWarnings("removal") public void addNonExportedPackages(ModuleLayer layer) { SecurityManager.addNonExportedPackages(layer); diff --git a/src/java.base/share/classes/java/util/HexDigits.java b/src/java.base/share/classes/java/util/HexDigits.java index 3ac843aa4a8..7cae4b731f8 100644 --- a/src/java.base/share/classes/java/util/HexDigits.java +++ b/src/java.base/share/classes/java/util/HexDigits.java @@ -35,6 +35,44 @@ import jdk.internal.vm.annotation.Stable; * @since 21 */ final class HexDigits implements Digits { + /** + * Each element of the array represents the ascii encoded + * hex relative to its index, for example:
+ *
+ * 0 -> '00' -> ('0' << 8) | '0' -> 12336 + * 1 -> '01' -> ('0' << 8) | '1' -> 12337 + * 2 -> '02' -> ('0' << 8) | '2' -> 12338 + * + * ... + * + * 10 -> '0a' -> ('0' << 8) | 'a' -> 12385 + * 11 -> '0b' -> ('0' << 8) | 'b' -> 12386 + * 12 -> '0c' -> ('0' << 8) | 'b' -> 12387 + * + * ... + * + * 26 -> '1a' -> ('1' << 8) | 'a' -> 12641 + * 27 -> '1b' -> ('1' << 8) | 'b' -> 12642 + * 28 -> '1c' -> ('1' << 8) | 'c' -> 12643 + * + * ... + * + * 253 -> 'fd' -> ('f' << 8) | 'd' -> 26212 + * 254 -> 'fe' -> ('f' << 8) | 'e' -> 26213 + * 255 -> 'ff' -> ('f' << 8) | 'f' -> 26214 + *+ *
use like this: + *
+ * int v = 254; + * + * char[] chars = new char[2]; + * + * short i = DIGITS[v]; // 26213 + * + * chars[0] = (char) (byte) (i >> 8); // 'f' + * chars[1] = (char) (byte) i; // 'e' + *+ */ @Stable private static final short[] DIGITS; @@ -64,6 +102,23 @@ final class HexDigits implements Digits { private HexDigits() { } + /** + * Combine two hex shorts into one int based on big endian + */ + static int digit(int b0, int b1) { + return (DIGITS[b0 & 0xff] << 16) | DIGITS[b1 & 0xff]; + } + + /** + * Combine four hex shorts into one long based on big endian + */ + static long digit(int b0, int b1, int b2, int b3) { + return (((long) DIGITS[b0 & 0xff]) << 48) + | (((long) DIGITS[b1 & 0xff]) << 32) + | (DIGITS[b2 & 0xff] << 16) + | DIGITS[b3 & 0xff]; + } + @Override public int digits(long value, byte[] buffer, int index, MethodHandle putCharMH) throws Throwable { diff --git a/src/java.base/share/classes/java/util/UUID.java b/src/java.base/share/classes/java/util/UUID.java index 2f5ad22653d..78846ba48c2 100644 --- a/src/java.base/share/classes/java/util/UUID.java +++ b/src/java.base/share/classes/java/util/UUID.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 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 @@ -25,10 +25,13 @@ package java.util; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.StandardCharsets; import java.security.*; import jdk.internal.access.JavaLangAccess; import jdk.internal.access.SharedSecrets; +import jdk.internal.util.ByteArray; /** * A class that represents an immutable universally unique identifier (UUID). @@ -463,7 +466,43 @@ public final class UUID implements java.io.Serializable, Comparable