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

63
LINES

< > BotCompany Repo | #1001552 // base64decode

JavaX fragment (include)

1  
  static byte[] base64decode(String s) {
2  
    byte[] alphaToInt = base64decode_base64toint;
3  
    int sLen = s.length();
4  
    int numGroups = sLen/4;
5  
    if (4*numGroups != sLen)
6  
      throw new IllegalArgumentException(
7  
        "String length must be a multiple of four.");
8  
    int missingBytesInLastGroup = 0;
9  
    int numFullGroups = numGroups;
10  
    if (sLen != 0) {
11  
      if (s.charAt(sLen-1) == '=') {
12  
        missingBytesInLastGroup++;
13  
        numFullGroups--;
14  
      }
15  
      if (s.charAt(sLen-2) == '=')
16  
        missingBytesInLastGroup++;
17  
    }
18  
    byte[] result = new byte[3*numGroups - missingBytesInLastGroup];
19  
20  
    // Translate all full groups from base64 to byte array elements
21  
    int inCursor = 0, outCursor = 0;
22  
    for (int i=0; i<numFullGroups; i++) {
23  
      int ch0 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
24  
      int ch1 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
25  
      int ch2 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
26  
      int ch3 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
27  
      result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
28  
      result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
29  
      result[outCursor++] = (byte) ((ch2 << 6) | ch3);
30  
    }
31  
32  
    // Translate partial group, if present
33  
    if (missingBytesInLastGroup != 0) {
34  
      int ch0 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
35  
      int ch1 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
36  
      result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
37  
38  
      if (missingBytesInLastGroup == 1) {
39  
        int ch2 = base64decode_base64toint(s.charAt(inCursor++), alphaToInt);
40  
        result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
41  
      }
42  
    }
43  
    // assert inCursor == s.length()-missingBytesInLastGroup;
44  
    // assert outCursor == result.length;
45  
    return result;
46  
  }
47  
48  
  static int base64decode_base64toint(char c, byte[] alphaToInt) {
49  
    int result = alphaToInt[c];
50  
    if (result < 0)
51  
      throw new IllegalArgumentException("Illegal character " + c);
52  
    return result;
53  
  }
54  
55  
  static final byte base64decode_base64toint[] = {
56  
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
57  
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58  
    -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
59  
    55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
60  
    5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
61  
    24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
62  
    35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
63  
  };

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001552
Snippet name: base64decode
Eternal ID of this version: #1001552/1
Text MD5: 5749ed1ad53e065c0111cd71e62d154b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-24 15:32:21
Source code size: 2717 bytes / 63 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 685 / 1052
Referenced in: [show references]