@Test public void shouldEncodeBase45Correctly() { assertEquals("#", encodeBase45(1L, false, false)); assertEquals("$", encodeBase45(2L, false, false)); assertEquals("R", encodeBase45(1L, true, false)); assertEquals("S", encodeBase45(2L, true, false)); assertEquals("P", encodeBase45(44L, false, false)); assertEquals("~", encodeBase45(44L, true, false)); assertEquals("#!", encodeBase45(45L, false, false)); assertEquals("RQ", encodeBase45(45L, true, false)); assertEquals("!R", encodeBase45(1L, false, true)); assertEquals("!S", encodeBase45(2L, false, true)); assertEquals("Q#", encodeBase45(1L, true, true)); assertEquals("Q$", encodeBase45(2L, true, true)); assertEquals("!~", encodeBase45(44L, false, true)); assertEquals("QP", encodeBase45(44L, true, true)); assertEquals("#Q", encodeBase45(45L, false, true)); assertEquals("R!", encodeBase45(45L, true, true)); assertEquals("RQ!", encodeBase45(2025L, true, true)); assertEquals("RQQ", encodeBase45(2025L, true, false)); assertEquals("#!Q", encodeBase45(2025L, false, true)); assertEquals("#!!", encodeBase45(2025L, false, false)); assertEquals("~P", encodeBase45(2024L, true, true)); assertEquals("~~", encodeBase45(2024L, true, false)); assertEquals("P~", encodeBase45(2024L, false, true)); assertEquals("PP", encodeBase45(2024L, false, false)); }
public static String encodeBase45(long value, boolean highRange, boolean flipLastByte) { char[] out = new char[MAX_NUMBER_LENGTH]; for (int i = out.length - 1;; i--) { boolean isFlip = flipLastByte && (i == (out.length - 1)); int index = (int) Math.abs(value % BASE45); if (isFlip != highRange) { index += BASE45; } value = value / BASE45; out[i] = BASECHARS[index]; int currentSize = out.length - i; boolean isLast = (value == 0) || (i == 0); boolean isDone = isLast && ((currentSize != 1) || !flipLastByte); if (isDone) { return new String(out, i, currentSize); } } }
CookieSafeBase90 { public static String encodeBase45(long value, boolean highRange, boolean flipLastByte) { char[] out = new char[MAX_NUMBER_LENGTH]; for (int i = out.length - 1;; i--) { boolean isFlip = flipLastByte && (i == (out.length - 1)); int index = (int) Math.abs(value % BASE45); if (isFlip != highRange) { index += BASE45; } value = value / BASE45; out[i] = BASECHARS[index]; int currentSize = out.length - i; boolean isLast = (value == 0) || (i == 0); boolean isDone = isLast && ((currentSize != 1) || !flipLastByte); if (isDone) { return new String(out, i, currentSize); } } } }
CookieSafeBase90 { public static String encodeBase45(long value, boolean highRange, boolean flipLastByte) { char[] out = new char[MAX_NUMBER_LENGTH]; for (int i = out.length - 1;; i--) { boolean isFlip = flipLastByte && (i == (out.length - 1)); int index = (int) Math.abs(value % BASE45); if (isFlip != highRange) { index += BASE45; } value = value / BASE45; out[i] = BASECHARS[index]; int currentSize = out.length - i; boolean isLast = (value == 0) || (i == 0); boolean isDone = isLast && ((currentSize != 1) || !flipLastByte); if (isDone) { return new String(out, i, currentSize); } } }  }
CookieSafeBase90 { public static String encodeBase45(long value, boolean highRange, boolean flipLastByte) { char[] out = new char[MAX_NUMBER_LENGTH]; for (int i = out.length - 1;; i--) { boolean isFlip = flipLastByte && (i == (out.length - 1)); int index = (int) Math.abs(value % BASE45); if (isFlip != highRange) { index += BASE45; } value = value / BASE45; out[i] = BASECHARS[index]; int currentSize = out.length - i; boolean isLast = (value == 0) || (i == 0); boolean isDone = isLast && ((currentSize != 1) || !flipLastByte); if (isDone) { return new String(out, i, currentSize); } } }  static String encodeBase45(long value, boolean highRange, boolean flipLastByte); static String encodeBase90Unsigned(long value); static String encodeBase90Signed(long value); static String encodeBytesBase90(byte[] bytes); static byte[] decodeBytesBase90(CharSequence charSequence); static long decodeBase90(CharSequence charSequence, boolean signed); static long decodeBase45(CharSequence charSequence); }
CookieSafeBase90 { public static String encodeBase45(long value, boolean highRange, boolean flipLastByte) { char[] out = new char[MAX_NUMBER_LENGTH]; for (int i = out.length - 1;; i--) { boolean isFlip = flipLastByte && (i == (out.length - 1)); int index = (int) Math.abs(value % BASE45); if (isFlip != highRange) { index += BASE45; } value = value / BASE45; out[i] = BASECHARS[index]; int currentSize = out.length - i; boolean isLast = (value == 0) || (i == 0); boolean isDone = isLast && ((currentSize != 1) || !flipLastByte); if (isDone) { return new String(out, i, currentSize); } } }  static String encodeBase45(long value, boolean highRange, boolean flipLastByte); static String encodeBase90Unsigned(long value); static String encodeBase90Signed(long value); static String encodeBytesBase90(byte[] bytes); static byte[] decodeBytesBase90(CharSequence charSequence); static long decodeBase90(CharSequence charSequence, boolean signed); static long decodeBase45(CharSequence charSequence); static final int MIDDLE_CHAR; }