001    package com.hs.mail.util;
002    
003    import java.io.ByteArrayOutputStream;
004    import java.io.IOException;
005    
006    public class BASE64 {
007    
008            private BASE64() {
009            }
010    
011            public static String encode(byte b[]) {
012                    StringBuffer sb = new StringBuffer((b.length / 3 + 1) * 4);
013                    int i = 0;
014                    int n = b.length;
015                    do {
016                            if (i >= n)
017                                    break;
018                            sb.append(ALPHA[(b[i] & 0xff) >>> 2]);
019                            if (++i < n) {
020                                    sb.append(ALPHA[(b[i - 1] & 3) << 4 | (b[i] & 0xff) >>> 4]);
021                            } else {
022                                    sb.append(ALPHA[(b[i - 1] & 3) << 4]);
023                                    sb.append("==");
024                                    break;
025                            }
026                            if (++i < n) {
027                                    sb.append(ALPHA[(b[i - 1] & 0xf) << 2 | (b[i] & 0xff) >>> 6]);
028                                    sb.append(ALPHA[b[i] & 0x3f]);
029                            } else {
030                                    sb.append(ALPHA[(b[i - 1] & 0xf) << 2]);
031                                    sb.append('=');
032                                    break;
033                            }
034                            i++;
035                    } while (true);
036                    return sb.toString();
037            }
038    
039            public static byte[] decode(String s) throws IOException {
040                    ByteArrayOutputStream out = new ByteArrayOutputStream(
041                                    (s.length() / 4) * 3);
042                    int cb[] = new int[4];
043                    int i = 0;
044                    int n = s.length();
045                    do {
046                            if (i >= n)
047                                    break;
048                            int j = 0;
049                            do {
050                                    if (j >= 4 || i >= n)
051                                            break;
052                                    int c = INDEX[s.charAt(i++)];
053                                    if (c != -1)
054                                            cb[j++] = c;
055                            } while (true);
056                            if (j < 4 && i < n)
057                                    throw new IOException("Character buffer underflow");
058                            out.write(cb[0] << 2 | (cb[1] & 0x3f) >>> 4);
059                            if (j > 2)
060                                    out.write((cb[1] & 0xf) << 4 | cb[2] >>> 2);
061                            if (j > 3)
062                                    out.write((cb[2] & 3) << 6 | cb[3]);
063                    } while (true);
064                    return out.toByteArray();
065            }
066    
067        private static final char ALPHA[] = {
068            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
069            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
070            'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 
071            'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
072            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
073            'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', 
074            '8', '9', '+', '/'
075        };
076     
077            private static final int INDEX[];
078    
079            static {
080                    INDEX = new int[256];
081                    for (int i = 0; i < 256; i++)
082                            INDEX[i] = -1;
083    
084                    for (int i = 0; i < ALPHA.length; i++)
085                            INDEX[ALPHA[i]] = i;
086            }
087    
088    }