66package io .opentelemetry .api .internal ;
77
88import java .util .Arrays ;
9- import javax .annotation .Nullable ;
109import javax .annotation .concurrent .Immutable ;
1110
1211/**
@@ -22,8 +21,16 @@ public final class OtelEncodingUtils {
2221 private static final int NUM_ASCII_CHARACTERS = 128 ;
2322 private static final char [] ENCODING = buildEncodingArray ();
2423 private static final byte [] DECODING = buildDecodingArray ();
25-
26- @ Nullable private static volatile boolean [] validHex = null ;
24+ /**
25+ * Stores whether the character at that index is a valid HEX character.
26+ * We lazy init the array values to minimize impact during process startup esp. on mobile.
27+ * A value of 0 means the character validity has not been determined yet.
28+ * A value of 1 means the character is a valid HEX character.
29+ * A value of -1 means the character is an invalid HEX character.
30+ *
31+ * @see #isValidBase16Character(char)
32+ */
33+ private static final int [] VALID_HEX = new int [Character .MAX_VALUE ];
2734
2835 private static char [] buildEncodingArray () {
2936 char [] encoding = new char [512 ];
@@ -44,14 +51,6 @@ private static byte[] buildDecodingArray() {
4451 return decoding ;
4552 }
4653
47- private static boolean [] buildValidHexArray () {
48- boolean [] validHex = new boolean [Character .MAX_VALUE ];
49- for (int i = 0 ; i < Character .MAX_VALUE ; i ++) {
50- validHex [i ] = (48 <= i && i <= 57 ) || (97 <= i && i <= 102 );
51- }
52- return validHex ;
53- }
54-
5554 /**
5655 * Returns the {@code long} value whose base16 representation is stored in the first 16 chars of
5756 * {@code chars} starting from the {@code offset}.
@@ -154,15 +153,12 @@ public static boolean isValidBase16String(CharSequence value) {
154153
155154 /** Returns whether the given {@code char} is a valid hex character. */
156155 public static boolean isValidBase16Character (char b ) {
157- if (validHex == null ) {
158- synchronized (OtelEncodingUtils .class ) {
159- if (validHex == null ) {
160- validHex = buildValidHexArray ();
161- }
162- }
156+ int isValid = VALID_HEX [b ];
157+ if (isValid == 0 ) {
158+ isValid = (48 <= b && b <= 57 ) || (97 <= b && b <= 102 ) ? 1 : -1 ;
159+ VALID_HEX [b ] = isValid ;
163160 }
164-
165- return validHex [b ];
161+ return isValid == 1 ;
166162 }
167163
168164 private OtelEncodingUtils () {}
0 commit comments