1. public class Base64 {  
2. /**
3.      * Default values for encoder/decoder flags.
4.      */  
5. public static final int DEFAULT = 0;  
6.   
7. /**
8.      * Encoder flag bit to omit the padding '=' characters at the end
9.      * of the output (if any).
10.      */  
11. public static final int NO_PADDING = 1;  
12.   
13. /**
14.      * Encoder flag bit to omit all line terminators (i.e., the output
15.      * will be on one long line).
16.      */  
17. public static final int NO_WRAP = 2;  
18.   
19. /**
20.      * Encoder flag bit to indicate lines should be terminated with a
21.      * CRLF pair instead of just an LF.  Has no effect if {@code
22.      * NO_WRAP} is specified as well.
23.      */  
24. public static final int CRLF = 4;  
25.   
26. /**
27.      * Encoder/decoder flag bit to indicate using the "URL and
28.      * filename safe" variant of Base64 (see RFC 3548 section 4) where
29.      * {@code -} and {@code _} are used in place of {@code +} and
30.      * {@code /}.
31.      */  
32. public static final int URL_SAFE = 8;  
33.   
34. /**
35.      * Flag to pass to {@link Base64OutputStream} to indicate that it
36.      * should not close the output stream it is wrapping when it
37.      * itself is closed.
38.      */  
39. public static final int NO_CLOSE = 16;  
40.   
41. //  --------------------------------------------------------  
42. //  shared code  
43. //  --------------------------------------------------------  
44.   
45. /* package */ static abstract class Coder {  
46. public byte[] output;  
47. public int op;  
48.   
49. /**
50.          * Encode/decode another block of input data.  this.output is
51.          * provided by the caller, and must be big enough to hold all
52.          * the coded data.  On exit, this.opwill be set to the length
53.          * of the coded data.
54.          *
55.          * @param finish true if this is the final call to process for
56.          *        this object.  Will finalize the coder state and
57.          *        include any final bytes in the output.
58.          *
59.          * @return true if the input so far is good; false if some
60.          *         error has been detected in the input stream..
61.          */  
62. public abstract boolean process(byte[] input, int offset, int len, boolean finish);  
63.   
64. /**
65.          * @return the maximum number of bytes a call to process()
66.          * could produce for the given number of input bytes.  This may
67.          * be an overestimate.
68.          */  
69. public abstract int maxOutputSize(int len);  
70.     }  
71.   
72. //  --------------------------------------------------------  
73. //  decoding  
74. //  --------------------------------------------------------  
75.   
76. /**
77.      * Decode the Base64-encoded data in input and return the data in
78.      * a new byte array.
79.      *
80.      * <p>The padding '=' characters at the end are considered optional, but
81.      * if any are present, there must be the correct number of them.
82.      *
83.      * @param str    the input String to decode, which is converted to
84.      *               bytes using the default charset
85.      * @param flags  controls certain features of the decoded output.
86.      *               Pass {@code DEFAULT} to decode standard Base64.
87.      *
88.      * @throws IllegalArgumentException if the input contains
89.      * incorrect padding
90.      */  
91. public static byte[] decode(String str, int flags) {  
92. return decode(str.getBytes(), flags);  
93.     }  
94.   
95. /**
96.      * Decode the Base64-encoded data in input and return the data in
97.      * a new byte array.
98.      *
99.      * <p>The padding '=' characters at the end are considered optional, but
100.      * if any are present, there must be the correct number of them.
101.      *
102.      * @param input the input array to decode
103.      * @param flags  controls certain features of the decoded output.
104.      *               Pass {@code DEFAULT} to decode standard Base64.
105.      *
106.      * @throws IllegalArgumentException if the input contains
107.      * incorrect padding
108.      */  
109. public static byte[] decode(byte[] input, int flags) {  
110. return decode(input, 0, input.length, flags);  
111.     }  
112.   
113. /**
114.      * Decode the Base64-encoded data in input and return the data in
115.      * a new byte array.
116.      *
117.      * <p>The padding '=' characters at the end are considered optional, but
118.      * if any are present, there must be the correct number of them.
119.      *
120.      * @param input  the data to decode
121.      * @param offset the position within the input array at which to start
122.      * @param len    the number of bytes of input to decode
123.      * @param flags  controls certain features of the decoded output.
124.      *               Pass {@code DEFAULT} to decode standard Base64.
125.      *
126.      * @throws IllegalArgumentException if the input contains
127.      * incorrect padding
128.      */  
129. public static byte[] decode(byte[] input, int offset, int len, int flags) {  
130. // Allocate space for the most data the input could represent.  
131. // (It could contain less if it contains whitespace, etc.)  
132. new Decoder(flags, new byte[len*3/4]);  
133.   
134. if (!decoder.process(input, offset, len, true)) {  
135. throw new IllegalArgumentException("bad base-64");  
136.         }  
137.   
138. // Maybe we got lucky and allocated exactly enough output space.  
139. if (decoder.op == decoder.output.length) {  
140. return decoder.output;  
141.         }  
142.   
143. // Need to shorten the array, so allocate a new one of the  
144. // right size and copy.  
145. byte[] temp = new byte[decoder.op];  
146. 0, temp, 0, decoder.op);  
147. return temp;  
148.     }  
149.   
150. /* package */ static class Decoder extends Coder {  
151. /**
152.          * Lookup table for turning bytes into their position in the
153.          * Base64 alphabet.
154.          */  
155. private static final int DECODE[] = {  
156. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
157. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
158. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,  
159. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1,  
160. 1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,  
161. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,  
162. 1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,  
163. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,  
164. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
165. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
166. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
167. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
168. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
169. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
170. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
171. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
172.         };  
173.   
174. /**
175.          * Decode lookup table for the "web safe" variant (RFC 3548
176.          * sec. 4) where - and _ replace + and /.
177.          */  
178. private static final int DECODE_WEBSAFE[] = {  
179. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
180. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
181. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1,  
182. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1,  
183. 1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,  
184. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,  
185. 1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,  
186. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,  
187. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
188. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
189. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
190. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
191. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
192. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
193. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
194. 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
195.         };  
196.   
197. /** Non-data values in the DECODE arrays. */  
198. private static final int SKIP = -1;  
199. private static final int EQUALS = -2;  
200.   
201. /**
202.          * States 0-3 are reading through the next input tuple.
203.          * State 4 is having read one '=' and expecting exactly
204.          * one more.
205.          * State 5 is expecting no more data or padding characters
206.          * in the input.
207.          * State 6 is the error state; an error has been detected
208.          * in the input and no future input can "fix" it.
209.          */  
210. private int state;   // state number (0 to 6)  
211. private int value;  
212.   
213. final private int[] alphabet;  
214.   
215. public Decoder(int flags, byte[] output) {  
216. this.output = output;  
217.   
218. 0) ? DECODE : DECODE_WEBSAFE;  
219. 0;  
220. 0;  
221.         }  
222.   
223. /**
224.          * @return an overestimate for the number of bytes {@code
225.          * len} bytes could decode to.
226.          */  
227. public int maxOutputSize(int len) {  
228. return len * 3/4 + 10;  
229.         }  
230.   
231. /**
232.          * Decode another block of input data.
233.          *
234.          * @return true if the state machine is still healthy.  false if
235.          *         bad base-64 data has been detected in the input stream.
236.          */  
237. public boolean process(byte[] input, int offset, int len, boolean finish) {  
238. if (this.state == 6) return false;  
239.   
240. int p = offset;  
241.             len += offset;  
242.   
243. // Using local variables makes the decoder about 12%  
244. // faster than if we manipulate the member variables in  
245. // the loop.  (Even alphabet makes a measurable  
246. // difference, which is somewhat surprising to me since  
247. // the member variable is final.)  
248. int state = this.state;  
249. int value = this.value;  
250. int op = 0;  
251. final byte[] output = this.output;  
252. final int[] alphabet = this.alphabet;  
253.   
254. while (p < len) {  
255. // Try the fast path:  we're starting a new tuple and the  
256. // next four bytes of the input stream are all data  
257. // bytes.  This corresponds to going through states  
258. // 0-1-2-3-0.  We expect to use this method for most of  
259. // the data.  
260. //  
261. // If any of the next four bytes of input are non-data  
262. // (whitespace, etc.), value will end up negative.  (All  
263. // the non-data values in decode are small negative  
264. // numbers, so shifting any of them up and or'ing them  
265. // together will result in a value with its top bit set.)  
266. //  
267. // You can remove this whole block and the output should  
268. // be the same, just slower.  
269. if (state == 0) {  
270. while (p+4 <= len &&  
271. 0xff] << 18) |  
272. 1] & 0xff] << 12) |  
273. 2] & 0xff] << 6) |  
274. 3] & 0xff]))) >= 0) {  
275. 2] = (byte) value;  
276. 1] = (byte) (value >> 8);  
277. byte) (value >> 16);  
278. 3;  
279. 4;  
280.                     }  
281. if (p >= len) break;  
282.                 }  
283.   
284. // The fast path isn't available -- either we've read a  
285. // partial tuple, or the next four input bytes aren't all  
286. // data, or whatever.  Fall back to the slower state  
287. // machine implementation.  
288.   
289. int d = alphabet[input[p++] & 0xff];  
290.   
291. switch (state) {  
292. case 0:  
293. if (d >= 0) {  
294.                         value = d;  
295.                         ++state;  
296. else if (d != SKIP) {  
297. this.state = 6;  
298. return false;  
299.                     }  
300. break;  
301.   
302. case 1:  
303. if (d >= 0) {  
304. 6) | d;  
305.                         ++state;  
306. else if (d != SKIP) {  
307. this.state = 6;  
308. return false;  
309.                     }  
310. break;  
311.   
312. case 2:  
313. if (d >= 0) {  
314. 6) | d;  
315.                         ++state;  
316. else if (d == EQUALS) {  
317. // Emit the last (partial) output tuple;  
318. // expect exactly one more padding character.  
319. byte) (value >> 4);  
320. 4;  
321. else if (d != SKIP) {  
322. this.state = 6;  
323. return false;  
324.                     }  
325. break;  
326.   
327. case 3:  
328. if (d >= 0) {  
329. // Emit the output triple and return to state 0.  
330. 6) | d;  
331. 2] = (byte) value;  
332. 1] = (byte) (value >> 8);  
333. byte) (value >> 16);  
334. 3;  
335. 0;  
336. else if (d == EQUALS) {  
337. // Emit the last (partial) output tuple;  
338. // expect no further data or padding characters.  
339. 1] = (byte) (value >> 2);  
340. byte) (value >> 10);  
341. 2;  
342. 5;  
343. else if (d != SKIP) {  
344. this.state = 6;  
345. return false;  
346.                     }  
347. break;  
348.   
349. case 4:  
350. if (d == EQUALS) {  
351.                         ++state;  
352. else if (d != SKIP) {  
353. this.state = 6;  
354. return false;  
355.                     }  
356. break;  
357.   
358. case 5:  
359. if (d != SKIP) {  
360. this.state = 6;  
361. return false;  
362.                     }  
363. break;  
364.                 }  
365.             }  
366.   
367. if (!finish) {  
368. // We're out of input, but a future call could provide  
369. // more.  
370. this.state = state;  
371. this.value = value;  
372. this.op = op;  
373. return true;  
374.             }  
375.   
376. // Done reading input.  Now figure out where we are left in  
377. // the state machine and finish up.  
378.   
379. switch (state) {  
380. case 0:  
381. // Output length is a multiple of three.  Fine.  
382. break;  
383. case 1:  
384. // Read one extra input byte, which isn't enough to  
385. // make another output byte.  Illegal.  
386. this.state = 6;  
387. return false;  
388. case 2:  
389. // Read two extra input bytes, enough to emit 1 more  
390. // output byte.  Fine.  
391. byte) (value >> 4);  
392. break;  
393. case 3:  
394. // Read three extra input bytes, enough to emit 2 more  
395. // output bytes.  Fine.  
396. byte) (value >> 10);  
397. byte) (value >> 2);  
398. break;  
399. case 4:  
400. // Read one padding '=' when we expected 2.  Illegal.  
401. this.state = 6;  
402. return false;  
403. case 5:  
404. // Read all the padding '='s we expected and no more.  
405. // Fine.  
406. break;  
407.             }  
408.   
409. this.state = state;  
410. this.op = op;  
411. return true;  
412.         }  
413.     }  
414.   
415. //  --------------------------------------------------------  
416. //  encoding  
417. //  --------------------------------------------------------  
418.   
419. /**
420.      * Base64-encode the given data and return a newly allocated
421.      * String with the result.
422.      *
423.      * @param input  the data to encode
424.      * @param flags  controls certain features of the encoded output.
425.      *               Passing {@code DEFAULT} results in output that
426.      *               adheres to RFC 2045.
427.      */  
428. public static String encodeToString(byte[] input, int flags) {  
429. try {  
430. return new String(encode(input, flags), "US-ASCII");  
431. catch (UnsupportedEncodingException e) {  
432. // US-ASCII is guaranteed to be available.  
433. throw new AssertionError(e);  
434.         }  
435.     }  
436.   
437. /**
438.      * Base64-encode the given data and return a newly allocated
439.      * String with the result.
440.      *
441.      * @param input  the data to encode
442.      * @param offset the position within the input array at which to
443.      *               start
444.      * @param len    the number of bytes of input to encode
445.      * @param flags  controls certain features of the encoded output.
446.      *               Passing {@code DEFAULT} results in output that
447.      *               adheres to RFC 2045.
448.      */  
449. public static String encodeToString(byte[] input, int offset, int len, int flags) {  
450. try {  
451. return new String(encode(input, offset, len, flags), "US-ASCII");  
452. catch (UnsupportedEncodingException e) {  
453. // US-ASCII is guaranteed to be available.  
454. throw new AssertionError(e);  
455.         }  
456.     }  
457.   
458. /**
459.      * Base64-encode the given data and return a newly allocated
460.      * byte[] with the result.
461.      *
462.      * @param input  the data to encode
463.      * @param flags  controls certain features of the encoded output.
464.      *               Passing {@code DEFAULT} results in output that
465.      *               adheres to RFC 2045.
466.      */  
467. public static byte[] encode(byte[] input, int flags) {  
468. return encode(input, 0, input.length, flags);  
469.     }  
470.   
471. /**
472.      * Base64-encode the given data and return a newly allocated
473.      * byte[] with the result.
474.      *
475.      * @param input  the data to encode
476.      * @param offset the position within the input array at which to
477.      *               start
478.      * @param len    the number of bytes of input to encode
479.      * @param flags  controls certain features of the encoded output.
480.      *               Passing {@code DEFAULT} results in output that
481.      *               adheres to RFC 2045.
482.      */  
483. public static byte[] encode(byte[] input, int offset, int len, int flags) {  
484. new Encoder(flags, null);  
485.   
486. // Compute the exact length of the array we will produce.  
487. int output_len = len / 3 * 4;  
488.   
489. // Account for the tail of the data and the padding bytes, if any.  
490. if (encoder.do_padding) {  
491. if (len % 3 > 0) {  
492. 4;  
493.             }  
494. else {  
495. switch (len % 3) {  
496. case 0: break;  
497. case 1: output_len += 2; break;  
498. case 2: output_len += 3; break;  
499.             }  
500.         }  
501.   
502. // Account for the newlines, if any.  
503. if (encoder.do_newline && len > 0) {  
504. 1) / (3 * Encoder.LINE_GROUPS)) + 1) *  
505. 2 : 1);  
506.         }  
507.   
508. new byte[output_len];  
509. true);  
510.   
511. assert encoder.op == output_len;  
512.   
513. return encoder.output;  
514.     }  
515.   
516. /* package */ static class Encoder extends Coder {  
517. /**
518.          * Emit a new line every this many output tuples.  Corresponds to
519.          * a 76-character line length (the maximum allowable according to
520.          * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>).
521.          */  
522. public static final int LINE_GROUPS = 19;  
523.   
524. /**
525.          * Lookup table for turning Base64 alphabet positions (6 bits)
526.          * into output bytes.
527.          */  
528. private static final byte ENCODE[] = {  
529. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',  
530. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',  
531. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',  
532. 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',  
533.         };  
534.   
535. /**
536.          * Lookup table for turning Base64 alphabet positions (6 bits)
537.          * into output bytes.
538.          */  
539. private static final byte ENCODE_WEBSAFE[] = {  
540. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',  
541. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',  
542. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',  
543. 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_',  
544.         };  
545.   
546. final private byte[] tail;  
547. /* package */ int tailLen;  
548. private int count;  
549.   
550. final public boolean do_padding;  
551. final public boolean do_newline;  
552. final public boolean do_cr;  
553. final private byte[] alphabet;  
554.   
555. public Encoder(int flags, byte[] output) {  
556. this.output = output;  
557.   
558. 0;  
559. 0;  
560. 0;  
561. 0) ? ENCODE : ENCODE_WEBSAFE;  
562.   
563. new byte[2];  
564. 0;  
565.   
566. 1;  
567.         }  
568.   
569. /**
570.          * @return an overestimate for the number of bytes {@code
571.          * len} bytes could encode to.
572.          */  
573. public int maxOutputSize(int len) {  
574. return len * 8/5 + 10;  
575.         }  
576.   
577. public boolean process(byte[] input, int offset, int len, boolean finish) {  
578. // Using local variables makes the encoder about 9% faster.  
579. final byte[] alphabet = this.alphabet;  
580. final byte[] output = this.output;  
581. int op = 0;  
582. int count = this.count;  
583.   
584. int p = offset;  
585.             len += offset;  
586. int v = -1;  
587.   
588. // First we need to concatenate the tail of the previous call  
589. // with any input bytes available now and see if we can empty  
590. // the tail.  
591.   
592. switch (tailLen) {  
593. case 0:  
594. // There was no tail.  
595. break;  
596.   
597. case 1:  
598. if (p+2 <= len) {  
599. // A 1-byte tail with at least 2 bytes of  
600. // input available now.  
601. 0] & 0xff) << 16) |  
602. 0xff) << 8) |  
603. 0xff);  
604. 0;  
605.                     };  
606. break;  
607.   
608. case 2:  
609. if (p+1 <= len) {  
610. // A 2-byte tail with at least 1 byte of input.  
611. 0] & 0xff) << 16) |  
612. 1] & 0xff) << 8) |  
613. 0xff);  
614. 0;  
615.                     }  
616. break;  
617.             }  
618.   
619. if (v != -1) {  
620. 18) & 0x3f];  
621. 12) & 0x3f];  
622. 6) & 0x3f];  
623. 0x3f];  
624. if (--count == 0) {  
625. if (do_cr) output[op++] = '\r';  
626. '\n';  
627.                     count = LINE_GROUPS;  
628.                 }  
629.             }  
630.   
631. // At this point either there is no tail, or there are fewer  
632. // than 3 bytes of input available.  
633.   
634. // The main loop, turning 3 input bytes into 4 output bytes on  
635. // each iteration.  
636. while (p+3 <= len) {  
637. 0xff) << 16) |  
638. 1] & 0xff) << 8) |  
639. 2] & 0xff);  
640. 18) & 0x3f];  
641. 1] = alphabet[(v >> 12) & 0x3f];  
642. 2] = alphabet[(v >> 6) & 0x3f];  
643. 3] = alphabet[v & 0x3f];  
644. 3;  
645. 4;  
646. if (--count == 0) {  
647. if (do_cr) output[op++] = '\r';  
648. '\n';  
649.                     count = LINE_GROUPS;  
650.                 }  
651.             }  
652.   
653. if (finish) {  
654. // Finish up the tail of the input.  Note that we need to  
655. // consume any bytes in tail before any bytes  
656. // remaining in input; there should be at most two bytes  
657. // total.  
658.   
659. if (p-tailLen == len-1) {  
660. int t = 0;  
661. 0 ? tail[t++] : input[p++]) & 0xff) << 4;  
662.                     tailLen -= t;  
663. 6) & 0x3f];  
664. 0x3f];  
665. if (do_padding) {  
666. '=';  
667. '=';  
668.                     }  
669. if (do_newline) {  
670. if (do_cr) output[op++] = '\r';  
671. '\n';  
672.                     }  
673. else if (p-tailLen == len-2) {  
674. int t = 0;  
675. 1 ? tail[t++] : input[p++]) & 0xff) << 10) |  
676. 0 ? tail[t++] : input[p++]) & 0xff) << 2);  
677.                     tailLen -= t;  
678. 12) & 0x3f];  
679. 6) & 0x3f];  
680. 0x3f];  
681. if (do_padding) {  
682. '=';  
683.                     }  
684. if (do_newline) {  
685. if (do_cr) output[op++] = '\r';  
686. '\n';  
687.                     }  
688. else if (do_newline && op > 0 && count != LINE_GROUPS) {  
689. if (do_cr) output[op++] = '\r';  
690. '\n';  
691.                 }  
692.   
693. assert tailLen == 0;  
694. assert p == len;  
695. else {  
696. // Save the leftovers in tail to be consumed on the next  
697. // call to encodeInternal.  
698.   
699. if (p == len-1) {  
700.                     tail[tailLen++] = input[p];  
701. else if (p == len-2) {  
702.                     tail[tailLen++] = input[p];  
703. 1];  
704.                 }  
705.             }  
706.   
707. this.op = op;  
708. this.count = count;  
709.   
710. return true;  
711.         }  
712.     }  
713.   
714. private Base64() { }   // don't instantiate  
715. }





1. /**
2.  * bitmap转为base64
3.  * @param bitmap
4.  * @return
5.  */  
6. public static String bitmapToBase64(Bitmap bitmap) {  
7.   
8. null;  
9. null;  
10. try {  
11. if (bitmap != null) {  
12. new ByteArrayOutputStream();  
13. 100, baos);  
14.   
15.             baos.flush();  
16.             baos.close();  
17.   
18. byte[] bitmapBytes = baos.toByteArray();  
19.             result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);  
20.         }  
21. catch (IOException e) {  
22.         e.printStackTrace();  
23. finally {  
24. try {  
25. if (baos != null) {  
26.                 baos.flush();  
27.                 baos.close();  
28.             }  
29. catch (IOException e) {  
30.             e.printStackTrace();  
31.         }  
32.     }  
33. return result;  
34. }  
35.   
36. /**
37.  * base64转为bitmap
38.  * @param base64Data
39.  * @return
40.  */  
41. public static Bitmap base64ToBitmap(String base64Data) {  
42. byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);  
43. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);  
44. }