public static byte[] base64decode(String coded){
  if (null == coded)   return null;
  byte[] src=coded.getBytes();
  int len=src.length;
  int dlen=len - (len / 77);
  dlen=(dlen >>> 2) + (dlen >>> 1);
  int rem=0;
  if (61 == src[len - 1])   rem++;
  if (61 == src[len - 2])   rem++;
  dlen-=rem;
  byte[] dst=new byte[dlen];
  int pos=0;
  int dpos=0;
  int col=0;
  len-=4;
  while (pos < len) {
    byte b0=_base64de[src[pos++]];
    byte b1=_base64de[src[pos++]];
    byte b2=_base64de[src[pos++]];
    byte b3=_base64de[src[pos++]];
    if (B64INV == b0 || B64INV == b1 || B64INV == b2 || B64INV == b3)     throw new RuntimeException("Invalid character at or around position " + pos);
    dst[dpos++]=(byte)((b0 << 2) | ((b1 >>> 4) & 0x03));
    dst[dpos++]=(byte)((b1 << 4) | ((b2 >>> 2) & 0x0f));
    dst[dpos++]=(byte)((b2 << 6) | (b3 & 0x3f));
    col+=4;
    if (76 == col) {
      if (10 != src[pos++])       throw new RuntimeException("No linefeed found at position " + (pos - 1));
      col=0;
    }
  }
  byte b0=_base64de[src[pos++]];
  byte b1=_base64de[src[pos++]];
  byte b2=_base64de[src[pos++]];
  byte b3=_base64de[src[pos++]];
  if (B64INV == b0 || B64INV == b1 || B64INV == b2 || B64INV == b3)   throw new RuntimeException("Invalid character at or around position " + pos);
  dst[dpos++]=(byte)((b0 << 2) | ((b1 >>> 4) & 0x03));
  if (2 == rem)   return dst;
  dst[dpos++]=(byte)((b1 << 4) | ((b2 >>> 2) & 0x0f));
  if (1 == rem)   return dst;
  dst[dpos++]=(byte)((b2 << 6) | (b3 & 0x3f));
  return dst;
}

public static byte [ ] base64decode ( String coded ) { if ( null == coded ) return null ; byte [ ] src = coded . getBytes ( ) ; int len = src . length ; int dlen = len - ( len / 77 ) ; dlen = ( dlen > > > 2 ) + ( dlen > > > 1 ) ; int rem = 0 ; if ( 61 == src [ len - 1 ] ) rem ++ ; if ( 61 == src [ len - 2 ] ) rem ++ ; dlen -= rem ; byte [ ] dst = new byte [ dlen ] ; int pos = 0 ; int dpos = 0 ; int col = 0 ; len -= 4 ; while ( pos < len ) { byte b0 = _base64de [ src [ pos ++ ] ] ; byte b1 = _base64de [ src [ pos ++ ] ] ; byte b2 = _base64de [ src [ pos ++ ] ] ; byte b3 = _base64de [ src [ pos ++ ] ] ; if ( B64INV == b0 || B64INV == b1 || B64INV == b2 || B64INV == b3 ) throw new RuntimeException ( "Invalid character at or around position " + pos ) ; dst [ dpos ++ ] = ( byte ) ( ( b0 << 2 ) | ( ( b1 > > > 4 ) & 0x03 ) ) ; dst [ dpos ++ ] = ( byte ) ( ( b1 << 4 ) | ( ( b2 > > > 2 ) & 0x0f ) ) ; dst [ dpos ++ ] = ( byte ) ( ( b2 << 6 ) | ( b3 & 0x3f ) ) ; col += 4 ; if ( 76 == col ) { if ( 10 != src [ pos ++ ] ) throw new RuntimeException ( "No linefeed found at position " + ( pos - 1 ) ) ; col = 0 ; } } byte b0 = _base64de [ src [ pos ++ ] ] ; byte b1 = _base64de [ src [ pos ++ ] ] ; byte b2 = _base64de [ src [ pos ++ ] ] ; byte b3 = _base64de [ src [ pos ++ ] ] ; if ( B64INV == b0 || B64INV == b1 || B64INV == b2 || B64INV == b3 ) throw new RuntimeException ( "Invalid character at or around position " + pos ) ; dst [ dpos ++ ] = ( byte ) ( ( b0 << 2 ) | ( ( b1 > > > 4 ) & 0x03 ) ) ; if ( 2 == rem ) return dst ; dst [ dpos ++ ] = ( byte ) ( ( b1 << 4 ) | ( ( b2 > > > 2 ) & 0x0f ) ) ; if ( 1 == rem ) return dst ; dst [ dpos ++ ] = ( byte ) ( ( b2 << 6 ) | ( b3 & 0x3f ) ) ; return dst ; }
decodes a base64 encoded string . < br / > ( cf . rfc 1341 section 5 . 2 ) note : this decoder silently ignores all legal line breaks in the input and throws a runtimeexception on any illegal input . this impl slightly outperforms sun ' s decoder on 1 . 3 and heavily outperforms it on 1 . 4 ( about a third faster ) .
