1 64 65 package com.jcorporate.expresso.core.misc; 66 67 77 public class Base64 { 78 static final private int bytesPerLine = 80; 80 81 private final static char[] vec = { 82 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; 91 private final static byte padding = (byte) '='; 92 93 public Base64() { 94 } 95 96 100 static private byte convertToNumber(byte inByte) { 101 if (inByte >= 'A' && inByte <= 'Z') { 102 return (byte) (inByte - 'A'); 103 } 104 if (inByte >= 'a' && inByte <= 'z') { 105 return (byte) (inByte - 'a' + 26); 106 } 107 if (inByte >= '0' && inByte <= '9') { 108 return (byte) (inByte - '0' + 52); 109 } 110 if (inByte == '+') { 111 return (62); 112 } 113 if (inByte == '/') { 114 return (63); 115 } 116 117 return (-1); 118 } 119 120 128 static public byte[] decode(String data) 129 throws IllegalArgumentException { 130 131 if (data == null) { 133 throw new IllegalArgumentException ("Base64.decode: data must not be null"); 134 } 135 if (data.length() % 4 != 0) { 137 throw new IllegalArgumentException ("Base64.decode: data is not of proper length"); 138 } 139 140 byte[] inputBuffer = data.getBytes(); 141 int validInputCount = 0; 142 byte[] inBuffer = new byte[inputBuffer.length]; 143 int dataLength = inputBuffer.length; 144 145 int i; 151 152 for (i = 0; i < dataLength; i++) { 153 byte temp = convertToNumber(inputBuffer[i]); 154 155 if (temp >= 0) { 156 inBuffer[i] = temp; 157 validInputCount++; 158 } 159 } 160 161 byte[] outBuffer = new byte[dataLength]; 162 int startOut = 0; 163 int startIn = 0; 164 165 while (validInputCount > startIn) { 166 int dataLen = validInputCount; 167 168 if (dataLen - startIn > 3) { 169 outBuffer[startOut] = (byte) ((inBuffer[startIn] << 2) + 170 (inBuffer[startIn + 1] >>> 4)); 171 outBuffer[startOut + 1] = (byte) ((inBuffer[startIn + 1] << 4) + 172 (inBuffer[startIn + 2] >>> 2)); 173 outBuffer[startOut + 2] = (byte) ((inBuffer[startIn + 2] << 6) + 174 (inBuffer[startIn + 3])); 175 startOut += 3; 176 startIn += 4; 177 } else if (dataLen - startIn > 2) { 178 outBuffer[startOut] = (byte) ((inBuffer[startIn] << 2) + 179 (inBuffer[startIn + 1] >>> 4)); 180 outBuffer[startOut + 1] = (byte) ((inBuffer[startIn + 1] << 4) + 181 (inBuffer[startIn + 2] >>> 2)); 182 startOut += 2; 183 startIn += 3; 184 } else if (dataLen - startIn > 1) { 185 outBuffer[startOut] = (byte) ((inBuffer[startIn] << 2) + 186 (inBuffer[startIn + 1] >>> 4)); 187 startOut += 1; 188 startIn += 2; 189 } else { 190 throw new IllegalStateException ("com.jcorporate.expresso.core.misc." + 191 "Base64.Decode: Corrupt Input Data"); 192 } 193 } 194 195 byte[] finalOutput = new byte[startOut]; 197 198 for (i = 0; i < startOut; i++) { 199 finalOutput[i] = outBuffer[i]; 200 } 201 202 inBuffer = null; 203 outBuffer = null; 204 205 return finalOutput; 206 } 207 208 209 222 static public byte[] decodeNoPadding(String data) 223 throws IllegalArgumentException { 224 String myName = "Base64.decodeNoPadding"; 225 226 if (data == null) { 227 throw new IllegalArgumentException (myName + " parameter 'data'" + 228 " must not be null"); 229 } 230 if (data.length() == 0) { 231 throw new IllegalArgumentException (myName + " parameter 'data'" + 232 " must not be greater than zero length"); 233 } 234 for (; data.length() % 4 != 0; data = data + "=") { 236 ; 237 } 238 239 return decode(data); 240 } 241 242 243 250 static public String encode(byte[] inBuffer) 251 throws IllegalArgumentException { 252 if (inBuffer.length == 0) { 253 throw new IllegalArgumentException ("Base64.encode: " + 254 "inBuffer must be > zero bytes in length"); 255 } 256 257 int dataSize = inBuffer.length; 258 int startIn = 0; 259 int startOut = 0; 260 261 byte[] outBuffer; 264 265 if (dataSize < 4) { 266 outBuffer = new byte[4]; 267 } else { 268 outBuffer = new byte[dataSize * 2]; 269 } 270 while (startIn < dataSize) { 272 int dataLen = inBuffer.length; 273 byte a; 274 byte b; 275 byte c; 276 277 if ((startOut % bytesPerLine) + 4 > bytesPerLine) { 279 outBuffer[startOut] = (byte) '\r'; 280 outBuffer[startOut + 1] = (byte) '\n'; 281 startOut += 2; 282 } 283 if (dataLen - startIn > 2) { 284 a = inBuffer[startIn]; 285 b = inBuffer[startIn + 1]; 286 c = inBuffer[startIn + 2]; 287 outBuffer[startOut] = (byte) vec[(a >>> 2) & 0x3F]; 288 outBuffer[startOut + 1] = (byte) vec[((a << 4) & 0x30) + 289 ((b >>> 4) & 0xf)]; 290 outBuffer[startOut + 2] = (byte) vec[((b << 2) & 0x3c) + 291 ((c >>> 6) & 0x3)]; 292 outBuffer[startOut + 3] = (byte) vec[c & 0x3F]; 293 startOut += 4; 294 startIn += 3; 295 } else if (dataLen - startIn == 1) { 296 a = inBuffer[startIn]; 297 b = 0; 298 c = 0; 299 outBuffer[startOut] = (byte) vec[(a >>> 2) & 0x3F]; 300 outBuffer[startOut + 1] = (byte) vec[((a << 4) & 0x30) + 301 ((b >>> 4) & 0xf)]; 302 outBuffer[startOut + 2] = padding; 303 outBuffer[startOut + 3] = padding; 304 startOut += 4; 305 startIn += 2; 306 } else if (dataLen - startIn == 2) { 307 a = inBuffer[startIn]; 308 b = inBuffer[startIn + 1]; 309 c = 0; 310 outBuffer[startOut] = (byte) vec[(a >>> 2) & 0x3F]; 311 outBuffer[startOut + 1] = (byte) vec[((a << 4) & 0x30) + 312 ((b >>> 4) & 0xf)]; 313 outBuffer[startOut + 2] = (byte) vec[((b << 2) & 0x3c) + 314 ((c >>> 6) & 0x3)]; 315 outBuffer[startOut + 3] = padding; 316 startOut += 4; 317 startIn += 2; 318 } 319 } 320 321 return new String (outBuffer, 0, startOut); 322 } 323 324 325 326 327 328 329 330 344 static public String encodeNoPadding(byte[] inBuffer) 345 throws IllegalArgumentException { 346 String stringWithPadding = encode(inBuffer); 347 int equalsPlacement = stringWithPadding.indexOf((int) '='); 348 349 if (equalsPlacement == -1) { 350 return stringWithPadding; 351 } else { 352 return stringWithPadding.substring(0, equalsPlacement); 353 } 354 } 355 356 357 } 358 359 | Popular Tags |