Debian Codegraph @

src/com/bulenkov/iconloader/util/Base64Converter.java

raw symbols SCIP
1 /*
2 * Copyright 2000-2014 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.bulenkov.iconloader.util;
18
19 /**
20 * @author Konstantin Bulenkov
21 */
22 public class Base64Converter {
23 private static final char[] alphabet = {
24 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
25 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
26 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
27 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
28 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
29 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
30 'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
31 '4', '5', '6', '7', '8', '9', '+', '/'}; // 56 to 63
32
33 private static final byte[] decodeTable = {
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36 -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
37 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
38 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1,
39 -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1,
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1,
49 };
50
51 public static String encode(String s) {
52 return encode(s.getBytes());
53 }
54
55 public static String encode(byte[] octetString) {
56 int bits24;
57 int bits6;
58
59 char[] out = new char[((octetString.length - 1) / 3 + 1) * 4];
60
61 int outIndex = 0;
62 int i = 0;
63
64 while ((i + 3) <= octetString.length) {
65 // store the octets
66 bits24 = (octetString[i++] & 0xFF) << 16;
67 bits24 |= (octetString[i++] & 0xFF) << 8;
68 bits24 |= (octetString[i++] & 0xFF);
69
70 bits6 = (bits24 & 0x00FC0000) >> 18;
71 out[outIndex++] = alphabet[bits6];
72 bits6 = (bits24 & 0x0003F000) >> 12;
73 out[outIndex++] = alphabet[bits6];
74 bits6 = (bits24 & 0x00000FC0) >> 6;
75 out[outIndex++] = alphabet[bits6];
76 bits6 = (bits24 & 0x0000003F);
77 out[outIndex++] = alphabet[bits6];
78 }
79
80 if (octetString.length - i == 2) {
81 // store the octets
82 bits24 = (octetString[i] & 0xFF) << 16;
83 bits24 |= (octetString[i + 1] & 0xFF) << 8;
84
85 bits6 = (bits24 & 0x00FC0000) >> 18;
86 out[outIndex++] = alphabet[bits6];
87 bits6 = (bits24 & 0x0003F000) >> 12;
88 out[outIndex++] = alphabet[bits6];
89 bits6 = (bits24 & 0x00000FC0) >> 6;
90 out[outIndex++] = alphabet[bits6];
91
92 // padding
93 out[outIndex] = '=';
94 } else if (octetString.length - i == 1) {
95 // store the octets
96 bits24 = (octetString[i] & 0xFF) << 16;
97
98 bits6 = (bits24 & 0x00FC0000) >> 18;
99 out[outIndex++] = alphabet[bits6];
100 bits6 = (bits24 & 0x0003F000) >> 12;
101 out[outIndex++] = alphabet[bits6];
102
103 // padding
104 out[outIndex++] = '=';
105 out[outIndex] = '=';
106 }
107
108 return StringFactory.createShared(out);
109 }
110
111 public static String decode(String s) {
112 return new String(decode(s.getBytes()));
113 }
114
115 public static byte[] decode(byte[] bytes) {
116 int paddingCount = 0;
117 int realLength = 0;
118
119 for (int i = bytes.length - 1; i >= 0; i--) {
120 if (bytes[i] > ' ') {
121 realLength++;
122 }
123
124 if (bytes[i] == '=') {
125 paddingCount++;
126 }
127 }
128
129 if (realLength % 4 != 0) {
130 throw new IllegalArgumentException("Incorrect length " + realLength + ". Must be a multiple of 4");
131 }
132
133 final byte[] out = new byte[realLength / 4 * 3 - paddingCount];
134 final byte[] t = new byte[4];
135 int outIndex = 0;
136 int index = 0;
137 t[0] = t[1] = t[2] = t[3] = '=';
138
139 for (byte c : bytes) {
140 if (c > ' ') {
141 t[index++] = c;
142 }
143
144 if (index == 4) {
145 outIndex += decode(out, outIndex, t[0], t[1], t[2], t[3]);
146 index = 0;
147 t[0] = t[1] = t[2] = t[3] = '=';
148 }
149 }
150
151 if (index > 0) {
152 decode(out, outIndex, t[0], t[1], t[2], t[3]);
153 }
154 return out;
155 }
156
157 private static int decode(byte[] output, int outIndex, byte a, byte b, byte c, byte d) {
158 byte da = decodeTable[a];
159 byte db = decodeTable[b];
160 byte dc = decodeTable[c];
161 byte dd = decodeTable[d];
162
163 if ((da == -1) || (db == -1) || ((dc == -1) && (c != '=')) || ((dd == -1) && (d != '='))) {
164 throw new IllegalArgumentException(
165 "Invalid character [" + (a & 0xFF) + ", " + (b & 0xFF) + ", " + (c & 0xFF) + ", " + (d & 0xFF) + "]");
166 }
167 output[outIndex++] = (byte) ((da << 2) | db >>> 4);
168
169 if (c == '=') {
170 return 1;
171 }
172 output[outIndex++] = (byte) ((db << 4) | dc >>> 2);
173
174 if (d == '=') {
175 return 2;
176 }
177 output[outIndex] = (byte) ((dc << 6) | dd);
178 return 3;
179 }
180 }