public static byte[] decode(byte[] in,int len){
  int length=len / 4 * 3;
  if (length == 0) {
    return new byte[0];
  }
  byte[] out=new byte[length];
  int pad=0;
  byte chr;
  for (; ; len--) {
    chr=in[len - 1];
    if ((chr == '\n') || (chr == '\r') || (chr == ' ')|| (chr == '\t')) {
      continue;
    }
    if (chr == '=') {
      pad++;
    }
 else {
      break;
    }
  }
  int out_index=0;
  int in_index=0;
  int bits=0;
  int quantum=0;
  for (int i=0; i < len; i++) {
    chr=in[i];
    if ((chr == '\n') || (chr == '\r') || (chr == ' ')|| (chr == '\t')) {
      continue;
    }
    if ((chr >= 'A') && (chr <= 'Z')) {
      bits=chr - 65;
    }
 else     if ((chr >= 'a') && (chr <= 'z')) {
      bits=chr - 71;
    }
 else     if ((chr >= '0') && (chr <= '9')) {
      bits=chr + 4;
    }
 else     if (chr == '+') {
      bits=62;
    }
 else     if (chr == '/') {
      bits=63;
    }
 else {
      return null;
    }
    quantum=(quantum << 6) | (byte)bits;
    if (in_index % 4 == 3) {
      out[out_index++]=(byte)((quantum & 0x00FF0000) >> 16);
      out[out_index++]=(byte)((quantum & 0x0000FF00) >> 8);
      out[out_index++]=(byte)(quantum & 0x000000FF);
    }
    in_index++;
  }
  if (pad > 0) {
    quantum=quantum << (6 * pad);
    out[out_index++]=(byte)((quantum & 0x00FF0000) >> 16);
    if (pad == 1) {
      out[out_index++]=(byte)((quantum & 0x0000FF00) >> 8);
    }
  }
  byte[] result=new byte[out_index];
  System.arraycopy(out,0,result,0,out_index);
  return result;
}

public static byte [ ] decode ( byte [ ] in , int len ) { int length = len / 4 * 3 ; if ( length == 0 ) { return new byte [ 0 ] ; } byte [ ] out = new byte [ length ] ; int pad = 0 ; byte chr ; for ( ; ; len -- ) { chr = in [ len - 1 ] ; if ( ( chr == '\n' ) || ( chr == '\r' ) || ( chr == ' ' ) || ( chr == '\t' ) ) { continue ; } if ( chr == '=' ) { pad ++ ; } else { break ; } } int out_index = 0 ; int in_index = 0 ; int bits = 0 ; int quantum = 0 ; for ( int i = 0 ; i < len ; i ++ ) { chr = in [ i ] ; if ( ( chr == '\n' ) || ( chr == '\r' ) || ( chr == ' ' ) || ( chr == '\t' ) ) { continue ; } if ( ( chr >= 'A' ) && ( chr <= 'Z' ) ) { bits = chr - 65 ; } else if ( ( chr >= 'a' ) && ( chr <= 'z' ) ) { bits = chr - 71 ; } else if ( ( chr >= '0' ) && ( chr <= '9' ) ) { bits = chr + 4 ; } else if ( chr == '+' ) { bits = 62 ; } else if ( chr == '/' ) { bits = 63 ; } else { return null ; } quantum = ( quantum << 6 ) | ( byte ) bits ; if ( in_index % 4 == 3 ) { out [ out_index ++ ] = ( byte ) ( ( quantum & 0x00FF0000 ) > > 16 ) ; out [ out_index ++ ] = ( byte ) ( ( quantum & 0x0000FF00 ) > > 8 ) ; out [ out_index ++ ] = ( byte ) ( quantum & 0x000000FF ) ; } in_index ++ ; } if ( pad > 0 ) { quantum = quantum << ( 6 * pad ) ; out [ out_index ++ ] = ( byte ) ( ( quantum & 0x00FF0000 ) > > 16 ) ; if ( pad == 1 ) { out [ out_index ++ ] = ( byte ) ( ( quantum & 0x0000FF00 ) > > 8 ) ; } } byte [ ] result = new byte [ out_index ] ; System . arraycopy ( out , 0 , result , 0 , out_index ) ; return result ; }
decodes an array of bytes containing a base64 ascii string into binary data
