1
2
3
4
5
6
7
8 package org.asteriskjava.util;
9
10
11
12
13
14
15
16
17 public class Base64 {
18
19
20
21
22 public static String byteArrayToBase64(byte[] a) {
23 return byteArrayToBase64(a, false);
24 }
25
26
27
28
29
30
31
32 public static String byteArrayToAltBase64(byte[] a) {
33 return byteArrayToBase64(a, true);
34 }
35
36 private static String byteArrayToBase64(byte[] a, boolean alternate) {
37 int aLen = a.length;
38 int numFullGroups = aLen/3;
39 int numBytesInPartialGroup = aLen - 3*numFullGroups;
40 int resultLen = 4*((aLen + 2)/3);
41 StringBuffer result = new StringBuffer(resultLen);
42 char[] intToAlpha = (alternate ? intToAltBase64 : intToBase64);
43
44
45 int inCursor = 0;
46 for (int i=0; i<numFullGroups; i++) {
47 int byte0 = a[inCursor++] & 0xff;
48 int byte1 = a[inCursor++] & 0xff;
49 int byte2 = a[inCursor++] & 0xff;
50 result.append(intToAlpha[byte0 >> 2]);
51 result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
52 result.append(intToAlpha[(byte1 << 2)&0x3f | (byte2 >> 6)]);
53 result.append(intToAlpha[byte2 & 0x3f]);
54 }
55
56
57 if (numBytesInPartialGroup != 0) {
58 int byte0 = a[inCursor++] & 0xff;
59 result.append(intToAlpha[byte0 >> 2]);
60 if (numBytesInPartialGroup == 1) {
61 result.append(intToAlpha[(byte0 << 4) & 0x3f]);
62 result.append("==");
63 } else {
64
65 int byte1 = a[inCursor++] & 0xff;
66 result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
67 result.append(intToAlpha[(byte1 << 2)&0x3f]);
68 result.append('=');
69 }
70 }
71
72
73 return result.toString();
74 }
75
76
77
78
79
80
81 private static final char intToBase64[] = {
82 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
83 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
84 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
85 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
86 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
87 };
88
89
90
91
92
93
94
95
96 private static final char intToAltBase64[] = {
97 '!', '"', '#', '$', '%', '&', '\'', '(', ')', ',', '-', '.', ':',
98 ';', '<', '>', '@', '[', ']', '^', '`', '_', '{', '|', '}', '~',
99 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
100 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
101 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '?'
102 };
103
104
105
106
107
108
109
110
111 public static byte[] base64ToByteArray(String s) {
112 return base64ToByteArray(s, false);
113 }
114
115
116
117
118
119
120
121
122
123 public static byte[] altBase64ToByteArray(String s) {
124 return base64ToByteArray(s, true);
125 }
126
127 private static byte[] base64ToByteArray(String s, boolean alternate) {
128 byte[] alphaToInt = (alternate ? altBase64ToInt : base64ToInt);
129 int sLen = s.length();
130 int numGroups = sLen/4;
131 if (4*numGroups != sLen)
132 throw new IllegalArgumentException(
133 "String length must be a multiple of four.");
134 int missingBytesInLastGroup = 0;
135 int numFullGroups = numGroups;
136 if (sLen != 0) {
137 if (s.charAt(sLen-1) == '=') {
138 missingBytesInLastGroup++;
139 numFullGroups--;
140 }
141 if (s.charAt(sLen-2) == '=')
142 missingBytesInLastGroup++;
143 }
144 byte[] result = new byte[3*numGroups - missingBytesInLastGroup];
145
146
147 int inCursor = 0, outCursor = 0;
148 for (int i=0; i<numFullGroups; i++) {
149 int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
150 int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
151 int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
152 int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt);
153 result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
154 result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
155 result[outCursor++] = (byte) ((ch2 << 6) | ch3);
156 }
157
158
159 if (missingBytesInLastGroup != 0) {
160 int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
161 int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
162 result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
163
164 if (missingBytesInLastGroup == 1) {
165 int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
166 result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
167 }
168 }
169
170
171 return result;
172 }
173
174
175
176
177
178
179
180
181 private static int base64toInt(char c, byte[] alphaToInt) {
182 int result = alphaToInt[c];
183 if (result < 0)
184 throw new IllegalArgumentException("Illegal character " + c);
185 return result;
186 }
187
188
189
190
191
192
193
194
195 private static final byte base64ToInt[] = {
196 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
197 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
198 -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
199 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
200 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
201 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
202 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
203 };
204
205
206
207
208
209 private static final byte altBase64ToInt[] = {
210 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
211 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
212 2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10, 11, -1 , 52, 53, 54, 55, 56, 57,
213 58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1, -1, -1, -1, -1,
214 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
215 -1, -1, -1, 17, -1, 18, 19, 21, 20, 26, 27, 28, 29, 30, 31, 32, 33,
216 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
217 51, 22, 23, 24, 25
218 };
219 }