Image.java

1
package pro.verron.officestamper.preset;
2
3
import org.jspecify.annotations.Nullable;
4
import pro.verron.officestamper.api.OfficeStamperException;
5
6
import java.io.ByteArrayInputStream;
7
import java.io.IOException;
8
import java.io.InputStream;
9
import java.util.Optional;
10
11
import static java.util.Optional.ofNullable;
12
13
/// This class describes an image, which will be inserted into a document.
14
///
15
/// @author Joseph Verron
16
/// @author Romster
17
/// @since 1.0.0
18
public final class Image {
19
    private final InputStream source;
20
    private final @Nullable Integer maxWidth;
21
    private final String filenameHint;
22
    private final String altText;
23
    private byte @Nullable [] bytes = null;
24
25
    /// Constructor for Image.
26
    ///
27
    /// @param source - content of the image as InputStream
28
    ///
29
    /// @throws IOException if any.
30
    public Image(InputStream source)
31
            throws IOException {
32
        this(source, null);
33
    }
34
35
    /// Constructor for Image.
36
    ///
37
    /// @param source   content of the image as InputStream
38
    /// @param maxWidth max width of the image in twip
39
    public Image(InputStream source, @Nullable Integer maxWidth) {
40
        this(source, maxWidth, "dummyFileName", "dummyAltText");
41
    }
42
43
    /// Constructor for Image.
44
    ///
45
    /// @param source       content of the image as InputStream
46
    /// @param maxWidth     max width of the image in twip
47
    /// @param filenameHint filename hint for the image.
48
    /// @param altText      alternative text for the image.
49
    public Image(
50
            InputStream source,
51
            @Nullable Integer maxWidth,
52
            String filenameHint,
53
            String altText
54
    ) {
55
        this.source = source;
56
        this.maxWidth = maxWidth;
57
        this.filenameHint = filenameHint;
58
        this.altText = altText;
59
    }
60
61
    /// Constructor for Image.
62
    ///
63
    /// @param imageBytes - content of the image as an array of the bytes
64
    public Image(byte[] imageBytes) {
65
        this(new ByteArrayInputStream(imageBytes), null);
66
    }
67
68
    /// Constructor for Image.
69
    ///
70
    /// @param imageBytes - content of the image as an array of the bytes
71
    /// @param maxWidth   - max width of the image in twip
72
    public Image(byte[] imageBytes, @Nullable Integer maxWidth) {
73
        this(imageBytes, maxWidth, "dummyFileName", "dummyAltText");
74
    }
75
76
    /// Constructor for Image.
77
    ///
78
    /// @param imageBytes   content of the image as an array of the bytes
79
    /// @param maxWidth     max width of the image in twip
80
    /// @param filenameHint filename hint for the image.
81
    /// @param altText      alternative text for the image.
82
    public Image(
83
            byte[] imageBytes,
84
            @Nullable Integer maxWidth,
85
            String filenameHint,
86
            String altText
87
    ) {
88
        var inputStream = new ByteArrayInputStream(imageBytes);
89
        this(inputStream, maxWidth, filenameHint, altText);
90
    }
91
92
    /// Returns the byte content of the image.
93
    ///
94
    /// The bytes are lazily cached from the source [InputStream] on first
95
    /// access. Subsequent calls return the cached bytes without re-reading the
96
    /// stream.
97
    ///
98
    /// @return the byte content of the image
99
    ///
100
    /// @throws OfficeStamperException if the image bytes cannot be read from
101
    ///  the source stream
102
    public synchronized byte[] getBytes() {
103 1 1. getBytes : negated conditional → KILLED
        if (bytes == null) try (InputStream source = this.source) {
104
            bytes = source.readAllBytes();
105
        } catch (IOException e) {
106
            throw new OfficeStamperException("Failed to cache the image bytes",
107
                    e);
108
        }
109 1 1. getBytes : replaced return value with null for pro/verron/officestamper/preset/Image::getBytes → KILLED
        return bytes;
110
    }
111
112
    /// Returns the alternative text for the image.
113
    ///
114
    /// @return the alternative text
115
    public String getAltText() {
116 1 1. getAltText : replaced return value with "" for pro/verron/officestamper/preset/Image::getAltText → SURVIVED
        return altText;
117
    }
118
119
    /// Returns the filename hint for the image.
120
    ///
121
    /// @return the filename hint
122
    public String getFilenameHint() {
123 1 1. getFilenameHint : replaced return value with "" for pro/verron/officestamper/preset/Image::getFilenameHint → SURVIVED
        return filenameHint;
124
    }
125
126
    /// Returns the maximum width of the image in twip, if specified.
127
    ///
128
    /// @return an [Optional] containing the maximum width in twip, or empty if
129
    /// not specified
130
    public Optional<Integer> getMaxWidth() {
131 1 1. getMaxWidth : replaced return value with Optional.empty for pro/verron/officestamper/preset/Image::getMaxWidth → KILLED
        return ofNullable(maxWidth);
132
    }
133
}

Mutations

103

1.1
Location : getBytes
Killed by : pro.verron.officestamper.test.PptxImageTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.PptxImageTest]/[method:testImageStamping()]
negated conditional → KILLED

109

1.1
Location : getBytes
Killed by : pro.verron.officestamper.test.PptxImageTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.PptxImageTest]/[method:testImageStamping()]
replaced return value with null for pro/verron/officestamper/preset/Image::getBytes → KILLED

116

1.1
Location : getAltText
Killed by : none
replaced return value with "" for pro/verron/officestamper/preset/Image::getAltText → SURVIVED
Covering tests

123

1.1
Location : getFilenameHint
Killed by : none
replaced return value with "" for pro/verron/officestamper/preset/Image::getFilenameHint → SURVIVED
Covering tests

131

1.1
Location : getMaxWidth
Killed by : pro.verron.officestamper.test.ImageTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ImageTests]/[test-template:jpgImageReplacementInGlobalParagraphsTestWithMaxWidth(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#1]
replaced return value with Optional.empty for pro/verron/officestamper/preset/Image::getMaxWidth → KILLED

Active mutators

Tests examined


Report generated by PIT 1.25.5 support