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
/// Utility class providing common operations for byte manipulation and conversions.
12
/// This class is not intended to be instantiated.
13
public class ByteUtils {
14
15
    private ByteUtils() {
16
        throw new OfficeStamperException("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/ByteUtils::sha1b64 → KILLED
        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/ByteUtils::findDigest → KILLED
            return MessageDigest.getInstance("SHA-1");
34
        } catch (NoSuchAlgorithmException e) {
35
            throw new OfficeStamperException(e);
36
        }
37
    }
38
39
    /// Converts the size of a byte array into a human-readable string representation
40
    /// using standard size prefixes (e.g., KB, MB, GB).
41
    ///
42
    /// @param imageBytes the input byte array whose size needs to be converted
43
    /// @return a human-readable string representing the size of the byte array
44
    ///         in appropriate units (e.g., "1.2KB", "3.4MB")
45
    public static String readableSize(byte[] imageBytes) {
46
        double size = imageBytes.length;
47
        var prefixes = new StringCharacterIterator(" kMGTPE");
48 2 1. readableSize : changed conditional boundary → SURVIVED
2. readableSize : negated conditional → KILLED
        while (size >= 1_000) {
49 1 1. readableSize : Replaced double division with multiplication → TIMED_OUT
            size /= 1_000;
50
            prefixes.next();
51
        }
52 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());
53
    }
54
}

Mutations

28

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:#30]
replaced return value with "" for pro/verron/officestamper/utils/ByteUtils::sha1b64 → KILLED

33

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:#30]
replaced return value with null for pro/verron/officestamper/utils/ByteUtils::findDigest → KILLED

48

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:#30]
negated conditional → KILLED

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

49

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

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:#30]
replaced return value with "" for pro/verron/officestamper/utils/ByteUtils::readableSize → KILLED

Active mutators

Tests examined


Report generated by PIT 1.21.0