public static String encode ( byte [ ] rawData ) { StringBuilder buffer = new StringBuilder ( 4 * rawData . length / 3 ) ; int pos = 0 ; int iterations = rawData . length / 3 ; for ( int i = 0 ; i < iterations ; i ++ ) { int value = ( ( rawData [ pos ++ ] & 0xFF ) << 16 ) | ( ( rawData [ pos ++ ] & 0xFF ) << 8 ) | ( rawData [ pos ++ ] & 0xFF ) ; buffer . append ( BASE64_ALPHABET [ ( value >>> 18 ) & 0x3F ] ) ; buffer . append ( BASE64_ALPHABET [ ( value >>> 12 ) & 0x3F ] ) ; buffer . append ( BASE64_ALPHABET [ ( value >>> 6 ) & 0x3F ] ) ; buffer . append ( BASE64_ALPHABET [ value & 0x3F ] ) ; } switch ( rawData . length % 3 ) { case 1 : buffer . append ( BASE64_ALPHABET [ ( rawData [ pos ] >>> 2 ) & 0x3F ] ) ; buffer . append ( BASE64_ALPHABET [ ( rawData [ pos ] << 4 ) & 0x3F ] ) ; buffer . append ( "==" ) ; break ; case 2 : int value = ( ( rawData [ pos ++ ] & 0xFF ) << 8 ) | ( rawData [ pos ] & 0xFF ) ; buffer . append ( BASE64_ALPHABET [ ( value >>> 10 ) & 0x3F ] ) ; buffer . append ( BASE64_ALPHABET [ ( value >>> 4 ) & 0x3F ] ) ; buffer . append ( BASE64_ALPHABET [ ( value << 2 ) & 0x3F ] ) ; buffer . append ( "=" ) ; break ; } return buffer . toString ( ) ; }
Encodes the provided raw data using base64 .