Base64.java
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         // *  char[] version
00086         // ****************************************************************************************
00087 
00095         public final static char[] encodeToChar(byte[] sArr, boolean lineSep)
00096         {
00097                 // Check special case
00098                 int sLen = sArr != null ? sArr.length : 0;
00099                 if (sLen == 0)
00100                         return new char[0];
00101 
00102                 int eLen = (sLen / 3) * 3;              // Length of even 24-bits.
00103                 int cCnt = ((sLen - 1) / 3 + 1) << 2;   // Returned character count
00104                 int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
00105                 char[] dArr = new char[dLen];
00106 
00107                 // Encode even 24-bits
00108                 for (int s = 0, d = 0, cc = 0; s < eLen;) {
00109                         // Copy next three bytes into lower 24 bits of int, paying attension to sign.
00110                         int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);
00111 
00112                         // Encode the int into four chars
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                         // Add optional line separator
00119                         if (lineSep && ++cc == 19 && d < dLen - 2) {
00120                                 dArr[d++] = '\r';
00121                                 dArr[d++] = '\n';
00122                                 cc = 0;
00123                         }
00124                 }
00125 
00126                 // Pad and encode last bits if source isn't even 24 bits.
00127                 int left = sLen - eLen; // 0 - 2.
00128                 if (left > 0) {
00129                         // Prepare the int
00130                         int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
00131 
00132                         // Set last four chars
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                 // Check special case
00150                 int sLen = sArr != null ? sArr.length : 0;
00151                 if (sLen == 0)
00152                         return new byte[0];
00153 
00154                 // Count illegal characters (including '\r', '\n') to know what size the returned array will be,
00155                 // so we don't have to reallocate & copy it later.
00156                 int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
00157                 for (int i = 0; i < sLen; i++)  // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
00158                         if (IA[sArr[i]] < 0)
00159                                 sepCnt++;
00160 
00161                 // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
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];       // Preallocate byte[] of exact length
00173 
00174                 for (int s = 0, d = 0; d < len;) {
00175                         // Assemble three bytes into an int from four "valid" characters.
00176                         int i = 0;
00177                         for (int j = 0; j < 4; j++) {   // j only increased if a valid char was found.
00178                                 int c = IA[sArr[s++]];
00179                                 if (c >= 0)
00180                                     i |= c << (18 - j * 6);
00181                                 else
00182                                         j--;
00183                         }
00184                         // Add the bytes
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                 // Check special case
00207                 int sLen = sArr.length;
00208                 if (sLen == 0)
00209                         return new byte[0];
00210 
00211                 int sIx = 0, eIx = sLen - 1;    // Start and end index after trimming.
00212 
00213                 // Trim illegal chars from start
00214                 while (sIx < eIx && IA[sArr[sIx]] < 0)
00215                         sIx++;
00216 
00217                 // Trim illegal chars from end
00218                 while (eIx > 0 && IA[sArr[eIx]] < 0)
00219                         eIx--;
00220 
00221                 // get the padding count (=) (0, 1 or 2)
00222                 int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0;  // Count '=' at end.
00223                 int cCnt = eIx - sIx + 1;   // Content count including possible separators
00224                 int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
00225 
00226                 int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
00227                 byte[] dArr = new byte[len];       // Preallocate byte[] of exact length
00228 
00229                 // Decode all but the last 0 - 2 bytes.
00230                 int d = 0;
00231                 for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
00232                         // Assemble three bytes into an int from four "valid" characters.
00233                         int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
00234 
00235                         // Add the bytes
00236                         dArr[d++] = (byte) (i >> 16);
00237                         dArr[d++] = (byte) (i >> 8);
00238                         dArr[d++] = (byte) i;
00239 
00240                         // If line separator, jump over it.
00241                         if (sepCnt > 0 && ++cc == 19) {
00242                                 sIx += 2;
00243                                 cc = 0;
00244                         }
00245                 }
00246 
00247                 if (d < len) {
00248                         // Decode last 1-3 bytes (incl '=') into 1-3 bytes
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         // *  byte[] version
00262         // ****************************************************************************************
00263 
00271         public final static byte[] encodeToByte(byte[] sArr, boolean lineSep)
00272         {
00273                 // Check special case
00274                 int sLen = sArr != null ? sArr.length : 0;
00275                 if (sLen == 0)
00276                         return new byte[0];
00277 
00278                 int eLen = (sLen / 3) * 3;                              // Length of even 24-bits.
00279                 int cCnt = ((sLen - 1) / 3 + 1) << 2;                   // Returned character count
00280                 int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
00281                 byte[] dArr = new byte[dLen];
00282 
00283                 // Encode even 24-bits
00284                 for (int s = 0, d = 0, cc = 0; s < eLen;) {
00285                         // Copy next three bytes into lower 24 bits of int, paying attension to sign.
00286                         int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);
00287 
00288                         // Encode the int into four chars
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                         // Add optional line separator
00295                         if (lineSep && ++cc == 19 && d < dLen - 2) {
00296                                 dArr[d++] = '\r';
00297                                 dArr[d++] = '\n';
00298                                 cc = 0;
00299                         }
00300                 }
00301 
00302                 // Pad and encode last bits if source isn't an even 24 bits.
00303                 int left = sLen - eLen; // 0 - 2.
00304                 if (left > 0) {
00305                         // Prepare the int
00306                         int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
00307 
00308                         // Set last four chars
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                 // Check special case
00326                 int sLen = sArr.length;
00327 
00328                 // Count illegal characters (including '\r', '\n') to know what size the returned array will be,
00329                 // so we don't have to reallocate & copy it later.
00330                 int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
00331                 for (int i = 0; i < sLen; i++)      // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
00332                         if (IA[sArr[i] & 0xff] < 0)
00333                                 sepCnt++;
00334 
00335                 // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
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];       // Preallocate byte[] of exact length
00347 
00348                 for (int s = 0, d = 0; d < len;) {
00349                         // Assemble three bytes into an int from four "valid" characters.
00350                         int i = 0;
00351                         for (int j = 0; j < 4; j++) {   // j only increased if a valid char was found.
00352                                 int c = IA[sArr[s++] & 0xff];
00353                                 if (c >= 0)
00354                                     i |= c << (18 - j * 6);
00355                                 else
00356                                         j--;
00357                         }
00358 
00359                         // Add the bytes
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                 // Check special case
00384                 int sLen = sArr.length;
00385                 if (sLen == 0)
00386                         return new byte[0];
00387 
00388                 int sIx = 0, eIx = sLen - 1;    // Start and end index after trimming.
00389 
00390                 // Trim illegal chars from start
00391                 while (sIx < eIx && IA[sArr[sIx] & 0xff] < 0)
00392                         sIx++;
00393 
00394                 // Trim illegal chars from end
00395                 while (eIx > 0 && IA[sArr[eIx] & 0xff] < 0)
00396                         eIx--;
00397 
00398                 // get the padding count (=) (0, 1 or 2)
00399                 int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0;  // Count '=' at end.
00400                 int cCnt = eIx - sIx + 1;   // Content count including possible separators
00401                 int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
00402 
00403                 int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
00404                 byte[] dArr = new byte[len];       // Preallocate byte[] of exact length
00405 
00406                 // Decode all but the last 0 - 2 bytes.
00407                 int d = 0;
00408                 for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
00409                         // Assemble three bytes into an int from four "valid" characters.
00410                         int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
00411 
00412                         // Add the bytes
00413                         dArr[d++] = (byte) (i >> 16);
00414                         dArr[d++] = (byte) (i >> 8);
00415                         dArr[d++] = (byte) i;
00416 
00417                         // If line separator, jump over it.
00418                         if (sepCnt > 0 && ++cc == 19) {
00419                                 sIx += 2;
00420                                 cc = 0;
00421                         }
00422                 }
00423 
00424                 if (d < len) {
00425                         // Decode last 1-3 bytes (incl '=') into 1-3 bytes
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         // * String version
00439         // ****************************************************************************************
00440 
00448         public final static String encodeToString(byte[] sArr, boolean lineSep)
00449         {
00450                 // Reuse char[] since we can't create a String incrementally anyway and StringBuffer/Builder would be slower.
00451                 return new String(encodeToChar(sArr, lineSep));
00452         }
00453 
00462         public final static byte[] decode(String str)
00463         {
00464                 // Check special case
00465                 int sLen = str != null ? str.length() : 0;
00466                 if (sLen == 0)
00467                         return new byte[0];
00468 
00469                 // Count illegal characters (including '\r', '\n') to know what size the returned array will be,
00470                 // so we don't have to reallocate & copy it later.
00471                 int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
00472                 for (int i = 0; i < sLen; i++)  // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
00473                         if (IA[str.charAt(i)] < 0)
00474                                 sepCnt++;
00475 
00476                 // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
00477                 if ((sLen - sepCnt) % 4 != 0)
00478                         return null;
00479 
00480                 // Count '=' at end
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];       // Preallocate byte[] of exact length
00489 
00490                 for (int s = 0, d = 0; d < len;) {
00491                         // Assemble three bytes into an int from four "valid" characters.
00492                         int i = 0;
00493                         for (int j = 0; j < 4; j++) {   // j only increased if a valid char was found.
00494                                 int c = IA[str.charAt(s++)];
00495                                 if (c >= 0)
00496                                     i |= c << (18 - j * 6);
00497                                 else
00498                                         j--;
00499                         }
00500                         // Add the bytes
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                 // Check special case
00523                 int sLen = s.length();
00524                 if (sLen == 0)
00525                         return new byte[0];
00526 
00527                 int sIx = 0, eIx = sLen - 1;    // Start and end index after trimming.
00528 
00529                 // Trim illegal chars from start
00530                 while (sIx < eIx && IA[s.charAt(sIx) & 0xff] < 0)
00531                         sIx++;
00532 
00533                 // Trim illegal chars from end
00534                 while (eIx > 0 && IA[s.charAt(eIx) & 0xff] < 0)
00535                         eIx--;
00536 
00537                 // get the padding count (=) (0, 1 or 2)
00538                 int pad = s.charAt(eIx) == '=' ? (s.charAt(eIx - 1) == '=' ? 2 : 1) : 0;  // Count '=' at end.
00539                 int cCnt = eIx - sIx + 1;   // Content count including possible separators
00540                 int sepCnt = sLen > 76 ? (s.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0;
00541 
00542                 int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
00543                 byte[] dArr = new byte[len];       // Preallocate byte[] of exact length
00544 
00545                 // Decode all but the last 0 - 2 bytes.
00546                 int d = 0;
00547                 for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
00548                         // Assemble three bytes into an int from four "valid" characters.
00549                         int i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6 | IA[s.charAt(sIx++)];
00550 
00551                         // Add the bytes
00552                         dArr[d++] = (byte) (i >> 16);
00553                         dArr[d++] = (byte) (i >> 8);
00554                         dArr[d++] = (byte) i;
00555 
00556                         // If line separator, jump over it.
00557                         if (sepCnt > 0 && ++cc == 19) {
00558                                 sIx += 2;
00559                                 cc = 0;
00560                         }
00561                 }
00562 
00563                 if (d < len) {
00564                         // Decode last 1-3 bytes (incl '=') into 1-3 bytes
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 }


smarthome_media_kodi_driver
Author(s): Mickael Gaillard , Erwan Le Huitouze
autogenerated on Thu Jun 6 2019 21:03:49