public static byte []
fromString(String str) {
	ByteArrayOutputStream bs = new ByteArrayOutputStream();
	byte [] raw = str.getBytes();
	for (int i = 0; i < raw.length; i++) {
		if (!Character.isWhitespace((char)raw[i]))
			bs.write(raw[i]);
	}
	byte [] in = bs.toByteArray();
	if (in.length % 4 != 0) {
		return null;
	}

	bs.reset();
	DataOutputStream ds = new DataOutputStream(bs);

	for (int i = 0; i < (in.length + 3) / 4; i++) {
		short [] s = new short[4];
		short [] t = new short[3];

		for (int j = 0; j < 4; j++)
			s[j] = (short) Base64.indexOf(in[i*4+j]);

		t[0] = (short) ((s[0] << 2) + (s[1] >> 4));
		if (s[2] == 64) {
			t[1] = t[2] = (short) (-1);
			if ((s[1] & 0xF) != 0)
				return null;
		}
		else if (s[3] == 64) {
			t[1] = (short) (((s[1] << 4) + (s[2] >> 2)) & 0xFF);
			t[2] = (short) (-1);
			if ((s[2] & 0x3) != 0)
				return null;
		}
		else {
			t[1] = (short) (((s[1] << 4) + (s[2] >> 2)) & 0xFF);
			t[2] = (short) (((s[2] << 6) + s[3]) & 0xFF);
		}

		try {
			for (int j = 0; j < 3; j++)
				if (t[j] >= 0)
					ds.writeByte(t[j]);
		}
		catch (IOException e) {
		}
	}
	return bs.toByteArray();
}
public static byte [ ] fromString ( String str ) { ByteArrayOutputStream bs = new ByteArrayOutputStream ( ) ; byte [ ] raw = str . getBytes ( ) ; for ( int i = 0 ; i < raw . length ; i ++ ) { if ( ! Character . isWhitespace ( ( char ) raw [ i ] ) ) bs . write ( raw [ i ] ) ; } byte [ ] in = bs . toByteArray ( ) ; if ( in . length % 4 != 0 ) { return null ; } bs . reset ( ) ; DataOutputStream ds = new DataOutputStream ( bs ) ; for ( int i = 0 ; i < ( in . length + 3 ) / 4 ; i ++ ) { short [ ] s = new short [ 4 ] ; short [ ] t = new short [ 3 ] ; for ( int j = 0 ; j < 4 ; j ++ ) s [ j ] = ( short ) Base64 . indexOf ( in [ i * 4 + j ] ) ; t [ 0 ] = ( short ) ( ( s [ 0 ] << 2 ) + ( s [ 1 ] > > 4 ) ) ; if ( s [ 2 ] == 64 ) { t [ 1 ] = t [ 2 ] = ( short ) ( - 1 ) ; if ( ( s [ 1 ] & 0xF ) != 0 ) return null ; } else if ( s [ 3 ] == 64 ) { t [ 1 ] = ( short ) ( ( ( s [ 1 ] << 4 ) + ( s [ 2 ] > > 2 ) ) & 0xFF ) ; t [ 2 ] = ( short ) ( - 1 ) ; if ( ( s [ 2 ] & 0x3 ) != 0 ) return null ; } else { t [ 1 ] = ( short ) ( ( ( s [ 1 ] << 4 ) + ( s [ 2 ] > > 2 ) ) & 0xFF ) ; t [ 2 ] = ( short ) ( ( ( s [ 2 ] << 6 ) + s [ 3 ] ) & 0xFF ) ; } try { for ( int j = 0 ; j < 3 ; j ++ ) if ( t [ j ] >= 0 ) ds . writeByte ( t [ j ] ) ; } catch ( IOException e ) { } } return bs . toByteArray ( ) ; }
Convert a base64-encoded String to binary data
@param str A String containing the encoded data
@return An array containing the binary data, or null if the string is invalid