• 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

77.42
/exist-core/src/main/java/org/exist/util/io/ByteBufferInputStream.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.io;
25

26
import java.io.IOException;
27
import java.io.InputStream;
28

29
/**
30
 * Implementation of an InputStream which reads from a ByteBuffer
31
 *
32
 * @version 1.0
33
 *
34
 * @author <a href="mailto:adam.retter@googlemail.com">Adam Retter</a>
35
 */
36
public class ByteBufferInputStream extends InputStream {
37

38
    private final ByteBufferAccessor bufAccessor;
39
    private boolean closed = false;
1✔
40
    private final static int END_OF_STREAM = -1;
41

42
    public ByteBufferInputStream(final ByteBufferAccessor bufAccessor) {
1✔
43
        this.bufAccessor = bufAccessor;
1✔
44
    }
1✔
45

46
    @Override
47
    public int available() throws IOException {
48
        int available = 0;
1✔
49

50
        if(!closed) {
1✔
51
            available = bufAccessor.getBuffer().capacity() - bufAccessor.getBuffer().position();
1✔
52
        }
53

54
        return available;
1✔
55
    }
56
    
57
    @Override
58
    public int read() throws IOException {
59
        isClosed();
1✔
60
        
61
        if(available() == 0) {
1✔
62
            return END_OF_STREAM;
1✔
63
        }
64

65
        return bufAccessor.getBuffer().get();
1✔
66
    }
67

68
    @Override
69
    public int read(final byte[] b) throws IOException {
70
        isClosed();
1✔
71

72
        if(available() == 0) {
1✔
73
            return END_OF_STREAM;
1✔
74
        } else if(b.length > available()) {
1✔
75
            return read(b, 0, available());
1✔
76
        } else {
77
            final int currentPosition = bufAccessor.getBuffer().position();
1✔
78
            return bufAccessor.getBuffer().get(b).position() - currentPosition;
1✔
79
        }
80
    }
81

82
    @Override
83
    public int read(final byte[] b, final int off, int len) throws IOException {
84
        isClosed();
1✔
85

86
        if(available() == 0) {
1✔
87
            return END_OF_STREAM;
1✔
88
        }
89
        
90
        if(len > available()) {
1✔
91
            len = available();
1✔
92
        }
93

94
        final int currentPosition = bufAccessor.getBuffer().position();
1✔
95
        return bufAccessor.getBuffer().get(b, off, len).position() - currentPosition;
1✔
96
    }
97

98
    @Override
99
    public boolean markSupported() {
100
        return true;
1✔
101
    }
102

103
    @Override
104
    public synchronized void mark(final int i) {
105
        bufAccessor.getBuffer().mark();
×
106
    }
×
107

108
    @Override
109
    public synchronized void reset() throws IOException {
110
        bufAccessor.getBuffer().reset();
×
111
    }
×
112

113
    @Override
114
    public long skip(long l) throws IOException {
115

116
        if(l > available()) {
×
117
            l = available();
×
118
        }
119

120
        long newPosition = bufAccessor.getBuffer().position();
×
121
        newPosition += l;
×
122
        try {
123
            bufAccessor.getBuffer().position((int)newPosition);
×
124
        } catch(final IllegalArgumentException iae) {
×
125
            throw new IOException("Unable to skip " + l + " bytes", iae);
×
126
        }
127

128
        return l;
×
129
    }
130

131
    @Override
132
    public void close() throws IOException {
133

134
        isClosed();
1✔
135

136
        bufAccessor.getBuffer().clear();
1✔
137
        closed = true;
1✔
138
    }
1✔
139

140
    private void isClosed() throws IOException {
141
        if(closed) {
1✔
142
            throw new IOException("The stream was previously closed");
1✔
143
        }
144
    }
1✔
145

146
}
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