Base64E.java
2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.prws.common.utils;
//import com.aisimba.clearcar.util.aess.AESUtil;
//import com.aisimba.clearcar.util.sdk.utils.encoder.BASE64Encoder;
//import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
public class Base64E {
private static char[] SIXTY_FOUR_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
private static final int[] REVERSE_MAPPING = new int[123];
public Base64E() {
}
public void str(String str){
SIXTY_FOUR_CHARS=str.toCharArray();
}
public String encode(byte[] input) {
List<StringBuffer> buffers = new ArrayList<>();
StringBuffer result = new StringBuffer();
int outputCharCount = 0;
for (int i = 0; i < input.length; i += 3) {
int remaining = Math.min(3, input.length - i);
int oneBigNumber = (input[i] & 255) << 16 | (remaining <= 1 ? 0 : input[i + 1] & 255) << 8 | (remaining <= 2 ? 0 : input[i + 2] & 255);
for (int j = 0; j < 4; ++j) {
result.append(remaining + 1 > j ? SIXTY_FOUR_CHARS[63 & oneBigNumber >> 6 * (3 - j)] : '=');
}
outputCharCount += 4;
if (outputCharCount % 76 == 0) {
result.append('\n');
}
}
return result.toString();
}
public byte[] decode(String input) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
StringReader in = new StringReader(input);
for (int i = 0; i < input.length(); i += 4) {
int[] a = new int[]{this.mapCharToInt(in), this.mapCharToInt(in), this.mapCharToInt(in), this.mapCharToInt(in)};
int oneBigNumber = (a[0] & 63) << 18 | (a[1] & 63) << 12 | (a[2] & 63) << 6 | a[3] & 63;
for (int j = 0; j < 3; ++j) {
if (a[j + 1] >= 0) {
out.write(255 & oneBigNumber >> 8 * (2 - j));
}
}
}
return out.toByteArray();
} catch (IOException var8) {
throw new Error(var8 + ": " + var8.getMessage());
}
}
private int mapCharToInt(Reader input) throws IOException {
while (true) {
int c;
if ((c = input.read()) != -1) {
int result = REVERSE_MAPPING[c];
if (result != 0) {
return result - 1;
}
if (c != 61) {
continue;
}
return -1;
}
return -1;
}
}
static {
for (int i = 0; i < SIXTY_FOUR_CHARS.length; ++i) {
REVERSE_MAPPING[SIXTY_FOUR_CHARS[i]] = i + 1;
}
}
}