Class Base62Util

java.lang.Object
com.sure.tool.codec.Base62Util

public final class Base62Util extends Object
Base62 编解码工具,字母表为 0-9A-Za-z。

Base62 常用于短链接、订单号、分享码等需要紧凑且 URL 安全的场景。 编码保留原始字节的前导零字节(输出为等数量的 0),解码时原样恢复。

示例:

String encoded = Base62Util.encode("hello".getBytes(StandardCharsets.UTF_8)); // "7tQLFHz"
String plain = Base62Util.decodeToString("7tQLFHz");                          // "hello"
boolean valid = Base62Util.isBase62("7tQLFHz");                               // true

null 语义:入参为 null 时返回 null(空安全); 解码遇到非 Base62 字母表字符抛 IllegalArgumentException。

Since:
1.19.1
Author:
suretool
  • Method Details

    • encode

      public static String encode(byte[] data)
      编码字节数组为 Base62 字符串。

      示例:Base62Util.encode("hello".getBytes(UTF_8)) -> "7tQLFHz"

      Parameters:
      data - 待编码字节,可为 null
      Returns:
      Base62 字符串;null 返回 null
    • encode

      public static String encode(String text, Charset charset)
      编码字符串为 Base62 字符串(默认 UTF-8)。
      Parameters:
      text - 待编码字符串,可为 null
      charset - 字符集,null 时使用 UTF-8
      Returns:
      Base62 字符串;null 返回 null
    • decode

      public static byte[] decode(String base62)
      解码 Base62 字符串为字节数组。

      示例:Base62Util.decode("7tQLFHz") -> "hello" 的 UTF-8 字节

      Parameters:
      base62 - Base62 字符串,可为 null
      Returns:
      解码字节数组;null 返回 null,空串返回空数组
      Throws:
      IllegalArgumentException - 含非 Base62 字符
    • decodeToString

      public static String decodeToString(String base62)
      解码 Base62 字符串为 UTF-8 字符串。
      Parameters:
      base62 - Base62 字符串,可为 null
      Returns:
      解码字符串;null 返回 null
    • decodeToString

      public static String decodeToString(String base62, Charset charset)
      解码 Base62 字符串为字符串。
      Parameters:
      base62 - Base62 字符串,可为 null
      charset - 字符集,null 时使用 UTF-8
      Returns:
      解码字符串;null 返回 null
    • isBase62

      public static boolean isBase62(CharSequence text)
      判断字符串是否为合法的 Base62 文本。
      Parameters:
      text - 待校验文本,可为 null
      Returns:
      非空且所有字符均在 Base62 字母表内返回 true