public static byte [ ] decodeBase64 ( String value ) { int byteShift = 4 ; int tmp = 0 ; boolean done = false ; final StringBuilder buffer = new StringBuilder ( ) ; for ( int i = 0 ; i != value . length ( ) ; i ++ ) { final char c = value . charAt ( i ) ; final int sixBit = ( c < 123 ) ? EncodingGroovyMethodsSupport . TRANSLATE_TABLE [ c ] : 66 ; if ( sixBit < 64 ) { if ( done ) throw new RuntimeException ( "= character not at end of base64 value" ) ; // TODO: change this exception type tmp = ( tmp << 6 ) | sixBit ; if ( byteShift -- != 4 ) { buffer . append ( ( char ) ( ( tmp >> ( byteShift * 2 ) ) & 0XFF ) ) ; } } else if ( sixBit == 64 ) { byteShift -- ; done = true ; } else if ( sixBit == 66 ) { // RFC 2045 says that I'm allowed to take the presence of // these characters as evidence of data corruption // So I will throw new RuntimeException ( "bad character in base64 value" ) ; // TODO: change this exception type } if ( byteShift == 0 ) byteShift = 4 ; } try { return buffer . toString ( ) . getBytes ( "ISO-8859-1" ) ; } catch ( UnsupportedEncodingException e ) { throw new RuntimeException ( "Base 64 decode produced byte values > 255" ) ; // TODO: change this exception type } }
Decode the String from Base64 into a byte array .