1 55 package org.jboss.axis.encoding; 56 57 import org.jboss.axis.utils.JavaUtils; 58 import org.jboss.axis.utils.Messages; 59 60 import java.io.IOException ; 61 import java.io.OutputStream ; 62 import java.io.Writer ; 63 64 67 public class Base64 68 { 69 private static final char[] S_BASE64CHAR = { 70 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 71 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 72 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 73 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 74 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 75 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', 76 '8', '9', '+', '/' 77 }; 78 private static final char S_BASE64PAD = '='; 79 private static final byte[] S_DECODETABLE = new byte[128]; 80 81 static 82 { 83 for (int i = 0; i < S_DECODETABLE.length; i++) 84 S_DECODETABLE[i] = Byte.MAX_VALUE; for (int i = 0; i < S_BASE64CHAR.length; i++) S_DECODETABLE[S_BASE64CHAR[i]] = (byte)i; 87 } 88 89 private static int decode0(char[] ibuf, byte[] obuf, int wp) 90 { 91 int outlen = 3; 92 if (ibuf[3] == S_BASE64PAD) outlen = 2; 93 if (ibuf[2] == S_BASE64PAD) outlen = 1; 94 int b0 = S_DECODETABLE[ibuf[0]]; 95 int b1 = S_DECODETABLE[ibuf[1]]; 96 int b2 = S_DECODETABLE[ibuf[2]]; 97 int b3 = S_DECODETABLE[ibuf[3]]; 98 switch (outlen) 99 { 100 case 1: 101 obuf[wp] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3); 102 return 1; 103 case 2: 104 obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3); 105 obuf[wp] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf); 106 return 2; 107 case 3: 108 obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3); 109 obuf[wp++] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf); 110 obuf[wp] = (byte)(b2 << 6 & 0xc0 | b3 & 0x3f); 111 return 3; 112 default: 113 throw new RuntimeException (Messages.getMessage("internalError00")); 114 } 115 } 116 117 120 public static byte[] decode(char[] data, int off, int len) 121 { 122 char[] ibuf = new char[4]; 123 int ibufcount = 0; 124 byte[] obuf = new byte[len / 4 * 3 + 3]; 125 int obufcount = 0; 126 for (int i = off; i < off + len; i++) 127 { 128 char ch = data[i]; 129 if (ch == S_BASE64PAD 130 || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) 131 { 132 ibuf[ibufcount++] = ch; 133 if (ibufcount == ibuf.length) 134 { 135 ibufcount = 0; 136 obufcount += decode0(ibuf, obuf, obufcount); 137 } 138 } 139 } 140 if (obufcount == obuf.length) 141 return obuf; 142 byte[] ret = new byte[obufcount]; 143 System.arraycopy(obuf, 0, ret, 0, obufcount); 144 return ret; 145 } 146 147 150 public static byte[] decode(String data) 151 { 152 char[] ibuf = new char[4]; 153 int ibufcount = 0; 154 byte[] obuf = new byte[data.length() / 4 * 3 + 3]; 155 int obufcount = 0; 156 for (int i = 0; i < data.length(); i++) 157 { 158 char ch = data.charAt(i); 159 if (ch == S_BASE64PAD 160 || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) 161 { 162 ibuf[ibufcount++] = ch; 163 if (ibufcount == ibuf.length) 164 { 165 ibufcount = 0; 166 obufcount += decode0(ibuf, obuf, obufcount); 167 } 168 } 169 } 170 if (obufcount == obuf.length) 171 return obuf; 172 byte[] ret = new byte[obufcount]; 173 System.arraycopy(obuf, 0, ret, 0, obufcount); 174 return ret; 175 } 176 177 180 public static void decode(char[] data, int off, int len, OutputStream ostream) throws IOException 181 { 182 char[] ibuf = new char[4]; 183 int ibufcount = 0; 184 byte[] obuf = new byte[3]; 185 for (int i = off; i < off + len; i++) 186 { 187 char ch = data[i]; 188 if (ch == S_BASE64PAD 189 || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) 190 { 191 ibuf[ibufcount++] = ch; 192 if (ibufcount == ibuf.length) 193 { 194 ibufcount = 0; 195 int obufcount = decode0(ibuf, obuf, 0); 196 ostream.write(obuf, 0, obufcount); 197 } 198 } 199 } 200 } 201 202 205 public static void decode(String data, OutputStream ostream) throws IOException 206 { 207 char[] ibuf = new char[4]; 208 int ibufcount = 0; 209 byte[] obuf = new byte[3]; 210 for (int i = 0; i < data.length(); i++) 211 { 212 char ch = data.charAt(i); 213 if (ch == S_BASE64PAD 214 || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) 215 { 216 ibuf[ibufcount++] = ch; 217 if (ibufcount == ibuf.length) 218 { 219 ibufcount = 0; 220 int obufcount = decode0(ibuf, obuf, 0); 221 ostream.write(obuf, 0, obufcount); 222 } 223 } 224 } 225 } 226 227 230 public static String encode(byte[] data) 231 { 232 return encode(data, 0, data.length); 233 } 234 235 238 public static String encode(byte[] data, int off, int len) 239 { 240 if (len <= 0) return ""; 241 char[] out = new char[len / 3 * 4 + 4]; 242 int rindex = off; 243 int windex = 0; 244 int rest = len - off; 245 while (rest >= 3) 246 { 247 int i = ((data[rindex] & 0xff) << 16) 248 + ((data[rindex + 1] & 0xff) << 8) 249 + (data[rindex + 2] & 0xff); 250 out[windex++] = S_BASE64CHAR[i >> 18]; 251 out[windex++] = S_BASE64CHAR[(i >> 12) & 0x3f]; 252 out[windex++] = S_BASE64CHAR[(i >> 6) & 0x3f]; 253 out[windex++] = S_BASE64CHAR[i & 0x3f]; 254 rindex += 3; 255 rest -= 3; 256 } 257 if (rest == 1) 258 { 259 int i = data[rindex] & 0xff; 260 out[windex++] = S_BASE64CHAR[i >> 2]; 261 out[windex++] = S_BASE64CHAR[(i << 4) & 0x3f]; 262 out[windex++] = S_BASE64PAD; 263 out[windex++] = S_BASE64PAD; 264 } 265 else if (rest == 2) 266 { 267 int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff); 268 out[windex++] = S_BASE64CHAR[i >> 10]; 269 out[windex++] = S_BASE64CHAR[(i >> 4) & 0x3f]; 270 out[windex++] = S_BASE64CHAR[(i << 2) & 0x3f]; 271 out[windex++] = S_BASE64PAD; 272 } 273 return new String (out, 0, windex); 274 } 275 276 279 public static void encode(byte[] data, int off, int len, OutputStream ostream) throws IOException 280 { 281 if (len <= 0) return; 282 byte[] out = new byte[4]; 283 int rindex = off; 284 int rest = len - off; 285 while (rest >= 3) 286 { 287 int i = ((data[rindex] & 0xff) << 16) 288 + ((data[rindex + 1] & 0xff) << 8) 289 + (data[rindex + 2] & 0xff); 290 out[0] = (byte)S_BASE64CHAR[i >> 18]; 291 out[1] = (byte)S_BASE64CHAR[(i >> 12) & 0x3f]; 292 out[2] = (byte)S_BASE64CHAR[(i >> 6) & 0x3f]; 293 out[3] = (byte)S_BASE64CHAR[i & 0x3f]; 294 ostream.write(out, 0, 4); 295 rindex += 3; 296 rest -= 3; 297 } 298 if (rest == 1) 299 { 300 int i = data[rindex] & 0xff; 301 out[0] = (byte)S_BASE64CHAR[i >> 2]; 302 out[1] = (byte)S_BASE64CHAR[(i << 4) & 0x3f]; 303 out[2] = (byte)S_BASE64PAD; 304 out[3] = (byte)S_BASE64PAD; 305 ostream.write(out, 0, 4); 306 } 307 else if (rest == 2) 308 { 309 int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff); 310 out[0] = (byte)S_BASE64CHAR[i >> 10]; 311 out[1] = (byte)S_BASE64CHAR[(i >> 4) & 0x3f]; 312 out[2] = (byte)S_BASE64CHAR[(i << 2) & 0x3f]; 313 out[3] = (byte)S_BASE64PAD; 314 ostream.write(out, 0, 4); 315 } 316 } 317 318 321 public static void encode(byte[] data, int off, int len, Writer writer) throws IOException 322 { 323 if (len <= 0) return; 324 char[] out = new char[4]; 325 int rindex = off; 326 int rest = len - off; 327 int output = 0; 328 while (rest >= 3) 329 { 330 int i = ((data[rindex] & 0xff) << 16) 331 + ((data[rindex + 1] & 0xff) << 8) 332 + (data[rindex + 2] & 0xff); 333 out[0] = S_BASE64CHAR[i >> 18]; 334 out[1] = S_BASE64CHAR[(i >> 12) & 0x3f]; 335 out[2] = S_BASE64CHAR[(i >> 6) & 0x3f]; 336 out[3] = S_BASE64CHAR[i & 0x3f]; 337 writer.write(out, 0, 4); 338 rindex += 3; 339 rest -= 3; 340 output += 4; 341 if (output % 76 == 0) 342 writer.write(JavaUtils.LS); 343 } 344 if (rest == 1) 345 { 346 int i = data[rindex] & 0xff; 347 out[0] = S_BASE64CHAR[i >> 2]; 348 out[1] = S_BASE64CHAR[(i << 4) & 0x3f]; 349 out[2] = S_BASE64PAD; 350 out[3] = S_BASE64PAD; 351 writer.write(out, 0, 4); 352 } 353 else if (rest == 2) 354 { 355 int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff); 356 out[0] = S_BASE64CHAR[i >> 10]; 357 out[1] = S_BASE64CHAR[(i >> 4) & 0x3f]; 358 out[2] = S_BASE64CHAR[(i << 2) & 0x3f]; 359 out[3] = S_BASE64PAD; 360 writer.write(out, 0, 4); 361 } 362 } 363 } 364 | Popular Tags |