Java Utililty Methods Base64

List of utility methods to do Base64

Description

The list of methods to do Base64 are organized into topic(s).

Method

booleanbase64Append(StringBuilder sb, int digit, boolean haveNonZero)
base Append
if (digit > 0) {
    haveNonZero = true;
if (haveNonZero) {
    int c;
    if (digit < 26) {
        c = 'A' + digit;
    } else if (digit < 52) {
...
charbase64Character(int number)
base Character
if (number < 0 || number > 63) {
    return ' ';
return BASE_64_ENCODING.charAt(number);
intbase64CharToValue(byte c)
base Char To Value
if (c >= 'A' && c <= 'Z') {
    return c - 'A';
if (c >= 'a' && c <= 'z') {
    return 26 + (c - 'a');
if (c >= '0' && c <= '9') {
    return 52 + (c - '0');
...
Stringbase64enc(byte[] in)
baseenc
StringBuilder buf = new StringBuilder();
int p = 0;
while (in.length - p >= 3) {
    buf.append(base64set.charAt((in[p + 0] & 0xfc) >> 2));
    buf.append(base64set.charAt(((in[p + 0] & 0x03) << 4) | ((in[p + 1] & 0xf0) >> 4)));
    buf.append(base64set.charAt(((in[p + 1] & 0x0f) << 2) | ((in[p + 2] & 0xc0) >> 6)));
    buf.append(base64set.charAt(in[p + 2] & 0x3f));
    p += 3;
...
Stringbase64Pad(String s)
Pads a given String s to be usable for BASE64Decoder (if it isn't padded the resulting deoceded data may be wrong)
int toPad = s.length() % 4;
for (int i = 0; i < toPad; ++i) {
    s = s + "=";
return s;
booleanbase64ReadChunk(InputStream in, byte[] chunk)
Fully reads a chunk of data from an IO stream.
int count = in.read(chunk);
if (count < 0) {
    return false; 
} else if (count == chunk.length) {
    return true; 
} else {
    throw new IOException("BASE64Decoder: Not enough bytes for an atom.");
bytebase64ToBits(char data)
Converts a Base64 char to to corresponding 6-bit value
if (data >= 'A' && data <= 'Z') {
    return (byte) (data - 'A');
if (data >= 'a' && data <= 'z') {
    return (byte) (data - 'a' + 26);
if (data >= '0' && data <= '9') {
    return (byte) (data - '0' + 52);
...
byte[]base64ToByteArray(String s)
Translates the specified Base64 string (as per Preferences.get(byte[])) into a byte array.
return base64ToByteArray(s, false);
byte[]base64ToBytes(final String base64)
Converts the Base64 (RFC 2045) String into a byte array.
final int base64Length = base64.length();
final int numGroups = base64Length / 4;
if ((4 * numGroups) != base64Length) {
    throw new IllegalArgumentException("String length must be a multiple of four.");
int missingBytesInLastGroup = 0;
int numFullGroups = numGroups;
if (base64Length != 0) {
...
byte[]base64ToBytes(String value)
Converts a base64 encoded string of four chars to the corresponding bytes
if (value.length() != 4) {
    return null;
int paddingOffset = value.indexOf("=");
if (paddingOffset == -1) {
    byte[] result = new byte[3];
    result[0] = (byte) (base64ToBits(value.charAt(0)) << 2 | base64ToBits(value.charAt(1)) >> 4);
    result[1] = (byte) (base64ToBits(value.charAt(1)) << 4 | base64ToBits(value.charAt(2)) >> 2);
...