1 16 package com.sun.slamd.example; 17 18 19 20 import java.nio.*; 21 22 23 24 34 public class Base64Encoder 35 { 36 39 public static final char[] BASE64_ALPHABET = 40 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + 41 "0123456789+/").toCharArray(); 42 43 44 45 52 public static String encode(byte[] byteArray) 53 { 54 StringBuffer buffer = new StringBuffer (4 * byteArray.length / 3); 55 56 int i, j; 57 int iterations = byteArray.length / 3; 58 for (i=0,j=0; j < iterations; j++, i += 3) 59 { 60 int value = ((byteArray[i] & 0x000000FF) << 16) | 61 ((byteArray[i+1] & 0x000000FF) << 8) | 62 (byteArray[i+2] & 0x000000FF); 63 64 buffer.append(BASE64_ALPHABET[(value >>> 18) & 0x0000003F]); 65 buffer.append(BASE64_ALPHABET[(value >>> 12) & 0x0000003F]); 66 buffer.append(BASE64_ALPHABET[(value >>> 6) & 0x0000003F]); 67 buffer.append(BASE64_ALPHABET[value & 0x0000003F]); 68 } 69 70 switch (byteArray.length % 3) 71 { 72 case 1: 73 buffer.append(BASE64_ALPHABET[(byteArray[i] >>> 2) & 0x0000003F]); 74 buffer.append(BASE64_ALPHABET[(byteArray[i] << 4) & 0x0000003F]); 75 buffer.append("=="); 76 break; 77 case 2: 78 int value = ((byteArray[i] & 0x000000FF) << 8) | 79 (byteArray[i+1] & 0x000000FF); 80 81 buffer.append(BASE64_ALPHABET[(value >>> 10) & 0x0000003F]); 82 buffer.append(BASE64_ALPHABET[(value >>> 4) & 0x0000003F]); 83 buffer.append(BASE64_ALPHABET[(value << 2) & 0x0000003F]); 84 buffer.append("="); 85 break; 86 } 87 88 return buffer.toString(); 89 } 90 91 92 93 100 public static byte[] decode(String encodedValue) 101 { 102 int length = encodedValue.length(); 104 if ((length % 4) != 0) 105 { 106 return null; 107 } 108 109 char[] encodedChars = encodedValue.toCharArray(); 110 111 ByteBuffer buffer = ByteBuffer.allocate(length); 112 113 for (int i=0; i < length; i += 4) 114 { 115 boolean append = true; 116 int value = 0; 117 for (int j=0; j < 4; j++) 118 { 119 switch (encodedChars[i+j]) 120 { 121 case 'A': 122 value <<= 6; 123 break; 124 case 'B': 125 value = (value << 6) | 0x00000001; 126 break; 127 case 'C': 128 value = (value << 6) | 0x00000002; 129 break; 130 case 'D': 131 value = (value << 6) | 0x00000003; 132 break; 133 case 'E': 134 value = (value << 6) | 0x00000004; 135 break; 136 case 'F': 137 value = (value << 6) | 0x00000005; 138 break; 139 case 'G': 140 value = (value << 6) | 0x00000006; 141 break; 142 case 'H': 143 value = (value << 6) | 0x00000007; 144 break; 145 case 'I': 146 value = (value << 6) | 0x00000008; 147 break; 148 case 'J': 149 value = (value << 6) | 0x00000009; 150 break; 151 case 'K': 152 value = (value << 6) | 0x0000000A; 153 break; 154 case 'L': 155 value = (value << 6) | 0x0000000B; 156 break; 157 case 'M': 158 value = (value << 6) | 0x0000000C; 159 break; 160 case 'N': 161 value = (value << 6) | 0x0000000D; 162 break; 163 case 'O': 164 value = (value << 6) | 0x0000000E; 165 break; 166 case 'P': 167 value = (value << 6) | 0x0000000F; 168 break; 169 case 'Q': 170 value = (value << 6) | 0x00000010; 171 break; 172 case 'R': 173 value = (value << 6) | 0x00000011; 174 break; 175 case 'S': 176 value = (value << 6) | 0x00000012; 177 break; 178 case 'T': 179 value = (value << 6) | 0x00000013; 180 break; 181 case 'U': 182 value = (value << 6) | 0x00000014; 183 break; 184 case 'V': 185 value = (value << 6) | 0x00000015; 186 break; 187 case 'W': 188 value = (value << 6) | 0x00000016; 189 break; 190 case 'X': 191 value = (value << 6) | 0x00000017; 192 break; 193 case 'Y': 194 value = (value << 6) | 0x00000018; 195 break; 196 case 'Z': 197 value = (value << 6) | 0x00000019; 198 break; 199 case 'a': 200 value = (value << 6) | 0x0000001A; 201 break; 202 case 'b': 203 value = (value << 6) | 0x0000001B; 204 break; 205 case 'c': 206 value = (value << 6) | 0x0000001C; 207 break; 208 case 'd': 209 value = (value << 6) | 0x0000001D; 210 break; 211 case 'e': 212 value = (value << 6) | 0x0000001E; 213 break; 214 case 'f': 215 value = (value << 6) | 0x0000001F; 216 break; 217 case 'g': 218 value = (value << 6) | 0x00000020; 219 break; 220 case 'h': 221 value = (value << 6) | 0x00000021; 222 break; 223 case 'i': 224 value = (value << 6) | 0x00000022; 225 break; 226 case 'j': 227 value = (value << 6) | 0x00000023; 228 break; 229 case 'k': 230 value = (value << 6) | 0x00000024; 231 break; 232 case 'l': 233 value = (value << 6) | 0x00000025; 234 break; 235 case 'm': 236 value = (value << 6) | 0x00000026; 237 break; 238 case 'n': 239 value = (value << 6) | 0x00000027; 240 break; 241 case 'o': 242 value = (value << 6) | 0x00000028; 243 break; 244 case 'p': 245 value = (value << 6) | 0x00000029; 246 break; 247 case 'q': 248 value = (value << 6) | 0x0000002A; 249 break; 250 case 'r': 251 value = (value << 6) | 0x0000002B; 252 break; 253 case 's': 254 value = (value << 6) | 0x0000002C; 255 break; 256 case 't': 257 value = (value << 6) | 0x0000002D; 258 break; 259 case 'u': 260 value = (value << 6) | 0x0000002E; 261 break; 262 case 'v': 263 value = (value << 6) | 0x0000002F; 264 break; 265 case 'w': 266 value = (value << 6) | 0x00000030; 267 break; 268 case 'x': 269 value = (value << 6) | 0x00000031; 270 break; 271 case 'y': 272 value = (value << 6) | 0x00000032; 273 break; 274 case 'z': 275 value = (value << 6) | 0x00000033; 276 break; 277 case '0': 278 value = (value << 6) | 0x00000034; 279 break; 280 case '1': 281 value = (value << 6) | 0x00000035; 282 break; 283 case '2': 284 value = (value << 6) | 0x00000036; 285 break; 286 case '3': 287 value = (value << 6) | 0x00000037; 288 break; 289 case '4': 290 value = (value << 6) | 0x00000038; 291 break; 292 case '5': 293 value = (value << 6) | 0x00000039; 294 break; 295 case '6': 296 value = (value << 6) | 0x0000003A; 297 break; 298 case '7': 299 value = (value << 6) | 0x0000003B; 300 break; 301 case '8': 302 value = (value << 6) | 0x0000003C; 303 break; 304 case '9': 305 value = (value << 6) | 0x0000003D; 306 break; 307 case '+': 308 value = (value << 6) | 0x0000003E; 309 break; 310 case '/': 311 value = (value << 6) | 0x0000003F; 312 break; 313 case '=': 314 append = false; 315 switch (j) 316 { 317 case 2: 318 buffer.put((byte) ((value >>> 4) & 0x000000FF)); 319 break; 320 case 3: 321 buffer.put((byte) ((value >>> 10) & 0x000000FF)); 322 buffer.put((byte) ((value >>> 2) & 0x000000FF)); 323 break; 324 } 325 break; 326 default: 327 return null; 328 } 329 330 if (! append) 331 { 332 break; 333 } 334 } 335 336 if (append) 337 { 338 buffer.put((byte) ((value >>> 16) & 0x000000FF)); 339 buffer.put((byte) ((value >>> 8) & 0x000000FF)); 340 buffer.put((byte) (value & 0x000000FF)); 341 } 342 else 343 { 344 break; 345 } 346 } 347 348 buffer.flip(); 349 byte[] returnArray = new byte[buffer.limit()]; 350 buffer.get(returnArray); 351 return returnArray; 352 } 353 } 354 355 | Popular Tags |