jdk8开始已经含有base64编解码方法。这里是针对jdk7的实现,使用方法:Base64Decoder.decode(String str);
01 | package mybase64.decrypt; |
02 |
03 | import java.io.ByteArrayInputStream; |
04 | import java.io.ByteArrayOutputStream; |
05 | import java.io.FilterInputStream; |
06 | import java.io.IOException; |
07 | import java.io.InputStream; |
08 | import java.io.UnsupportedEncodingException; |
09 |
10 |
11 | class Base64Decoder extends FilterInputStream { |
12 |
13 | private static final char [] chars = { 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , |
14 | 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , |
15 | 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , |
16 | 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , |
17 | 'u' , 'v' , 'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' , '3' , '4' , '5' , '6' , |
18 | '7' , '8' , '9' , '+' , '/' }; |
19 |
20 | // A mapping between char values and six-bit integers |
21 | private static final int [] ints = new int [ 128 ]; |
22 | static { |
23 | for ( int i = 0 ; i < 64 ; i++) { |
24 | ints[chars[i]] = i; |
25 | } |
26 | } |
27 |
28 | private int charCount; |
29 | private int carryOver; |
30 |
31 | /*** |
32 | * Constructs a new Base64 decoder that reads input from the given |
33 | * InputStream. |
34 | * |
35 | * @param in |
36 | * the input stream |
37 | */ |
38 | public Base64Decoder(InputStream in) { |
39 | super (in); |
40 | } |
41 |
42 | /*** |
43 | * Returns the next decoded character from the stream, or -1 if end of |
44 | * stream was reached. |
45 | * |
46 | * @return the decoded character, or -1 if the end of the input stream is |
47 | * reached |
48 | * @exception IOException |
49 | * if an I/O error occurs |
50 | */ |
51 | public int read() throws IOException { |
52 | // Read the next non-whitespace character |
53 | int x; |
54 | do { |
55 | x = in.read(); |
56 | if (x == - 1 ) { |
57 | return - 1 ; |
58 | } |
59 | } while (Character.isWhitespace(( char ) x)); |
60 | charCount++; |
61 |
62 | // The '=' sign is just padding |
63 | if (x == '=' ) { |
64 | return - 1 ; // effective end of stream |
65 | } |
66 |
67 | // Convert from raw form to 6-bit form |
68 | x = ints[x]; |
69 |
70 | // Calculate which character we're decoding now |
71 | int mode = (charCount - 1 ) % 4 ; |
72 |
73 | // First char save all six bits, go for another |
74 | if (mode == 0 ) { |
75 | carryOver = x & 63 ; |
76 | return read(); |
77 | } |
78 | // Second char use previous six bits and first two new bits, |
79 | // save last four bits |
80 | else if (mode == 1 ) { |
81 | int decoded = ((carryOver << 2 = "" >> 4 )) & 255 ; |
82 | carryOver = x & 15 ; |
83 | return decoded; |
84 | } |
85 | // Third char use previous four bits and first four new bits, |
86 | // save last two bits |
87 | else if (mode == 2 ) { |
88 | int decoded = ((carryOver << 4 = "" >> 2 )) & 255 ; |
89 | carryOver = x & 3 ; |
90 | return decoded; |
91 | } |
92 | // Fourth char use previous two bits and all six new bits |
93 | else if (mode == 3 ) { |
94 | int decoded = ((carryOver << 6 = "" > |