public static byte [ ] encodeAsBytes ( byte [ ] inArray ) { // Output string must be 4 * floor((n+2)/3) large byte [ ] out = new byte [ 4 * ( ( inArray . length + 2 ) / 3 ) ] ; // Create padded input array with the next largest length that is a multiple of 3 byte [ ] in = new byte [ ( inArray . length + 2 ) / 3 * 3 ] ; // Copy content form unpadded to padded array System . arraycopy ( inArray , 0 , in , 0 , inArray . length ) ; int outi = 0 ; for ( int i = 0 ; i < in . length ; i += 3 ) { out [ outi ++ ] = ( byte ) ( ( in [ i ] & 0xFF ) >>> 2 ) ; out [ outi ++ ] = ( byte ) ( ( ( in [ i ] & 0x03 ) << 4 ) | ( ( in [ i + 1 ] & 0xFF ) >>> 4 ) ) ; out [ outi ++ ] = ( byte ) ( ( ( in [ i + 1 ] & 0x0F ) << 2 ) | ( ( in [ i + 2 ] & 0xFF ) >>> 6 ) ) ; out [ outi ++ ] = ( byte ) ( in [ i + 2 ] & 0x3F ) ; } for ( int i = in . length - inArray . length ; i > 0 ; i -- ) { out [ out . length - i ] = - 1 ; } return out ; }
Encode a byte array and return the encoded byte array . Bytes that has been appended to pad the string to a multiple of four are set to - 1 in the array .