Skip to content

Commit b67c308

Browse files
authored
Each mapped stream must be closed after its contents have been placed… (#9949)
… downstream
1 parent e43a589 commit b67c308

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

user/super/com/google/gwt/emul/java/util/stream/Collectors.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,14 @@ public static Collector<CharSequence,?,String> joining(CharSequence delimiter) {
177177
return new CollectorImpl<>(
178178
downstream.supplier(),
179179
(A a, T t) -> {
180-
Stream<? extends U> stream = mapper.apply(t);
180+
try (Stream<? extends U> stream = mapper.apply(t)) {
181181
if (stream == null) {
182182
return;
183183
}
184184
stream.forEach(u -> {
185185
downstream.accumulator().accept(a, u);
186186
});
187+
}
187188
},
188189
downstream.combiner(),
189190
downstream.finisher()

user/test/com/google/gwt/emultest/java9/util/stream/CollectorsTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public void testFlatMapping() {
4949
return items.stream();
5050
}, toList());
5151
applyItems(Arrays.asList("a"), flatMappingToNull, Arrays.asList("a"), Arrays.asList("b", "c"));
52+
53+
int[] calledCount = {0};
54+
Stream<String> mapped = Stream.of("x").onClose(() -> calledCount[0]++);
55+
Stream.of(1).collect(flatMapping(x -> mapped, toList()));
56+
assertEquals(1, calledCount[0]);
5257
}
5358

5459
public void testFiltering() {

0 commit comments

Comments
 (0)