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

javadev / underscore-java / #3210

pending completion
#3210

push

web-flow
Bump maven-project-info-reports-plugin from 3.4.4 to 3.4.5

4359 of 4359 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-2023 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.nio.charset.StandardCharsets;
27
import java.util.HashMap;
28
import java.util.Map;
29

30
public final class Base32 {
31

32
    private static final Base32 INSTANCE = new Base32();
1✔
33

34
    private final char[] digits;
35
    private final int mask;
36
    private final int shift;
37
    private final Map<Character, Integer> charMap;
38

39
    private Base32() {
1✔
40
        digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef".toCharArray();
1✔
41
        mask = digits.length - 1;
1✔
42
        shift = Integer.numberOfTrailingZeros(digits.length);
1✔
43
        charMap = new HashMap<>();
1✔
44
        for (int index = 0; index < digits.length; index += 1) {
1✔
45
            charMap.put(digits[index], index);
1✔
46
        }
47
    }
1✔
48

49
    public static String decode(final String encoded) {
50
        return new String(INSTANCE.decodeInternal(encoded), StandardCharsets.UTF_8);
1✔
51
    }
52

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

78
    public static String encode(final String data) {
79
        return INSTANCE.encodeInternal(data.getBytes(StandardCharsets.UTF_8));
1✔
80
    }
81

82
    private String encodeInternal(final byte[] data) {
83
        if (data.length == 0) {
1✔
84
            return "";
1✔
85
        }
86

87
        int outputLength = (data.length * 8 + shift - 1) / shift;
1✔
88
        StringBuilder result = new StringBuilder(outputLength);
1✔
89

90
        int buffer = data[0];
1✔
91
        int next = 1;
1✔
92
        int bitsLeft = 8;
1✔
93
        while (bitsLeft > 0 || next < data.length) {
1✔
94
            if (bitsLeft < shift) {
1✔
95
                if (next < data.length) {
1✔
96
                    buffer <<= 8;
1✔
97
                    buffer = buffer | (data[next++] & 0xff);
1✔
98
                    bitsLeft += 8;
1✔
99
                } else {
100
                    int pad = shift - bitsLeft;
1✔
101
                    buffer <<= pad;
1✔
102
                    bitsLeft += pad;
1✔
103
                }
104
            }
105
            int index = mask & (buffer >> (bitsLeft - shift));
1✔
106
            bitsLeft -= shift;
1✔
107
            result.append(digits[index]);
1✔
108
        }
1✔
109
        return result.toString();
1✔
110
    }
111

112
    public static class DecodingException extends RuntimeException {
113

114
        public DecodingException(final String message) {
115
            super(message);
1✔
116
        }
1✔
117
    }
118
}
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