ByteUtils.java

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

Mutations

30

1.1
Location : sha1b64
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#12]
replaced return value with "" for pro/verron/officestamper/utils/ByteUtils::sha1b64 → KILLED

35

1.1
Location : findDigest
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#12]
replaced return value with null for pro/verron/officestamper/utils/ByteUtils::findDigest → KILLED

52

1.1
Location : readableSize
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#12]
negated conditional → KILLED

2.2
Location : readableSize
Killed by : none
changed conditional boundary → SURVIVED
Covering tests

53

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

56

1.1
Location : readableSize
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#12]
replaced return value with "" for pro/verron/officestamper/utils/ByteUtils::readableSize → KILLED

Active mutators

Tests examined


Report generated by PIT 1.20.0