The reader should be closed when a Stream
is used in a try-with-resources.
Given this:
try(Stream<String> lines = new BufferedReader(reader).lines()) {
return lines.map(it -> trim ? it.trim() : it)
.collect(Collectors.toList());
}
... the reader is not being closed??
This test fails:
AtomicBoolean closed = new AtomicBoolean(false);
Reader r = new StringReader(" Line1 \n Line2") {
@Override
public void close() {
super.close();
closed.set(true);
}
};
try(Stream<String> lines = new BufferedReader(r).lines()) {
lines.map(it -> trim ? it.trim() : it)
.collect(Collectors.toList());
}
assertTrue("Reader was not closed.",closed.get());
Stream
implementAutocloseble
? – Adcock