Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ public final class OtelEncodingUtils {
private static final int NUM_ASCII_CHARACTERS = 128;
private static final char[] ENCODING = buildEncodingArray();
private static final byte[] DECODING = buildDecodingArray();
private static final boolean[] VALID_HEX = buildValidHexArray();
/**
* Stores whether the character at that index is a valid HEX character.
* We lazy init the array values to minimize impact during process startup esp. on mobile.
* A value of 0 means the character validity has not been determined yet.
* A value of 1 means the character is a valid HEX character.
* A value of -1 means the character is an invalid HEX character.
*
* @see #isValidBase16Character(char)
*/
private static final int[] VALID_HEX = new int[Character.MAX_VALUE];

private static char[] buildEncodingArray() {
char[] encoding = new char[512];
Expand All @@ -42,14 +51,6 @@ private static byte[] buildDecodingArray() {
return decoding;
}

private static boolean[] buildValidHexArray() {
boolean[] validHex = new boolean[Character.MAX_VALUE];
for (int i = 0; i < Character.MAX_VALUE; i++) {
validHex[i] = (48 <= i && i <= 57) || (97 <= i && i <= 102);
}
return validHex;
}

/**
* Returns the {@code long} value whose base16 representation is stored in the first 16 chars of
* {@code chars} starting from the {@code offset}.
Expand Down Expand Up @@ -152,7 +153,12 @@ public static boolean isValidBase16String(CharSequence value) {

/** Returns whether the given {@code char} is a valid hex character. */
public static boolean isValidBase16Character(char b) {
return VALID_HEX[b];
int isValid = VALID_HEX[b];
if (isValid == 0) {
isValid = (48 <= b && b <= 57) || (97 <= b && b <= 102) ? 1 : -1;
VALID_HEX[b] = isValid;
}
return isValid == 1;
}

private OtelEncodingUtils() {}
Expand Down
Loading