public static byte [ ] fromBase64 ( String data ) { if ( data == null ) { return null ; } int len = data . length ( ) ; assert ( len % 4 ) == 0 ; if ( len == 0 ) { return new byte [ 0 ] ; } char [ ] chars = new char [ len ] ; data . getChars ( 0 , len , chars , 0 ) ; int olen = 3 * ( len / 4 ) ; if ( chars [ len - 2 ] == ' ' ) { -- olen ; } if ( chars [ len - 1 ] == ' ' ) { -- olen ; } byte [ ] bytes = new byte [ olen ] ; int iidx = 0 ; int oidx = 0 ; while ( iidx < len ) { int c0 = base64Values [ chars [ iidx ++ ] & 0xff ] ; int c1 = base64Values [ chars [ iidx ++ ] & 0xff ] ; int c2 = base64Values [ chars [ iidx ++ ] & 0xff ] ; int c3 = base64Values [ chars [ iidx ++ ] & 0xff ] ; int c24 = ( c0 << 18 ) | ( c1 << 12 ) | ( c2 << 6 ) | c3 ; bytes [ oidx ++ ] = ( byte ) ( c24 >> 16 ) ; if ( oidx == olen ) { break ; } bytes [ oidx ++ ] = ( byte ) ( c24 >> 8 ) ; if ( oidx == olen ) { break ; } bytes [ oidx ++ ] = ( byte ) c24 ; } return bytes ; }
Decode a base64 string into a byte array .