ByteUtils.java

1
package pro.verron.officestamper.utils.bit;
2
3
import pro.verron.officestamper.utils.UtilsException;
4
5
import java.security.MessageDigest;
6
import java.security.NoSuchAlgorithmException;
7
import java.text.StringCharacterIterator;
8
import java.util.Base64;
9
import java.util.Locale;
10
11
/// Utility class providing common operations for byte manipulation and conversions. This class is not intended to be
12
/// instantiated.
13
public class ByteUtils {
14
15
    private ByteUtils() {
16
        throw new UtilsException("Utility class shouldn't be instantiated");
17
    }
18
19
    /// Computes the SHA-1 hash of the given input bytes and encodes the result in Base64.
20
    ///
21
    /// @param bytes the input byte array to be hashed.
22
    ///
23
    /// @return the SHA-1 hash of the input bytes, encoded in Base64.
24
    public static String sha1b64(byte[] bytes) {
25
        var messageDigest = findDigest();
26
        var encoder = Base64.getEncoder();
27
        var digest = messageDigest.digest(bytes);
28 1 1. sha1b64 : replaced return value with "" for pro/verron/officestamper/utils/bit/ByteUtils::sha1b64 → NO_COVERAGE
        return encoder.encodeToString(digest);
29
    }
30
31
    private static MessageDigest findDigest() {
32
        try {
33 1 1. findDigest : replaced return value with null for pro/verron/officestamper/utils/bit/ByteUtils::findDigest → NO_COVERAGE
            return MessageDigest.getInstance("SHA-1");
34
        } catch (NoSuchAlgorithmException e) {
35
            throw new UtilsException(e);
36
        }
37
    }
38
39
    /// Converts the size of a byte array into a human-readable string representation using standard size prefixes
40
    /// (e.g., KB, MB, GB).
41
    ///
42
    /// @param length the size of the byte array to be converted
43
    ///
44
    /// @return a human-readable string representing the size of the byte array in appropriate units (e.g., "1.2KB",
45
    ///         "3.4MB")
46
    public static String readableSize(int length) {
47 2 1. readableSize : negated conditional → KILLED
2. readableSize : changed conditional boundary → KILLED
        if (length < 0)
48
            throw new IllegalArgumentException("Length must be positive");
49 2 1. readableSize : changed conditional boundary → KILLED
2. readableSize : negated conditional → KILLED
        if (length < 1000)
50 1 1. readableSize : replaced return value with "" for pro/verron/officestamper/utils/bit/ByteUtils::readableSize → KILLED
            return length + " B";
51
        double size = length;
52
        var prefixes = new StringCharacterIterator(" kMGTPE");
53 2 1. readableSize : changed conditional boundary → KILLED
2. readableSize : negated conditional → KILLED
        while (size >= 1_000) {
54 1 1. readableSize : Replaced double division with multiplication → TIMED_OUT
            size /= 1_000;
55
            prefixes.next();
56
        }
57 1 1. readableSize : replaced return value with "" for pro/verron/officestamper/utils/bit/ByteUtils::readableSize → KILLED
        return String.format(Locale.ROOT, "%.1f %cB", size, prefixes.current());
58
    }
59
}

Mutations

28

1.1
Location : sha1b64
Killed by : none
replaced return value with "" for pro/verron/officestamper/utils/bit/ByteUtils::sha1b64 → NO_COVERAGE

33

1.1
Location : findDigest
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/bit/ByteUtils::findDigest → NO_COVERAGE

47

1.1
Location : readableSize
Killed by : pro.verron.officestamper.utils.bit.ByteUtilsTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.utils.bit.ByteUtilsTest]/[test-template:readableSize(java.lang.String, int)]/[test-template-invocation:#3]
negated conditional → KILLED

2.2
Location : readableSize
Killed by : pro.verron.officestamper.utils.bit.ByteUtilsTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.utils.bit.ByteUtilsTest]/[test-template:readableSize(java.lang.String, int)]/[test-template-invocation:#1]
changed conditional boundary → KILLED

49

1.1
Location : readableSize
Killed by : pro.verron.officestamper.utils.bit.ByteUtilsTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.utils.bit.ByteUtilsTest]/[test-template:readableSize(java.lang.String, int)]/[test-template-invocation:#4]
changed conditional boundary → KILLED

2.2
Location : readableSize
Killed by : pro.verron.officestamper.utils.bit.ByteUtilsTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.utils.bit.ByteUtilsTest]/[test-template:readableSize(java.lang.String, int)]/[test-template-invocation:#3]
negated conditional → KILLED

50

1.1
Location : readableSize
Killed by : pro.verron.officestamper.utils.bit.ByteUtilsTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.utils.bit.ByteUtilsTest]/[test-template:readableSize(java.lang.String, int)]/[test-template-invocation:#3]
replaced return value with "" for pro/verron/officestamper/utils/bit/ByteUtils::readableSize → KILLED

53

1.1
Location : readableSize
Killed by : pro.verron.officestamper.utils.bit.ByteUtilsTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.utils.bit.ByteUtilsTest]/[test-template:readableSize(java.lang.String, int)]/[test-template-invocation:#4]
changed conditional boundary → KILLED

2.2
Location : readableSize
Killed by : pro.verron.officestamper.utils.bit.ByteUtilsTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.utils.bit.ByteUtilsTest]/[test-template:readableSize(java.lang.String, int)]/[test-template-invocation:#9]
negated conditional → KILLED

54

1.1
Location : readableSize
Killed by : none
Replaced double division with multiplication → TIMED_OUT

57

1.1
Location : readableSize
Killed by : pro.verron.officestamper.utils.bit.ByteUtilsTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.utils.bit.ByteUtilsTest]/[test-template:readableSize(java.lang.String, int)]/[test-template-invocation:#9]
replaced return value with "" for pro/verron/officestamper/utils/bit/ByteUtils::readableSize → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.0