View Javadoc

1   package org.itracker.services.util;
2   /**
3   * A Base64 Encoder/Decoder.
4   *
5   * <p>
6   * This class is used to encode and decode data in Base64 format as described in RFC 1521.
7   *
8   * <p>
9   * This is "Open Source" software and released under the <a href="http://www.gnu.org/licenses/lgpl.html">GNU/LGPL</a> license.<br>
10  * It is provided "as is" without warranty of any kind.<br>
11  * Copyright 2003: Christian d'Heureuse, Inventec Informatik AG, Switzerland.<br>
12  * Home page: <a href="http://www.source-code.biz">www.source-code.biz</a><br>
13  *
14  * <p>
15  * Version history:<br>
16  * 2003-07-22 Christian d'Heureuse (chdh): Module created.<br>
17  * 2005-08-11 chdh: Lincense changed from GPL to LGPL.<br>
18  * 2006-11-21 chdh:<br>
19  *  &nbsp; Method encode(String) renamed to encodeString(String).<br>
20  *  &nbsp; Method decode(String) renamed to decodeString(String).<br>
21  *  &nbsp; New method encode(byte[],int) added.<br>
22  *  &nbsp; New method decode(String) added.<br>
23  */
24  
25  public class Base64Coder {
26  
27  // Mapping table from 6-bit nibbles to Base64 characters.
28  private static char[]    map1 = new char[64];
29     static {
30        int i=0;
31        for (char c='A'; c<='Z'; c++) map1[i++] = c;
32        for (char c='a'; c<='z'; c++) map1[i++] = c;
33        for (char c='0'; c<='9'; c++) map1[i++] = c;
34        map1[i++] = '+'; map1[i++] = '/'; }
35  
36  // Mapping table from Base64 characters to 6-bit nibbles.
37  private static byte[]    map2 = new byte[128];
38     static {
39        for (int i=0; i<map2.length; i++) map2[i] = -1;
40        for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
41  
42  /**
43  * Encodes a string into Base64 format.
44  * No blanks or line breaks are inserted.
45  * @param s  a String to be encoded.
46  * @return   A String with the Base64 encoded data.
47  */
48  public static String encodeString (String s) {
49     return new String(encode(s.getBytes())); }
50  
51  /**
52  * Encodes a byte array into Base64 format.
53  * No blanks or line breaks are inserted.
54  * @param in  an array containing the data bytes to be encoded.
55  * @return    A character array with the Base64 encoded data.
56  */
57  public static char[] encode (byte[] in) {
58     return encode(in,in.length); }
59  
60  /**
61  * Encodes a byte array into Base64 format.
62  * No blanks or line breaks are inserted.
63  * @param in   an array containing the data bytes to be encoded.
64  * @param iLen number of bytes to process in <code>in</code>.
65  * @return     A character array with the Base64 encoded data.
66  */
67  public static char[] encode (byte[] in, int iLen) {
68     int oDataLen = (iLen*4+2)/3;       // output length without padding
69     int oLen = ((iLen+2)/3)*4;         // output length including padding
70     char[] out = new char[oLen];
71     int ip = 0;
72     int op = 0;
73     while (ip < iLen) {
74        int i0 = in[ip++] & 0xff;
75        int i1 = ip < iLen ? in[ip++] & 0xff : 0;
76        int i2 = ip < iLen ? in[ip++] & 0xff : 0;
77        int o0 = i0 >>> 2;
78        int o1 = ((i0 &   3) << 4) | (i1 >>> 4);
79        int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
80        int o3 = i2 & 0x3F;
81        out[op++] = map1[o0];
82        out[op++] = map1[o1];
83        out[op] = op < oDataLen ? map1[o2] : '='; op++;
84        out[op] = op < oDataLen ? map1[o3] : '='; op++; }
85     return out; }
86  
87  /**
88  * Decodes a string from Base64 format.
89  * @param s  a Base64 String to be decoded.
90  * @return   A String containing the decoded data.
91  * @throws   IllegalArgumentException if the input is not valid Base64 encoded data.
92  */
93  public static String decodeString (String s) {
94     return new String(decode(s)); }
95  
96  /**
97  * Decodes a byte array from Base64 format.
98  * @param s  a Base64 String to be decoded.
99  * @return   An array containing the decoded data bytes.
100 * @throws   IllegalArgumentException if the input is not valid Base64 encoded data.
101 */
102 public static byte[] decode (String s) {
103    return decode(s.toCharArray()); }
104 
105 /**
106 * Decodes a byte array from Base64 format.
107 * No blanks or line breaks are allowed within the Base64 encoded data.
108 * @param in  a character array containing the Base64 encoded data.
109 * @return    An array containing the decoded data bytes.
110 * @throws    IllegalArgumentException if the input is not valid Base64 encoded data.
111 */
112 public static byte[] decode (char[] in) {
113    int iLen = in.length;
114    if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
115    while (iLen > 0 && in[iLen-1] == '=') iLen--;
116    int oLen = (iLen*3) / 4;
117    byte[] out = new byte[oLen];
118    int ip = 0;
119    int op = 0;
120    while (ip < iLen) {
121       int i0 = in[ip++];
122       int i1 = in[ip++];
123       int i2 = ip < iLen ? in[ip++] : 'A';
124       int i3 = ip < iLen ? in[ip++] : 'A';
125       if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
126          throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
127       int b0 = map2[i0];
128       int b1 = map2[i1];
129       int b2 = map2[i2];
130       int b3 = map2[i3];
131       if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
132          throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
133       int o0 = ( b0       <<2) | (b1>>>4);
134       int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
135       int o2 = ((b2 &   3)<<6) |  b3;
136       out[op++] = (byte)o0;
137       if (op<oLen) out[op++] = (byte)o1;
138       if (op<oLen) out[op++] = (byte)o2; }
139    return out; }
140 
141 // Dummy constructor.
142 private Base64Coder() {}
143 
144 } // end class Base64Coder