• 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

79.66
/exist-core/src/main/java/org/exist/util/io/FileFilterInputStreamCache.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
import java.io.RandomAccessFile;
29
import java.nio.file.Path;
30

31
/**
32
 * Cache implementation for CachingFilterInputStream Backed by a Random Access
33
 * File
34
 *
35
 * Probably slower than MemoryMappedFileFilterInputStreamCache for multiple
36
 * reads, but uses a fixed small amount of memory.
37
 *
38
 * @version 1.1
39
 *
40
 * @author <a href="mailto:adam.retter@googlemail.com">Adam Retter</a>
41
 * @author <a href="tobi.krebsATgmail.com">Tobi Krebs</a>
42
 */
43
public class FileFilterInputStreamCache extends AbstractFilterInputStreamCache {
44
    private final Path tempFile;
45
    private final boolean externalFile;
46
    private int length = 0;
1✔
47
    private int offset = 0;
1✔
48

49
    private final RandomAccessFile raf;
50

51
    public FileFilterInputStreamCache(final InputStream src) throws IOException {
52
        this(src, null);
1✔
53
    }
1✔
54

55
    public FileFilterInputStreamCache(final InputStream src, final Path f) throws IOException {
56
        super(src);
1✔
57
        if(f == null) {
1!
58
            tempFile = TemporaryFileManager.getInstance().getTemporaryFile();
1✔
59
            externalFile = false;
1✔
60
        } else {
1✔
61
            tempFile = f;
×
62
            externalFile = true;
×
63
        }
64

65
        this.raf = new RandomAccessFile(tempFile.toFile(), "rw"); //TODO(AR) consider moving to Files.newByteChannel(tempFile
1✔
66
    }
1✔
67

68
    @Override
69
    public void write(final byte[] b, final int off, final int len) throws IOException {
70
        //force writing to be append only
71
        if (offset != length) {
1!
72
            raf.seek(length);
×
73
            offset = length;
×
74
        }
75

76
        raf.write(b, off, len);
1✔
77
        length += len;
1✔
78
        offset += len;
1✔
79
    }
1✔
80

81
    @Override
82
    public void write(final int i) throws IOException {
83
        //force writing to be append only
84
        if (offset != length) {
1!
85
            raf.seek(length);
×
86
            offset = length;
×
87
        }
88

89
        raf.write(i);
1✔
90
        length++;
1✔
91
        offset++;
1✔
92
    }
1✔
93

94
    @Override
95
    public int getLength() {
96
        return length;
1✔
97
    }
98

99
    @Override
100
    public byte get(final int off) throws IOException {
101
        if (off != offset) {
1✔
102
            raf.seek(off);
1✔
103
            this.offset = off;
1✔
104
        }
105
        final byte b = raf.readByte();
1✔
106
        this.offset++;
1✔
107
        return b;
1✔
108
    }
109

110
    @Override
111
    public void copyTo(final int cacheOffset, final byte[] b, final int off, final int len) throws IOException {
112
        if (cacheOffset != offset) {
1✔
113
            raf.seek(cacheOffset);
1✔
114
            this.offset = cacheOffset;
1✔
115
        }
116
        raf.readFully(b, off, len);
1✔
117
        this.offset += len;
1✔
118
    }
1✔
119

120
    @Override
121
    public void invalidate() throws IOException {
122

123
        raf.close();
1✔
124

125
        if (tempFile != null && (!externalFile)) {
1!
126
            TemporaryFileManager.getInstance().returnTemporaryFile(tempFile);
1✔
127
        }
128
    }
1✔
129

130
    /**
131
     * Get the path of the file backing the cache.
132
     *
133
     * @return the path of the file backing the cache.
134
     */
135
    public Path getFilePath() {
136
        return tempFile;
×
137
    }
138
}
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