Top Description Inners Fields Constructors Methods
com.google.common.io

public abstract Class BaseEncoding

extends Object
Class Inheritance
Known Direct Subclasses
com.google.common.io.BaseEncoding.StandardBaseEncoding, com.google.common.io.BaseEncoding.SeparatedBaseEncoding
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkPositionIndexes, .Preconditions.checkState, com.google.common.math.IntMath.divide, .IntMath.log2, java.math.RoundingMode.CEILING, .RoundingMode.FLOOR, .RoundingMode.UNNECESSARY

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:
BaseEncoding.base32().encode("foo".getBytes(Charsets.US_ASCII))

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:

BaseEncoding.base16().lowerCase().decode("deadbeef");

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.

Encodings
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.

Author
Louis Wasserman
Since
14.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
pack-priv static class
pack-priv static class
public static class
BaseEncoding.DecodingException

Exception indicating invalid base-encoded input encountered while decoding.

pack-priv static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
private static final BaseEncoding
private static final BaseEncoding
private static final BaseEncoding
private static final BaseEncoding
private static final BaseEncoding

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public static BaseEncoding
base16()

The "base16" encoding specified by RFC 4648 section 8, Base 16 Encoding.

public static BaseEncoding
base32()

The "base32" encoding specified by RFC 4648 section 6, Base 32 Encoding.

public static BaseEncoding
base32Hex()

The "base32hex" encoding specified by RFC 4648 section 7, Base 32 Encoding with Extended Hex Alphabet.

public static BaseEncoding
base64()

The "base64" base encoding specified by RFC 4648 section 4, Base 64 Encoding.

public static BaseEncoding
base64Url()

The "base64url" encoding specified by RFC 4648 section 5, Base 64 Encoding with URL and Filename Safe Alphabet, also sometimes referred to as the "web safe Base64." (This is the same as the base 64 encoding with URL and filename safe alphabet from RFC 3548.)

The character '=' is used for padding, but can be omitted or replaced.

public abstract boolean
canDecode(CharSequence chars)

Determines whether the specified character sequence is a valid encoded string according to this encoding.

public final byte[]
decode(CharSequence chars)

Decodes the specified character sequence, and returns the resulting byte[].

pack-priv final byte[]
decodeChecked(CharSequence chars)

Decodes the specified character sequence, and returns the resulting byte[].

pack-priv abstract int
decodeTo(byte[] target, CharSequence chars)

public final ByteSource
decodingSource(CharSource encodedSource)

Returns a ByteSource that reads base-encoded bytes from the specified CharSource.

public abstract InputStream
decodingStream(Reader reader)

Returns an InputStream that decodes base-encoded input from the specified Reader.

public String
encode(byte[] bytes)

Encodes the specified byte array, and returns the encoded String.

public final String
encode(byte[] bytes, int off, int len)

Encodes the specified range of the specified byte array, and returns the encoded String.

pack-priv abstract void
encodeTo(Appendable target, byte[] bytes, int off, int len)

public final ByteSink
encodingSink(CharSink encodedSink)

Returns a ByteSink that writes base-encoded bytes to the specified CharSink.

public abstract OutputStream
encodingStream(Writer writer)

Returns an OutputStream that encodes bytes using this encoding into the specified Writer.

private static byte[]
extract(byte[] result, int length)

public abstract BaseEncoding
ignoreCase()

Returns an encoding that behaves equivalently to this encoding, but decodes letters without regard to case.

pack-priv static Reader
ignoringReader(Reader delegate, String toIgnore)

public abstract BaseEncoding
lowerCase()

Returns an encoding that behaves equivalently to this encoding, but encodes and decodes with lowercase letters.

pack-priv abstract int
maxDecodedSize(int chars)

pack-priv abstract int
maxEncodedSize(int bytes)

public abstract BaseEncoding
omitPadding()

Returns an encoding that behaves equivalently to this encoding, but omits any padding characters as specified by RFC 4648 section 3.2, Padding of Encoded Data.

pack-priv static Appendable
separatingAppendable(Appendable delegate, String separator, int afterEveryChars)

pack-priv static Writer
separatingWriter(Writer delegate, String separator, int afterEveryChars)

pack-priv CharSequence
public abstract BaseEncoding
upperCase()

Returns an encoding that behaves equivalently to this encoding, but encodes and decodes with uppercase letters.

public abstract BaseEncoding
withPadChar(char padChar)

Returns an encoding that behaves equivalently to this encoding, but uses an alternate character for padding.

public abstract BaseEncoding
withSeparator(String separator, int n)

Returns an encoding that behaves equivalently to this encoding, but adds a separator string after every n characters.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait