public static void decode(Reader in,OutputStream out) throws Exception {
  char[] chars=new char[4];
  int[] sixbit=new int[4];
  while (true) {
    int numread=0;
    while (numread < 4) {
      int read=in.read();
      if (read < 0) {
        break;
      }
      char aChar=(char)read;
      if (Character.isWhitespace(aChar)) {
        continue;
      }
      chars[numread++]=aChar;
    }
    if (numread == 0) {
      return;
    }
    if (numread != 4) {
      throw new Exception("Incomplete character quartet at end of Base64 input");
    }
    for (int i=0; i < 4; i++) {
      Integer value=(Integer)charLookup.get(new Character(chars[i]));
      if (value == null) {
        if (chars[i] != '=' || i < 2) {
          throw new Exception("Invalid char (" + chars[i] + ") in Base64 data");
        }
        sixbit[i]=-1;
      }
 else {
        sixbit[i]=value.intValue();
      }
    }
    out.write((sixbit[0] << 2) + (sixbit[1] >> 4));
    int val=(sixbit[1] & 0xf) << 4;
    if (sixbit[2] >= 0) {
      val+=sixbit[2] >> 2;
      out.write(val);
      val=(sixbit[2] & 0x3) << 6;
      if (sixbit[3] >= 0) {
        val+=sixbit[3];
        out.write(val);
      }
    }
  }
}

public static void decode ( Reader in , OutputStream out ) throws Exception { char [ ] chars = new char [ 4 ] ; int [ ] sixbit = new int [ 4 ] ; while ( true ) { int numread = 0 ; while ( numread < 4 ) { int read = in . read ( ) ; if ( read < 0 ) { break ; } char aChar = ( char ) read ; if ( Character . isWhitespace ( aChar ) ) { continue ; } chars [ numread ++ ] = aChar ; } if ( numread == 0 ) { return ; } if ( numread != 4 ) { throw new Exception ( "Incomplete character quartet at end of Base64 input" ) ; } for ( int i = 0 ; i < 4 ; i ++ ) { Integer value = ( Integer ) charLookup . get ( new Character ( chars [ i ] ) ) ; if ( value == null ) { if ( chars [ i ] != '=' || i < 2 ) { throw new Exception ( "Invalid char (" + chars [ i ] + ") in Base64 data" ) ; } sixbit [ i ] = - 1 ; } else { sixbit [ i ] = value . intValue ( ) ; } } out . write ( ( sixbit [ 0 ] << 2 ) + ( sixbit [ 1 ] > > 4 ) ) ; int val = ( sixbit [ 1 ] & 0xf ) << 4 ; if ( sixbit [ 2 ] >= 0 ) { val += sixbit [ 2 ] > > 2 ; out . write ( val ) ; val = ( sixbit [ 2 ] & 0x3 ) << 6 ; if ( sixbit [ 3 ] >= 0 ) { val += sixbit [ 3 ] ; out . write ( val ) ; } } } }
description of the method
