View Javadoc

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