Skip to content

Commit

Permalink
eagerly load standard Base64 table
Browse files Browse the repository at this point in the history
  • Loading branch information
esaulpaugh committed Aug 13, 2024
1 parent 3b0beb8 commit 63c3003
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions src/main/java/com/esaulpaugh/headlong/util/FastBase64.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,7 @@ private FastBase64() {}

private static final short[] URL_SAFE = table("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");

private static volatile short[] standardTable = null;

private static short[] getStandardTable() {
final short[] temp = standardTable;
if (temp == null) {
synchronized (FastBase64.class) {
if (standardTable == null) {
return (standardTable = table("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"));
}
}
}
return temp;
}
private static final short[] STANDARD = table("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");

static short[] table(String alphabet) {
final byte[] bytes = Strings.decode(alphabet, Strings.ASCII);
Expand Down Expand Up @@ -89,7 +77,7 @@ public static void encodeToBytes(byte[] buffer, int offset, int len, byte[] dest
}
final int endEvenBytes = offset + evenBytes; // End of even 24-bits chunks
final int endEvenChars = destOff + (chunks * 4);
final short[] table = (flags & URL_SAFE_CHARS) != 0 ? URL_SAFE : getStandardTable();
final short[] table = (flags & URL_SAFE_CHARS) != 0 ? URL_SAFE : STANDARD;
if ((flags & NO_LINE_SEP) != 0) {
encodeMain(buffer, offset, table, endEvenBytes, dest, destOff);
insertRemainder(buffer, endEvenBytes, remainder, endEvenChars, charsLeft, table, dest);
Expand Down

0 comments on commit 63c3003

Please sign in to comment.