1 64 65 package com.jcorporate.expresso.core.misc; 66 67 68 74 public class CookieBase64 { 75 public CookieBase64() { 76 } 77 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 97 static private byte convertToNumber(byte inByte) { 98 if (inByte >= 'A' && inByte <= 'Z') { 99 return (byte) (inByte - 'A'); 100 } 101 if (inByte >= 'a' && inByte <= 'z') { 102 return (byte) (inByte - 'a' + 26); 103 } 104 if (inByte >= '0' && inByte <= '9') { 105 return (byte) (inByte - '0' + 52); 106 } 107 if (inByte == ',') { 108 return (62); 109 } 110 if (inByte == '.') { 111 return (63); 112 } 113 114 return (-1); 115 } 116 117 125 static private byte[] decode(String data) 126 throws IllegalArgumentException { 127 128 if (data == null) { 130 throw new IllegalArgumentException ("Base64.decode: data must not be null"); 131 } 132 if (data.length() % 4 != 0) { 134 throw new IllegalArgumentException ("Base64.decode: data it not of proper length"); 135 } 136 137 byte[] inputBuffer = data.getBytes(); 138 int validInputCount = 0; 139 byte[] inBuffer = new byte[inputBuffer.length]; 140 int dataLength = inputBuffer.length; 141 142 int i; 148 149 for (i = 0; i < dataLength; i++) { 150 byte temp = convertToNumber(inputBuffer[i]); 151 152 if (temp >= 0) { 153 inBuffer[i] = temp; 154 validInputCount++; 155 } 156 } 157 158 byte[] outBuffer = new byte[dataLength]; 159 int startOut = 0; 160 int startIn = 0; 161 162 while (validInputCount > startIn) { 163 int dataLen = validInputCount; 164 165 if (dataLen - startIn > 3) { 166 outBuffer[startOut] = (byte) ((inBuffer[startIn] << 2) + 167 (inBuffer[startIn + 1] >>> 4)); 168 outBuffer[startOut + 1] = (byte) ((inBuffer[startIn + 1] << 4) + 169 (inBuffer[startIn + 2] >>> 2)); 170 outBuffer[startOut + 2] = (byte) ((inBuffer[startIn + 2] << 6) + 171 (inBuffer[startIn + 3])); 172 startOut += 3; 173 startIn += 4; 174 } else if (dataLen - startIn > 2) { 175 outBuffer[startOut] = (byte) ((inBuffer[startIn] << 2) + 176 (inBuffer[startIn + 1] >>> 4)); 177 outBuffer[startOut + 1] = (byte) ((inBuffer[startIn + 1] << 4) + 178 (inBuffer[startIn + 2] >>> 2)); 179 startOut += 2; 180 startIn += 3; 181 } else if (dataLen - startIn > 1) { 182 outBuffer[startOut] = (byte) ((inBuffer[startIn] << 2) + 183 (inBuffer[startIn + 1] >>> 4)); 184 startOut += 1; 185 startIn += 2; 186 } else { 187 throw new IllegalStateException ("com.jcorporate.expresso.core.misc." + 188 "Base64.Decode: Corrupt Input Data"); 189 } 190 } 191 192 byte[] finalOutput = new byte[startOut]; 194 195 for (i = 0; i < startOut; i++) { 196 finalOutput[i] = outBuffer[i]; 197 } 198 199 inBuffer = null; 200 outBuffer = null; 201 202 return finalOutput; 203 } 204 205 206 219 static public byte[] decodeNoPadding(String data) 220 throws IllegalArgumentException { 221 String myName = "Base64.decodeNoPadding"; 222 223 if (data == null) { 224 throw new IllegalArgumentException (myName + " parameter 'data'" + 225 " must not be null"); 226 } 227 if (data.length() == 0) { 228 throw new IllegalArgumentException (myName + " parameter 'data'" + 229 " must not be greater than zero length"); 230 } 231 for (; data.length() % 4 != 0; data = data + "=") { 233 ; 234 } 235 236 return decode(data); 237 } 238 239 240 247 static private String encode(byte[] inBuffer) 248 throws IllegalArgumentException { 249 if (inBuffer.length == 0) { 250 throw new IllegalArgumentException ("Base64.encode: " + 251 "inBuffer must be > zero bytes in length"); 252 } 253 254 int dataSize = inBuffer.length; 255 int startIn = 0; 256 int startOut = 0; 257 258 byte[] outBuffer; 261 262 if (dataSize < 4) { 263 outBuffer = new byte[4]; 264 } else { 265 outBuffer = new byte[dataSize * 2]; 266 } 267 while (startIn < dataSize) { 269 int dataLen = inBuffer.length; 270 byte a; 271 byte b; 272 byte c; 273 274 if ((startOut % bytesPerLine) + 4 > bytesPerLine) { 276 outBuffer[startOut] = (byte) '\r'; 277 outBuffer[startOut + 1] = (byte) '\n'; 278 startOut += 2; 279 } 280 if (dataLen - startIn > 2) { 281 a = inBuffer[startIn]; 282 b = inBuffer[startIn + 1]; 283 c = inBuffer[startIn + 2]; 284 outBuffer[startOut] = (byte) vec[(a >>> 2) & 0x3F]; 285 outBuffer[startOut + 1] = (byte) vec[((a << 4) & 0x30) + 286 ((b >>> 4) & 0xf)]; 287 outBuffer[startOut + 2] = (byte) vec[((b << 2) & 0x3c) + 288 ((c >>> 6) & 0x3)]; 289 outBuffer[startOut + 3] = (byte) vec[c & 0x3F]; 290 startOut += 4; 291 startIn += 3; 292 } else if (dataLen - startIn == 1) { 293 a = inBuffer[startIn]; 294 b = 0; 295 c = 0; 296 outBuffer[startOut] = (byte) vec[(a >>> 2) & 0x3F]; 297 outBuffer[startOut + 1] = (byte) vec[((a << 4) & 0x30) + 298 ((b >>> 4) & 0xf)]; 299 outBuffer[startOut + 2] = padding; 300 outBuffer[startOut + 3] = padding; 301 startOut += 4; 302 startIn += 2; 303 } else if (dataLen - startIn == 2) { 304 a = inBuffer[startIn]; 305 b = inBuffer[startIn + 1]; 306 c = 0; 307 outBuffer[startOut] = (byte) vec[(a >>> 2) & 0x3F]; 308 outBuffer[startOut + 1] = (byte) vec[((a << 4) & 0x30) + 309 ((b >>> 4) & 0xf)]; 310 outBuffer[startOut + 2] = (byte) vec[((b << 2) & 0x3c) + 311 ((c >>> 6) & 0x3)]; 312 outBuffer[startOut + 3] = padding; 313 startOut += 4; 314 startIn += 2; 315 } 316 } 317 318 return new String (outBuffer, 0, startOut); 319 } 320 321 322 323 324 325 326 327 341 static public String encodeNoPadding(byte[] inBuffer) 342 throws IllegalArgumentException { 343 String stringWithPadding = encode(inBuffer); 344 int equalsPlacement = stringWithPadding.indexOf((int) '='); 345 346 return (equalsPlacement != -1) 347 ? (stringWithPadding.substring(0, equalsPlacement)) 348 : (stringWithPadding); 349 } 350 351 352 } | Popular Tags |