• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-mirana / #68

31 Aug 2024 02:56AM UTC coverage: 84.4% (+1.0%) from 83.382%
#68

push

web-flow
Merge pull request #45 from trydofor/develop

v2.7.3 with minor change

474 of 568 new or added lines in 27 files covered. (83.45%)

8 existing lines in 6 files now uncovered.

5237 of 6205 relevant lines covered (84.4%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.58
/src/main/java/pro/fessional/mirana/bits/Base64.java
1
package pro.fessional.mirana.bits;
2

3
import org.jetbrains.annotations.NotNull;
4
import org.jetbrains.annotations.Nullable;
5
import pro.fessional.mirana.data.Null;
6
import pro.fessional.mirana.io.InputStreams;
7

8
import java.io.InputStream;
9

10
import static java.nio.charset.StandardCharsets.UTF_8;
11

12
/**
13
 * <pre>
14
 * RFC4648_URLSAFE, UTF8 and no pad by default.
15
 *
16
 * This array is a lookup table that translates 6-bit positive integer
17
 * index values into their "Base64 Alphabet" equivalents as specified
18
 * in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648).
19
 * 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
20
 * 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
21
 * 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
22
 * 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
23
 * '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
24
 *
25
 * It's the lookup table for "URL and Filename safe Base64" as specified
26
 * in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and
27
 * '_'. This table is used when BASE64_URL is specified.
28
 * 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
29
 * 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
30
 * 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
31
 * 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
32
 * '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
33
 * </pre>
34
 *
35
 * @author trydofor
36
 * @see java.util.Base64
37
 * @since 2019-10-12
38
 */
39
public class Base64 {
×
40

41
    public static boolean isB64(char c) {
42
        return ((c >= '0' && c <= '9') ||
1✔
43
                (c >= 'a' && c <= 'z') ||
44
                (c >= 'A' && c <= 'Z') ||
45
                c == '=' ||
46
                c == '-' || c == '_' ||
47
                c == '+' || c == '/');
48
    }
49

50
    public static boolean isB64(char c, boolean urlSafe) {
51
        return ((c >= '0' && c <= '9') ||
1✔
52
                (c >= 'a' && c <= 'z') ||
53
                (c >= 'A' && c <= 'Z') ||
54
                c == '=' ||
55
                (urlSafe && (c == '-' || c == '_')) ||
56
                (!urlSafe && (c == '+' || c == '/')));
57
    }
58

59
    /**
60
     * check base64 char URLSAFE and Default, empty string means false always
61
     */
62
    public static boolean isB64(String b64) {
63
        if (b64 == null) return false;
1✔
64
        final int len = b64.length();
1✔
65
        if (len == 0) return false;
1✔
66
        for (int i = 0; i < len; i++) {
1✔
67
            char c = b64.charAt(i);
1✔
68
            if (!isB64(c)) return false;
1✔
69
        }
70
        return true;
1✔
71
    }
72

73
    /**
74
     * check base64 char URLSAFE or Default, empty string means false always
75
     */
76
    public static boolean isB64(String b64, boolean urlSafe) {
77
        if (b64 == null) return false;
1✔
78
        final int len = b64.length();
1✔
79
        if (len == 0) return false;
1✔
80
        for (int i = 0; i < len; i++) {
1✔
81
            char c = b64.charAt(i);
1✔
82
            if (!isB64(c, urlSafe)) return false;
1✔
83
        }
84
        return true;
1✔
85
    }
86

87
    /**
88
     * check base64 char URLSAFE and Default, empty string means false always
89
     */
90
    public static boolean asB64(String b64) {
NEW
91
        if (b64 == null) return false;
×
NEW
92
        final int len = b64.length();
×
NEW
93
        int i = 0;
×
NEW
94
        for (; i < len; i++) {
×
NEW
95
            char c = b64.charAt(i);
×
NEW
96
            if (Bytes.isPad(c)) continue;
×
NEW
97
            if (!isB64(c)) return false;
×
98
        }
NEW
99
        return i > 0;
×
100
    }
101

102
    /**
103
     * check base64 char URLSAFE or Default, empty string means false always
104
     */
105
    public static boolean asB64(String b64, boolean urlSafe) {
NEW
106
        if (b64 == null) return false;
×
NEW
107
        final int len = b64.length();
×
NEW
108
        int i = 0;
×
NEW
109
        for (; i < len; i++) {
×
NEW
110
            char c = b64.charAt(i);
×
NEW
111
            if (Bytes.isPad(c)) continue;
×
NEW
112
            if (!isB64(c, urlSafe)) return false;
×
113
        }
NEW
114
        return i > 0;
×
115
    }
116

117
    public static java.util.Base64.Encoder getEncoder(boolean urlSafe) {
118
        return getEncoder(urlSafe, true);
1✔
119
    }
120

121
    public static java.util.Base64.Encoder getEncoder(boolean urlSafe, boolean noPad) {
122
        java.util.Base64.Encoder encoder = urlSafe ? java.util.Base64.getUrlEncoder() : java.util.Base64.getEncoder();
1✔
123
        return noPad ? encoder.withoutPadding() : encoder;
1✔
124
    }
125

126
    @NotNull
127
    public static String encode(@Nullable String str) {
128
        return encode(str, true);
1✔
129
    }
130

131
    @NotNull
132
    public static String encode(@Nullable InputStream ins) {
133
        return encode(ins, true);
×
134
    }
135

136
    @NotNull
137
    public static String encode(byte[] bytes) {
138
        return encode(bytes, true);
1✔
139
    }
140

141
    @NotNull
142
    public static String encode(@Nullable String str, boolean urlSafe) {
143
        if (str == null) return Null.Str;
1✔
144
        return getEncoder(urlSafe).encodeToString(str.getBytes(UTF_8));
1✔
145
    }
146

147
    @NotNull
148
    public static String encode(@Nullable InputStream ins, boolean urlSafe) {
149
        if (ins == null) return Null.Str;
×
150
        byte[] bytes = InputStreams.readBytes(ins);
×
151
        return getEncoder(urlSafe).encodeToString(bytes);
×
152
    }
153

154
    @NotNull
155
    public static String encode(byte[] bytes, boolean urlSafe) {
156
        if (bytes == null) return Null.Str;
1✔
157
        return getEncoder(urlSafe).encodeToString(bytes);
1✔
158
    }
159

160
    @NotNull
161
    public static String de2str(@Nullable String str) {
162
        if (str == null) return Null.Str;
1✔
163
        byte[] bytes = decode(str.getBytes(UTF_8));
1✔
164
        return new String(bytes, UTF_8);
1✔
165
    }
166

167
    @NotNull
168
    public static String de2str(byte[] bytes) {
169
        if (bytes == null) return Null.Str;
×
170
        byte[] res = decode(bytes);
×
171
        return new String(res, UTF_8);
×
172
    }
173

174
    @NotNull
175
    public static String de2str(@Nullable InputStream ins) {
176
        if (ins == null) return Null.Str;
×
177
        byte[] bytes = InputStreams.readBytes(ins);
×
178
        byte[] res = decode(bytes);
×
179
        return new String(res, UTF_8);
×
180
    }
181

182
    public static byte @NotNull [] decode(@Nullable String str) {
183
        if (str == null) return Null.Bytes;
1✔
184
        return decode(str.getBytes(UTF_8));
1✔
185
    }
186

187
    public static byte @NotNull [] decode(byte[] bytes) {
188
        if (bytes == null) return Null.Bytes;
1✔
189
        boolean urlSafe = true;
1✔
190
        for (byte c : bytes) {
1✔
191
            if (c == '+' || c == '/') {
1✔
192
                urlSafe = false;
×
193
                break;
×
194
            }
195
        }
196
        java.util.Base64.Decoder decoder = urlSafe
1✔
197
            ? java.util.Base64.getUrlDecoder()
1✔
198
            : java.util.Base64.getDecoder();
1✔
199
        return decoder.decode(bytes);
1✔
200
    }
201

202
    public static byte @NotNull [] decode(@Nullable InputStream ins) {
203
        if (ins == null) return Null.Bytes;
×
204
        byte[] bytes = InputStreams.readBytes(ins);
×
205
        return decode(bytes);
×
206
    }
207

208
    public static String pad(String b64) {
209
        if (b64 == null) return Null.Str;
1✔
210
        switch (b64.length() % 4) {
1✔
211
            case 1:
212
            case 3:
213
                return b64 + "=";
1✔
214
            case 2:
215
                return b64 + "==";
1✔
216
            default:
217
                return b64;
1✔
218
        }
219
    }
220
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc