apache/dubbo · error · IllegalArgumentException

Base64 code length < 64.

Error message

Base64 code length < 64.

What it means

Thrown by Bytes.bytes2base64(byte[], int, int, String code) when the custom base64 alphabet string 'code' has fewer than 64 characters. Base64 requires exactly 64 encoding characters (indices 0-63); an optional 65th character is the pad. A code shorter than 64 cannot map all 6-bit values and is rejected before any encoding happens.

Source

Thrown at dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java:500

     *
     * @param b    byte array.
     * @param code base64 code string(0-63 is base64 char,64 is pad char).
     * @return base64 string.
     */
    public static String bytes2base64(byte[] b, String code) {
        return bytes2base64(b, 0, b.length, code);
    }

    /**
     * to base64 string.
     *
     * @param b    byte array.
     * @param code base64 code string(0-63 is base64 char,64 is pad char).
     * @return base64 string.
     */
    public static String bytes2base64(byte[] b, int offset, int length, String code) {
        if (code.length() < 64) {
            throw new IllegalArgumentException("Base64 code length < 64.");
        }

        return bytes2base64(b, offset, length, code.toCharArray());
    }

    /**
     * to base64 string.
     *
     * @param b    byte array.
     * @param code base64 code(0-63 is base64 char,64 is pad char).
     * @return base64 string.
     */
    public static String bytes2base64(byte[] b, char[] code) {
        return bytes2base64(b, 0, b.length, code);
    }

    /**
     * to base64 string.

View on GitHub (pinned to 3a3043227f)

Solutions

  1. Use the default alphabet by calling bytes2base64(byte[]) or bytes2base64(byte[], int, int) which use Bytes.BASE64.
  2. If a custom alphabet is required, ensure it has exactly 64 (optionally 65 with pad) characters - validate code.length() >= 64.
  3. Use a well-known constant for the custom alphabet rather than inline literals.

Example fix

// before
String urlSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; // 64 ok, but a typo can drop one
String b64 = Bytes.bytes2base64(data, 0, data.length, urlSafe); // throws [158] if < 64

// after: prefer the default or validate
String b64 = Bytes.bytes2base64(data); // uses Bytes.BASE64
Defensive patterns

Strategy: validation

Validate before calling

if (code == null || code.length() < 64) {
    throw new IllegalArgumentException("base64 alphabet must have >= 64 chars: " + (code == null ? 0 : code.length()));
}
String b64 = Bytes.bytes2base64(data, off, len, code);

Prevention

When it happens

Trigger: Calling the bytes2base64 overload that accepts a custom String alphabet with a code argument shorter than 64 chars. The String overload delegates to the char[] overload after this check. Passing a truncated or hand-written alphabet triggers it.

Common situations: Using a custom (e.g., URL-safe) base64 alphabet but providing an incomplete character set; copy-paste that dropped characters; mistaking the alphabet for a key/seed. The default Bytes.BASE64 constant is correct, so this only happens with custom alphabets.

Related errors


AI-assisted analysis of apache/dubbo@3a3043227f (2026-08-14). Data as JSON: /api/errors/bec00ac5e0b6acf8. Report an issue: GitHub.