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

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/exist-core/src/main/java/org/exist/util/BinaryValueInputSource.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 */
24
package org.exist.util;
25

26
import org.apache.commons.io.output.CountingOutputStream;
27
import org.apache.commons.io.output.NullOutputStream;
28
import org.apache.logging.log4j.LogManager;
29
import org.apache.logging.log4j.Logger;
30
import org.exist.xquery.value.BinaryValue;
31

32
import java.io.IOException;
33
import java.io.InputStream;
34
import java.io.Reader;
35
import java.util.Optional;
36

37
/**
38
 * Input Source for a Binary Value.
39
 *
40
 * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
41
 */
42
public class BinaryValueInputSource extends EXistInputSource {
43
    private final static Logger LOG = LogManager.getLogger(BinaryValueInputSource.class);
×
44

45
    private Optional<BinaryValue> binaryValue = Optional.empty();
×
46
    private Optional<InputStream> inputStream = Optional.empty();
×
47
    private long length = -1;
×
48

49
    /**
50
     * Constructor which calls {@link #setBinaryValue(BinaryValue)}
51
     * @param binaryValue
52
     * The binaryValue passed to {@link #setBinaryValue(BinaryValue)}
53
     */
54
    public BinaryValueInputSource(final BinaryValue binaryValue) {
55
        super();
×
56
        setBinaryValue(binaryValue);
×
57
    }
×
58

59
    /**
60
     * If a binary file source has been set, the BinaryValue
61
     * object used for that is returned
62
     *
63
     * @return The BinaryValue object.
64
     */
65
    public BinaryValue getBinaryValue() {
66
        return binaryValue.orElse(null);
×
67
    }
68

69
    /**
70
     * This method sets the BinaryValue object
71
     *
72
     * @param binaryValue The BinaryValue.
73
     *
74
     * @throws IllegalStateException if the InputSource was previously closed
75
     */
76
    public void setBinaryValue(final BinaryValue binaryValue) {
77
        assertOpen();
×
78

79
        close();
×
80
        this.binaryValue = Optional.of(binaryValue);
×
81
        reOpen();
×
82
    }
×
83

84
    /**
85
     * This method was re-implemented to open a
86
     * new InputStream each time it is called.
87
     *
88
     * @return If the binaryvalue was set, and it could be opened, an InputStream object.
89
     * null, otherwise.
90
     *
91
     * @throws IllegalStateException if the InputSource was previously closed
92
     */
93
    @Override
94
    public InputStream getByteStream() {
95
        assertOpen();
×
96

97
        // close any open stream first
98
        close();
×
99

100
        if (binaryValue.isPresent()) {
×
101
            this.inputStream = Optional.of(binaryValue.get().getInputStream());
×
102
            reOpen();
×
103
            return inputStream.get();
×
104
        }
105

106
        return null;
×
107
    }
108

109
    /**
110
     * This method now does nothing, so collateral
111
     * effects from superclass with this one are avoided
112
     *
113
     * @throws IllegalStateException if the InputSource was previously closed
114
     */
115
    @Override
116
    public void setByteStream(final InputStream is) {
117
        assertOpen();
×
118
        // Nothing, so collateral effects are avoided!
119
    }
×
120

121
    /**
122
     * This method now does nothing, so collateral
123
     * effects from superclass with this one are avoided
124
     *
125
     * @throws IllegalStateException if the InputSource was previously closed
126
     */
127
    @Override
128
    public void setCharacterStream(final Reader r) {
129
        assertOpen();
×
130
        // Nothing, so collateral effects are avoided!
131
    }
×
132

133
    /**
134
     * This method now does nothing, so collateral
135
     * effects from superclass with this one are avoided
136
     *
137
     * @throws IllegalStateException if the InputSource was previously closed
138
     */
139
    @Override
140
    public void setSystemId(final String systemId) {
141
        assertOpen();
×
142
        // Nothing, so collateral effects are avoided!
143
    }
×
144

145
    /**
146
     * @see EXistInputSource#getByteStreamLength()
147
     *
148
     * @throws IllegalStateException if the InputSource was previously closed
149
     */
150
    @Override
151
    public long getByteStreamLength() {
152
        assertOpen();
×
153
        if (length == -1 && binaryValue.isPresent()) {
×
154
            try (final CountingOutputStream cos = new CountingOutputStream(NullOutputStream.NULL_OUTPUT_STREAM)) {
×
155
                binaryValue.get().streamBinaryTo(cos);
×
156
                length = cos.getByteCount();
×
157
            } catch(final IOException e) {
×
158
                LOG.error(e);
×
159
            }
160
        }
161

162
        return length;
×
163
    }
164

165
    /**
166
     * @see EXistInputSource#getSymbolicPath()
167
     *
168
     * @throws IllegalStateException if the InputSource was previously closed
169
     */
170
    @Override
171
    public String getSymbolicPath() {
172
        assertOpen();
×
173
        return null;
×
174
    }
175

176
    @Override
177
    public void close() {
178
        if(!isClosed()) {
×
179
            try {
180
                if (inputStream.isPresent()) {
×
181
                    try {
182
                        inputStream.get().close();
×
183
                    } catch (final IOException e) {
×
184
                        LOG.warn(e);
×
185
                    }
186
                    inputStream = Optional.empty();
×
187
                    length = -1;
×
188
                }
189
            } finally {
×
190
                super.close();
×
191
            }
192
        }
193
    }
×
194
}
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