Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1905 chandransh 1
package in.shop2020.serving.utils.ebs;
2
 
3
import java.io.ByteArrayOutputStream;
4
 
5
/**
6
 * This class contains two static methods for Base64 encoding and decoding.
7
 *
8
 * @author <a href="http://izhuk.com">Igor Zhukovsky</a>
9
 */
10
public class Base64 {
11
 
12
	final static String base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
13
 
14
	/**
15
	 * Decodes BASE64 encoded string.
16
	 * 
17
	 * @param encoded
18
	 *            The BASE64 encoded string to be decoded
19
	 * @return decoded data as byte array
20
	 */
21
	public byte[] decode(String encoded) {
22
		int i;
23
		byte output[] = new byte[3];
24
		int state;
25
 
26
		ByteArrayOutputStream data = new ByteArrayOutputStream(encoded.length());
27
 
28
		state = 1;
29
		for (i = 0; i < encoded.length(); i++) {
30
			byte c;
31
			{
32
				char alpha = encoded.charAt(i);
33
				if (Character.isWhitespace(alpha))
34
					continue;
35
 
36
				if ((alpha >= 'A') && (alpha <= 'Z'))
37
					c = (byte) (alpha - 'A');
38
				else if ((alpha >= 'a') && (alpha <= 'z'))
39
					c = (byte) (26 + (alpha - 'a'));
40
				else if ((alpha >= '0') && (alpha <= '9'))
41
					c = (byte) (52 + (alpha - '0'));
42
				else if (alpha == '+')
43
					c = 62;
44
				else if (alpha == '/')
45
					c = 63;
46
				else if (alpha == '=')
47
					break; // end
48
				else
49
					return null; // error
50
			}
51
 
52
			switch (state) {
53
			case 1:
54
				output[0] = (byte) (c << 2);
55
				break;
56
			case 2:
57
				output[0] |= (byte) (c >>> 4);
58
				output[1] = (byte) ((c & 0x0F) << 4);
59
				break;
60
			case 3:
61
				output[1] |= (byte) (c >>> 2);
62
				output[2] = (byte) ((c & 0x03) << 6);
63
				break;
64
			case 4:
65
				output[2] |= c;
66
				data.write(output, 0, output.length);
67
				break;
68
			}
69
			state = (state < 4 ? state + 1 : 1);
70
		} // for
71
 
72
		if (i < encoded.length()) /* then '=' found, but the end of string */
73
			switch (state) {
74
			case 3:
75
				data.write(output, 0, 1);
76
				return (encoded.charAt(i) == '=')
77
						&& (encoded.charAt(i + 1) == '=') ? data.toByteArray()
78
						: null;
79
			case 4:
80
				data.write(output, 0, 2);
81
				return (encoded.charAt(i) == '=') ? data.toByteArray() : null;
82
			default:
83
				return null;
84
			}
85
		else
86
			return (state == 1 ? data.toByteArray() : null); /* end of string */
87
 
88
	} // decode
89
 
90
	/**
91
	 * Encodes binary data by BASE64 method.
92
	 * 
93
	 * @param data
94
	 *            binary data as byte array
95
	 * @return encoded data as String
96
	 */
97
	public String encode(byte[] data) {
98
		char output[] = new char[4];
99
		int state = 1;
100
		int restbits = 0;
101
		int chunks = 0;
102
 
103
		StringBuffer encoded = new StringBuffer();
104
 
105
		for (int i = 0; i < data.length; i++) {
106
			int ic = (data[i] >= 0 ? data[i] : (data[i] & 0x7F) + 128);
107
			switch (state) {
108
			case 1:
109
				output[0] = base64.charAt(ic >>> 2);
110
				restbits = ic & 0x03;
111
				break;
112
			case 2:
113
				output[1] = base64.charAt((restbits << 4) | (ic >>> 4));
114
				restbits = ic & 0x0F;
115
				break;
116
			case 3:
117
				output[2] = base64.charAt((restbits << 2) | (ic >>> 6));
118
				output[3] = base64.charAt(ic & 0x3F);
119
				encoded.append(output);
120
 
121
				// keep no more than 76 character per line
122
				chunks++;
123
				if ((chunks % 19) == 0)
124
					encoded.append("\r\n");
125
				break;
126
			}
127
			state = (state < 3 ? state + 1 : 1);
128
		} // for
129
 
130
		/* final */
131
		switch (state) {
132
		case 2:
133
			output[1] = base64.charAt((restbits << 4));
134
			output[2] = output[3] = '=';
135
			encoded.append(output);
136
			break;
137
		case 3:
138
			output[2] = base64.charAt((restbits << 2));
139
			output[3] = '=';
140
			encoded.append(output);
141
			break;
142
		}
143
 
144
		return encoded.toString();
145
	}
146
}