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

javadev / underscore-java / #4211

04 Jun 2025 06:13PM CUT coverage: 100.0%. Remained the same
#4211

push

web-flow
Spring boot 3.5.0

4519 of 4519 relevant lines covered (100.0%)

1.0 hits per line

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

100.0
/src/main/java/com/github/underscore/Base32.java
1
/*
2
 * The MIT License (MIT)
3
 *
4
 * Copyright 2015-2025 Valentyn Kolesnikov
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
package com.github.underscore;
25

26
import java.util.HashMap;
27
import java.util.Map;
28

29
public final class Base32 {
30

31
    private static final Base32 INSTANCE = new Base32();
1✔
32
    private final char[] digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef".toCharArray();
1✔
33
    private final int mask = digits.length - 1;
1✔
34
    private final int shift = Integer.numberOfTrailingZeros(digits.length);
1✔
35
    private final Map<Character, Integer> charMap = new HashMap<>();
1✔
36

37
    private Base32() {
1✔
38
        for (int index = 0; index < digits.length; index++) {
1✔
39
            charMap.put(digits[index], index);
1✔
40
        }
41
    }
1✔
42

43
    public static String decode(final String encoded) {
44
        return new String(INSTANCE.decodeInternal(encoded));
1✔
45
    }
46

47
    private byte[] decodeInternal(final String encoded) {
48
        if (encoded.isEmpty()) {
1✔
49
            return new byte[0];
1✔
50
        }
51
        int encodedLength = encoded.length();
1✔
52
        int outLength = encodedLength * shift / 8;
1✔
53
        byte[] result = new byte[outLength];
1✔
54
        int buffer = 0;
1✔
55
        int next = 0;
1✔
56
        int bitsLeft = 0;
1✔
57
        for (char c : encoded.toCharArray()) {
1✔
58
            if (!charMap.containsKey(c)) {
1✔
59
                throw new DecodingException("Illegal character: " + c);
1✔
60
            }
61
            buffer <<= shift;
1✔
62
            buffer |= charMap.get(c) & mask;
1✔
63
            bitsLeft += shift;
1✔
64
            if (bitsLeft >= 8) {
1✔
65
                result[next++] = (byte) (buffer >> (bitsLeft - 8));
1✔
66
                bitsLeft -= 8;
1✔
67
            }
68
        }
69
        return result;
1✔
70
    }
71

72
    public static String encode(final String data) {
73
        return INSTANCE.encodeInternal(data.getBytes());
1✔
74
    }
75

76
    private String encodeInternal(final byte[] data) {
77
        if (data.length == 0) {
1✔
78
            return "";
1✔
79
        }
80
        int outputLength = (data.length * 8 + shift - 1) / shift;
1✔
81
        StringBuilder result = new StringBuilder(outputLength);
1✔
82
        int buffer = data[0];
1✔
83
        int next = 1;
1✔
84
        int bitsLeft = 8;
1✔
85
        while (bitsLeft > 0 || next < data.length) {
1✔
86
            if (bitsLeft < shift) {
1✔
87
                if (next < data.length) {
1✔
88
                    buffer <<= 8;
1✔
89
                    buffer |= (data[next++] & 0xff);
1✔
90
                    bitsLeft += 8;
1✔
91
                } else {
92
                    int pad = shift - bitsLeft;
1✔
93
                    buffer <<= pad;
1✔
94
                    bitsLeft += pad;
1✔
95
                }
96
            }
97
            int index = mask & (buffer >> (bitsLeft - shift));
1✔
98
            bitsLeft -= shift;
1✔
99
            result.append(digits[index]);
1✔
100
        }
1✔
101
        return result.toString();
1✔
102
    }
103

104
    public static class DecodingException extends RuntimeException {
105
        public DecodingException(final String message) {
106
            super(message);
1✔
107
        }
1✔
108
    }
109
}
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

© 2025 Coveralls, Inc