Skip to content

Commit 0c94fb3

Browse files
acoburnlangsamu
andauthored
JCL-456: Close streams (#156)
* JCL-456: Close streams * Buffer stream in order to close resources before iterating * Add iterator buffering implementation note (#157) --------- Co-authored-by: Samu Lang <langsamu@users.noreply.github.com>
1 parent 4f73372 commit 0c94fb3

File tree

8 files changed

+64
-9
lines changed

8 files changed

+64
-9
lines changed

commons/src/main/java/com/inrupt/rdf/wrapping/commons/ObjectSet.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ public ObjectSet(
112112

113113
@Override
114114
public int size() {
115-
final long size = statements().count();
116-
return size > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) size;
115+
try (final Stream<? extends Triple> stream = statements()) {
116+
final long size = stream.count();
117+
return size > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) size;
118+
}
117119
}
118120

119121
@Override
@@ -131,14 +133,24 @@ public boolean contains(final Object o) {
131133
return graph.contains(subject, predicate, object);
132134
}
133135

136+
/**
137+
* @implNote Prior to returning the iterator, this implementation consumes (buffers) an underlying
138+
* {@link Graph#stream(BlankNodeOrIRI, IRI, RDFTerm) stream of statements} with the current {@link #subject} and
139+
* {@link #predicate} as well as the {@link #valueMapping value mapping function} applied to each object.
140+
*/
134141
@Override
135142
public Iterator<T> iterator() {
136-
return values().iterator();
143+
try (final Stream<T> stream = values()) {
144+
return stream.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList))
145+
.iterator();
146+
}
137147
}
138148

139149
@Override
140150
public Object[] toArray() {
141-
return values().toArray();
151+
try (final Stream<T> stream = values()) {
152+
return stream.toArray();
153+
}
142154
}
143155

144156
@Override
@@ -197,9 +209,11 @@ public boolean addAll(final Collection<? extends T> c) {
197209
public boolean retainAll(final Collection<?> c) {
198210
Objects.requireNonNull(c);
199211

200-
return values().collect(Collectors.toList()).stream()
212+
try (final Stream<T> stream = values()) {
213+
return stream.collect(Collectors.toList()).stream()
201214
.map(value -> removeUnlessContains(c, value))
202215
.reduce(false, EITHER);
216+
}
203217
}
204218

205219
// AbstractSet#removeAll relies on Iterator#remove, which is not supported here.

commons/src/main/java/com/inrupt/rdf/wrapping/commons/WrapperBlankNodeOrIRI.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Iterator;
2828
import java.util.Objects;
2929
import java.util.Set;
30+
import java.util.stream.Collectors;
3031
import java.util.stream.Stream;
3132

3233
import org.apache.commons.rdf.api.*;
@@ -103,7 +104,9 @@ protected <T> T anyOrNull(final IRI p, final ValueMapping<T> m) {
103104
Objects.requireNonNull(p);
104105
Objects.requireNonNull(m);
105106

106-
return objectStream(p, m).findAny().orElse(null);
107+
try (final Stream<T> stream = objectStream(p, m)) {
108+
return stream.findAny().orElse(null);
109+
}
107110
}
108111

109112
/**
@@ -183,12 +186,19 @@ protected <T> T singleOrThrow(final IRI p, final ValueMapping<T> m) {
183186
* @param <T> the type of values returned
184187
*
185188
* @return the converted objects of statements with this subject and the given predicate
189+
*
190+
* @implNote Prior to returning the iterator, this implementation consumes (buffers) an underlying
191+
* {@link Graph#stream(BlankNodeOrIRI, IRI, RDFTerm) stream of statements} with the predicate {@code p} and the
192+
* mapping function {@code m} applied to each object.
186193
*/
187194
protected <T> Iterator<T> objectIterator(final IRI p, final ValueMapping<T> m) {
188195
Objects.requireNonNull(p);
189196
Objects.requireNonNull(m);
190197

191-
return objectStream(p, m).iterator();
198+
try (final Stream<T> stream = objectStream(p, m)) {
199+
return stream.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList))
200+
.iterator();
201+
}
192202
}
193203

194204
/**
@@ -204,7 +214,9 @@ protected <T> Set<T> objectsReadOnly(final IRI p, final ValueMapping<T> m) {
204214
Objects.requireNonNull(p);
205215
Objects.requireNonNull(m);
206216

207-
return objectStream(p, m).collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
217+
try (final Stream<T> stream = objectStream(p, m)) {
218+
return stream.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
219+
}
208220
}
209221

210222
/**

jena/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<artifactId>hamcrest</artifactId>
4040
<scope>test</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.slf4j</groupId>
44+
<artifactId>slf4j-simple</artifactId>
45+
<scope>test</scope>
46+
</dependency>
4247
</dependencies>
4348

4449
<build>

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@
127127
<artifactId>guava</artifactId>
128128
<version>${guava.version}</version>
129129
</dependency>
130+
<!-- patch CVE-2024-25710 commons-compress 1.26.0 -->
131+
<dependency>
132+
<groupId>org.apache.commons</groupId>
133+
<artifactId>commons-compress</artifactId>
134+
<version>1.26.0</version>
135+
</dependency>
130136

131137
<!-- testing -->
132138
<dependency>
@@ -535,7 +541,6 @@
535541
</execution>
536542
</executions>
537543
<configuration>
538-
<cveValidForHours>24</cveValidForHours>
539544
<failBuildOnCVSS>7</failBuildOnCVSS>
540545
<formats>
541546
<format>HTML</format>

rdf4j/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
<version>${project.version}</version>
4444
<scope>test</scope>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.slf4j</groupId>
48+
<artifactId>slf4j-simple</artifactId>
49+
<scope>test</scope>
50+
</dependency>
4651
</dependencies>
4752

4853
<build>

test/commons/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
<groupId>org.mockito</groupId>
3636
<artifactId>mockito-core</artifactId>
3737
</dependency>
38+
<dependency>
39+
<groupId>org.slf4j</groupId>
40+
<artifactId>slf4j-simple</artifactId>
41+
</dependency>
3842
</dependencies>
3943

4044
<build>

test/jena/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<artifactId>jena-commonsrdf</artifactId>
3535
<scope>test</scope>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.slf4j</groupId>
39+
<artifactId>slf4j-simple</artifactId>
40+
<scope>test</scope>
41+
</dependency>
3742
</dependencies>
3843

3944
<build>

test/rdf4j/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
<artifactId>rdf4j-repository-sail</artifactId>
4747
<scope>test</scope>
4848
</dependency>
49+
<dependency>
50+
<groupId>org.slf4j</groupId>
51+
<artifactId>slf4j-simple</artifactId>
52+
<scope>test</scope>
53+
</dependency>
4954
</dependencies>
5055

5156
<build>

0 commit comments

Comments
 (0)