Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

52
LINES

< > BotCompany Repo | #1000750 // base64encode

JavaX fragment (include)

1  
  static String base64encode(byte[] a) {
2  
    int aLen = a.length;
3  
    int numFullGroups = aLen/3;
4  
    int numBytesInPartialGroup = aLen - 3*numFullGroups;
5  
    int resultLen = 4*((aLen + 2)/3);
6  
    StringBuffer result = new StringBuffer(resultLen);
7  
    char[] intToAlpha = intToBase64;
8  
9  
    // Translate all full groups from byte array elements to Base64
10  
    int inCursor = 0;
11  
    for (int i=0; i<numFullGroups; i++) {
12  
      int byte0 = a[inCursor++] & 0xff;
13  
      int byte1 = a[inCursor++] & 0xff;
14  
      int byte2 = a[inCursor++] & 0xff;
15  
      result.append(intToAlpha[byte0 >> 2]);
16  
      result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
17  
      result.append(intToAlpha[(byte1 << 2)&0x3f | (byte2 >> 6)]);
18  
      result.append(intToAlpha[byte2 & 0x3f]);
19  
    }
20  
21  
    // Translate partial group if present
22  
    if (numBytesInPartialGroup != 0) {
23  
      int byte0 = a[inCursor++] & 0xff;
24  
      result.append(intToAlpha[byte0 >> 2]);
25  
      if (numBytesInPartialGroup == 1) {
26  
        result.append(intToAlpha[(byte0 << 4) & 0x3f]);
27  
        result.append("==");
28  
      } else {
29  
        // assert numBytesInPartialGroup == 2;
30  
        int byte1 = a[inCursor++] & 0xff;
31  
        result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
32  
        result.append(intToAlpha[(byte1 << 2)&0x3f]);
33  
        result.append('=');
34  
      }
35  
    }
36  
    // assert inCursor == a.length;
37  
    // assert result.length() == resultLen;
38  
    return result.toString();
39  
  }
40  
41  
  /**
42  
   * This array is a lookup table that translates 6-bit positive integer
43  
   * index values into their "Base64 Alphabet" equivalents as specified
44  
   * in Table 1 of RFC 2045.
45  
   */
46  
  static final char intToBase64[] = {
47  
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
48  
    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
49  
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
50  
    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
51  
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
52  
  };

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000750
Snippet name: base64encode
Eternal ID of this version: #1000750/1
Text MD5: 2394dc52c19c893329dad4d4caae719e
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-24 21:23:44
Source code size: 2065 bytes / 52 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 701 / 1066
Referenced in: [show references]