public static String encode ( byte [ ] bytes ) { int length = bytes . length ; if ( length == 0 ) return "" ; StringBuffer buffer = new StringBuffer ( ( int ) Math . ceil ( ( double ) length / 3d ) * 4 ) ; int remainder = length % 3 ; length -= remainder ; int block ; int i = 0 ; while ( i < length ) { block = ( ( bytes [ i ++ ] & 0xff ) << 16 ) | ( ( bytes [ i ++ ] & 0xff ) << 8 ) | ( bytes [ i ++ ] & 0xff ) ; buffer . append ( ALPHABET . charAt ( block >>> 18 ) ) ; buffer . append ( ALPHABET . charAt ( ( block >>> 12 ) & 0x3f ) ) ; buffer . append ( ALPHABET . charAt ( ( block >>> 6 ) & 0x3f ) ) ; buffer . append ( ALPHABET . charAt ( block & 0x3f ) ) ; } if ( remainder == 0 ) return buffer . toString ( ) ; if ( remainder == 1 ) { block = ( bytes [ i ] & 0xff ) << 4 ; buffer . append ( ALPHABET . charAt ( block >>> 6 ) ) ; buffer . append ( ALPHABET . charAt ( block & 0x3f ) ) ; buffer . append ( "==" ) ; return buffer . toString ( ) ; } block = ( ( ( bytes [ i ++ ] & 0xff ) << 8 ) | ( ( bytes [ i ] ) & 0xff ) ) << 2 ; buffer . append ( ALPHABET . charAt ( block >>> 12 ) ) ; buffer . append ( ALPHABET . charAt ( ( block >>> 6 ) & 0x3f ) ) ; buffer . append ( ALPHABET . charAt ( block & 0x3f ) ) ; buffer . append ( "=" ) ; return buffer . toString ( ) ; }
Base - 64 encodes the supplied block of data . Line wrapping is not applied on output .