class Utils {

   public static byte [] b64Decode(CharSequence encoded) {
      if(encoded.length() % 4 != 0) {
         throw new IllegalArgumentException("Invalid base64 input, must have a multiple of 4 characters");
      }

      int resultLength = (encoded.length() / 4) * 3;
      if(encoded.charAt(encoded.length() - 1) == '=') {
         resultLength--;
      }
      if(encoded.charAt(encoded.length() - 2) == '=') {
         resultLength--;
      }
      byte [] decoded = new byte[resultLength];
      for(int i=3,o=2; o<resultLength; i+=4,o+=3) {
         int i0 = reverseLookup(encoded.charAt(i - 3));
         int i1 = reverseLookup(encoded.charAt(i - 2));
         int i2 = reverseLookup(encoded.charAt(i - 1));
         int i3 = reverseLookup(encoded.charAt(i));

         decoded[o - 2] = (byte)( (i0 << 2) | (i1 >> 4) );
         decoded[o - 1] = (byte)( ((i1 << 4) & 0xf0) | (i2 >> 2) );
         decoded[o]     = (byte)( ((i2 << 6) & 0xc0) | i3 );
      }
      if(resultLength % 3 == 2) {
         int i0 = reverseLookup(encoded.charAt(encoded.length() - 4));
         int i1 = reverseLookup(encoded.charAt(encoded.length() - 3));
         int i2 = reverseLookup(encoded.charAt(encoded.length() - 2));
         decoded[resultLength - 2] = (byte)( (i0 << 2) | (i1 >> 4) );
         decoded[resultLength - 1] = (byte)( ((i1 << 4) & 0xf0) | (i2 >> 2) );
      }
      if(resultLength % 3 == 1) {
         int i0 = reverseLookup(encoded.charAt(encoded.length() - 4));
         int i1 = reverseLookup(encoded.charAt(encoded.length() - 3));
         decoded[resultLength - 1] = (byte)( (i0 << 2) | (i1 >> 4) );
      }
      return decoded;
   }

   private  Utils();

}