Java Base64 Encode toBase64(byte[] aValue)

Here you can find the source of toBase64(byte[] aValue)

Description

Convert a byte array into a Base64 string (as used in mime formats)

License

Apache License

Declaration

public static String toBase64(byte[] aValue) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from www.ja v  a  2 s  .  c om*/
     * Convert a byte array into a Base64 string (as used in mime formats)
     */
    public static String toBase64(byte[] aValue) {

        final String m_strBase64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

        int byte1;
        int byte2;
        int byte3;
        int iByteLen = aValue.length;
        StringBuffer tt = new StringBuffer();

        for (int i = 0; i < iByteLen; i += 3) {
            boolean bByte2 = (i + 1) < iByteLen;
            boolean bByte3 = (i + 2) < iByteLen;
            byte1 = aValue[i] & 0xFF;
            byte2 = (bByte2) ? (aValue[i + 1] & 0xFF) : 0;
            byte3 = (bByte3) ? (aValue[i + 2] & 0xFF) : 0;

            tt.append(m_strBase64Chars.charAt(byte1 / 4));
            tt.append(m_strBase64Chars.charAt((byte2 / 16) + ((byte1 & 0x3) * 16)));
            tt.append(((bByte2) ? m_strBase64Chars.charAt((byte3 / 64) + ((byte2 & 0xF) * 4)) : '='));
            tt.append(((bByte3) ? m_strBase64Chars.charAt(byte3 & 0x3F) : '='));
        }

        return tt.toString();
    }
}

Related

  1. encodeBASE64(InputStream in, OutputStream out)
  2. encodeBase64(InputStream is, OutputStream os)
  3. encodeBase64(String data)
  4. encodeBase64(String data)
  5. toBase64(byte[] array)
  6. toBase64(byte[] buf, int start, int length)
  7. toBase64(byte[] data)
  8. toBase64(byte[] data)
  9. toBase64(final byte[] bytes)