Go to the documentation of this file.00001 package org.xbmc.android.jsonrpc.io;
00002
00003 import java.util.Arrays;
00004
00073 public class Base64
00074 {
00075 private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
00076 private static final int[] IA = new int[256];
00077 static {
00078 Arrays.fill(IA, -1);
00079 for (int i = 0, iS = CA.length; i < iS; i++)
00080 IA[CA[i]] = i;
00081 IA['='] = 0;
00082 }
00083
00084
00085
00086
00087
00095 public final static char[] encodeToChar(byte[] sArr, boolean lineSep)
00096 {
00097
00098 int sLen = sArr != null ? sArr.length : 0;
00099 if (sLen == 0)
00100 return new char[0];
00101
00102 int eLen = (sLen / 3) * 3;
00103 int cCnt = ((sLen - 1) / 3 + 1) << 2;
00104 int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0);
00105 char[] dArr = new char[dLen];
00106
00107
00108 for (int s = 0, d = 0, cc = 0; s < eLen;) {
00109
00110 int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);
00111
00112
00113 dArr[d++] = CA[(i >>> 18) & 0x3f];
00114 dArr[d++] = CA[(i >>> 12) & 0x3f];
00115 dArr[d++] = CA[(i >>> 6) & 0x3f];
00116 dArr[d++] = CA[i & 0x3f];
00117
00118
00119 if (lineSep && ++cc == 19 && d < dLen - 2) {
00120 dArr[d++] = '\r';
00121 dArr[d++] = '\n';
00122 cc = 0;
00123 }
00124 }
00125
00126
00127 int left = sLen - eLen;
00128 if (left > 0) {
00129
00130 int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
00131
00132
00133 dArr[dLen - 4] = CA[i >> 12];
00134 dArr[dLen - 3] = CA[(i >>> 6) & 0x3f];
00135 dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '=';
00136 dArr[dLen - 1] = '=';
00137 }
00138 return dArr;
00139 }
00140
00147 public final static byte[] decode(char[] sArr)
00148 {
00149
00150 int sLen = sArr != null ? sArr.length : 0;
00151 if (sLen == 0)
00152 return new byte[0];
00153
00154
00155
00156 int sepCnt = 0;
00157 for (int i = 0; i < sLen; i++)
00158 if (IA[sArr[i]] < 0)
00159 sepCnt++;
00160
00161
00162 if ((sLen - sepCnt) % 4 != 0)
00163 return null;
00164
00165 int pad = 0;
00166 for (int i = sLen; i > 1 && IA[sArr[--i]] <= 0;)
00167 if (sArr[i] == '=')
00168 pad++;
00169
00170 int len = ((sLen - sepCnt) * 6 >> 3) - pad;
00171
00172 byte[] dArr = new byte[len];
00173
00174 for (int s = 0, d = 0; d < len;) {
00175
00176 int i = 0;
00177 for (int j = 0; j < 4; j++) {
00178 int c = IA[sArr[s++]];
00179 if (c >= 0)
00180 i |= c << (18 - j * 6);
00181 else
00182 j--;
00183 }
00184
00185 dArr[d++] = (byte) (i >> 16);
00186 if (d < len) {
00187 dArr[d++]= (byte) (i >> 8);
00188 if (d < len)
00189 dArr[d++] = (byte) i;
00190 }
00191 }
00192 return dArr;
00193 }
00194
00204 public final static byte[] decodeFast(char[] sArr)
00205 {
00206
00207 int sLen = sArr.length;
00208 if (sLen == 0)
00209 return new byte[0];
00210
00211 int sIx = 0, eIx = sLen - 1;
00212
00213
00214 while (sIx < eIx && IA[sArr[sIx]] < 0)
00215 sIx++;
00216
00217
00218 while (eIx > 0 && IA[sArr[eIx]] < 0)
00219 eIx--;
00220
00221
00222 int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0;
00223 int cCnt = eIx - sIx + 1;
00224 int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
00225
00226 int len = ((cCnt - sepCnt) * 6 >> 3) - pad;
00227 byte[] dArr = new byte[len];
00228
00229
00230 int d = 0;
00231 for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
00232
00233 int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
00234
00235
00236 dArr[d++] = (byte) (i >> 16);
00237 dArr[d++] = (byte) (i >> 8);
00238 dArr[d++] = (byte) i;
00239
00240
00241 if (sepCnt > 0 && ++cc == 19) {
00242 sIx += 2;
00243 cc = 0;
00244 }
00245 }
00246
00247 if (d < len) {
00248
00249 int i = 0;
00250 for (int j = 0; sIx <= eIx - pad; j++)
00251 i |= IA[sArr[sIx++]] << (18 - j * 6);
00252
00253 for (int r = 16; d < len; r -= 8)
00254 dArr[d++] = (byte) (i >> r);
00255 }
00256
00257 return dArr;
00258 }
00259
00260
00261
00262
00263
00271 public final static byte[] encodeToByte(byte[] sArr, boolean lineSep)
00272 {
00273
00274 int sLen = sArr != null ? sArr.length : 0;
00275 if (sLen == 0)
00276 return new byte[0];
00277
00278 int eLen = (sLen / 3) * 3;
00279 int cCnt = ((sLen - 1) / 3 + 1) << 2;
00280 int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0);
00281 byte[] dArr = new byte[dLen];
00282
00283
00284 for (int s = 0, d = 0, cc = 0; s < eLen;) {
00285
00286 int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);
00287
00288
00289 dArr[d++] = (byte) CA[(i >>> 18) & 0x3f];
00290 dArr[d++] = (byte) CA[(i >>> 12) & 0x3f];
00291 dArr[d++] = (byte) CA[(i >>> 6) & 0x3f];
00292 dArr[d++] = (byte) CA[i & 0x3f];
00293
00294
00295 if (lineSep && ++cc == 19 && d < dLen - 2) {
00296 dArr[d++] = '\r';
00297 dArr[d++] = '\n';
00298 cc = 0;
00299 }
00300 }
00301
00302
00303 int left = sLen - eLen;
00304 if (left > 0) {
00305
00306 int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
00307
00308
00309 dArr[dLen - 4] = (byte) CA[i >> 12];
00310 dArr[dLen - 3] = (byte) CA[(i >>> 6) & 0x3f];
00311 dArr[dLen - 2] = left == 2 ? (byte) CA[i & 0x3f] : (byte) '=';
00312 dArr[dLen - 1] = '=';
00313 }
00314 return dArr;
00315 }
00316
00323 public final static byte[] decode(byte[] sArr)
00324 {
00325
00326 int sLen = sArr.length;
00327
00328
00329
00330 int sepCnt = 0;
00331 for (int i = 0; i < sLen; i++)
00332 if (IA[sArr[i] & 0xff] < 0)
00333 sepCnt++;
00334
00335
00336 if ((sLen - sepCnt) % 4 != 0)
00337 return null;
00338
00339 int pad = 0;
00340 for (int i = sLen; i > 1 && IA[sArr[--i] & 0xff] <= 0;)
00341 if (sArr[i] == '=')
00342 pad++;
00343
00344 int len = ((sLen - sepCnt) * 6 >> 3) - pad;
00345
00346 byte[] dArr = new byte[len];
00347
00348 for (int s = 0, d = 0; d < len;) {
00349
00350 int i = 0;
00351 for (int j = 0; j < 4; j++) {
00352 int c = IA[sArr[s++] & 0xff];
00353 if (c >= 0)
00354 i |= c << (18 - j * 6);
00355 else
00356 j--;
00357 }
00358
00359
00360 dArr[d++] = (byte) (i >> 16);
00361 if (d < len) {
00362 dArr[d++]= (byte) (i >> 8);
00363 if (d < len)
00364 dArr[d++] = (byte) i;
00365 }
00366 }
00367
00368 return dArr;
00369 }
00370
00371
00381 public final static byte[] decodeFast(byte[] sArr)
00382 {
00383
00384 int sLen = sArr.length;
00385 if (sLen == 0)
00386 return new byte[0];
00387
00388 int sIx = 0, eIx = sLen - 1;
00389
00390
00391 while (sIx < eIx && IA[sArr[sIx] & 0xff] < 0)
00392 sIx++;
00393
00394
00395 while (eIx > 0 && IA[sArr[eIx] & 0xff] < 0)
00396 eIx--;
00397
00398
00399 int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0;
00400 int cCnt = eIx - sIx + 1;
00401 int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
00402
00403 int len = ((cCnt - sepCnt) * 6 >> 3) - pad;
00404 byte[] dArr = new byte[len];
00405
00406
00407 int d = 0;
00408 for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
00409
00410 int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
00411
00412
00413 dArr[d++] = (byte) (i >> 16);
00414 dArr[d++] = (byte) (i >> 8);
00415 dArr[d++] = (byte) i;
00416
00417
00418 if (sepCnt > 0 && ++cc == 19) {
00419 sIx += 2;
00420 cc = 0;
00421 }
00422 }
00423
00424 if (d < len) {
00425
00426 int i = 0;
00427 for (int j = 0; sIx <= eIx - pad; j++)
00428 i |= IA[sArr[sIx++]] << (18 - j * 6);
00429
00430 for (int r = 16; d < len; r -= 8)
00431 dArr[d++] = (byte) (i >> r);
00432 }
00433
00434 return dArr;
00435 }
00436
00437
00438
00439
00440
00448 public final static String encodeToString(byte[] sArr, boolean lineSep)
00449 {
00450
00451 return new String(encodeToChar(sArr, lineSep));
00452 }
00453
00462 public final static byte[] decode(String str)
00463 {
00464
00465 int sLen = str != null ? str.length() : 0;
00466 if (sLen == 0)
00467 return new byte[0];
00468
00469
00470
00471 int sepCnt = 0;
00472 for (int i = 0; i < sLen; i++)
00473 if (IA[str.charAt(i)] < 0)
00474 sepCnt++;
00475
00476
00477 if ((sLen - sepCnt) % 4 != 0)
00478 return null;
00479
00480
00481 int pad = 0;
00482 for (int i = sLen; i > 1 && IA[str.charAt(--i)] <= 0;)
00483 if (str.charAt(i) == '=')
00484 pad++;
00485
00486 int len = ((sLen - sepCnt) * 6 >> 3) - pad;
00487
00488 byte[] dArr = new byte[len];
00489
00490 for (int s = 0, d = 0; d < len;) {
00491
00492 int i = 0;
00493 for (int j = 0; j < 4; j++) {
00494 int c = IA[str.charAt(s++)];
00495 if (c >= 0)
00496 i |= c << (18 - j * 6);
00497 else
00498 j--;
00499 }
00500
00501 dArr[d++] = (byte) (i >> 16);
00502 if (d < len) {
00503 dArr[d++]= (byte) (i >> 8);
00504 if (d < len)
00505 dArr[d++] = (byte) i;
00506 }
00507 }
00508 return dArr;
00509 }
00510
00520 public final static byte[] decodeFast(String s)
00521 {
00522
00523 int sLen = s.length();
00524 if (sLen == 0)
00525 return new byte[0];
00526
00527 int sIx = 0, eIx = sLen - 1;
00528
00529
00530 while (sIx < eIx && IA[s.charAt(sIx) & 0xff] < 0)
00531 sIx++;
00532
00533
00534 while (eIx > 0 && IA[s.charAt(eIx) & 0xff] < 0)
00535 eIx--;
00536
00537
00538 int pad = s.charAt(eIx) == '=' ? (s.charAt(eIx - 1) == '=' ? 2 : 1) : 0;
00539 int cCnt = eIx - sIx + 1;
00540 int sepCnt = sLen > 76 ? (s.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0;
00541
00542 int len = ((cCnt - sepCnt) * 6 >> 3) - pad;
00543 byte[] dArr = new byte[len];
00544
00545
00546 int d = 0;
00547 for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
00548
00549 int i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6 | IA[s.charAt(sIx++)];
00550
00551
00552 dArr[d++] = (byte) (i >> 16);
00553 dArr[d++] = (byte) (i >> 8);
00554 dArr[d++] = (byte) i;
00555
00556
00557 if (sepCnt > 0 && ++cc == 19) {
00558 sIx += 2;
00559 cc = 0;
00560 }
00561 }
00562
00563 if (d < len) {
00564
00565 int i = 0;
00566 for (int j = 0; sIx <= eIx - pad; j++)
00567 i |= IA[s.charAt(sIx++)] << (18 - j * 6);
00568
00569 for (int r = 16; d < len; r -= 8)
00570 dArr[d++] = (byte) (i >> r);
00571 }
00572
00573 return dArr;
00574 }
00575 }