Class BaseEncoding

  • @GwtCompatible(emulated=true)
    public abstract class BaseEncoding
    extends Object
    A binary encoding scheme for reversibly translating between byte sequences and printable ASCII strings. This class includes several constants for encoding schemes specified by RFC 4648. For example, the expression:

    returns the string "MZXW6===", and

     byte[] decoded = BaseEncoding.base32().decode("MZXW6===");

    ...returns the ASCII bytes of the string "foo".

    By default, BaseEncoding's behavior is relatively strict and in accordance with RFC 4648. Decoding rejects characters in the wrong case, though padding is optional. To modify encoding and decoding behavior, use configuration methods to obtain a new encoding with modified behavior:


    Warning: BaseEncoding instances are immutable. Invoking a configuration method has no effect on the receiving instance; you must store and use the new encoding instance it returns, instead.

     // Do NOT do this
     BaseEncoding hex = BaseEncoding.base16();
     hex.lowerCase(); // does nothing!
     return hex.decode("deadbeef"); // throws an IllegalArgumentException

    It is guaranteed that encoding.decode(encoding.encode(x)) is always equal to x, but the reverse does not necessarily hold.

    Encoding Alphabet char:byte ratio Default padding Comments
    base16() 0-9 A-F 2.00 N/A Traditional hexadecimal. Defaults to upper case.
    base32() A-Z 2-7 1.60 = Human-readable; no possibility of mixing up 0/O or 1/I. Defaults to upper case.
    base32Hex() 0-9 A-V 1.60 = "Numerical" base 32; extended from the traditional hex alphabet. Defaults to upper case.
    base64() A-Z a-z 0-9 + / 1.33 =
    base64Url() A-Z a-z 0-9 - _ 1.33 = Safe to use as filenames, or to pass in URLs without escaping

    All instances of this class are immutable, so they may be stored safely as static constants.

    Louis Wasserman