I have the following snippet and it generates Flowable<String>
. I'm not sure how can I make the Files.lines
Autoclosable
. I needed to pass in iterator
as the second argument to read line one by one as its consumed.
Please note that I have not used the third argument (disposeState) as in generate(Callable<S> initialState, BiConsumer<S,Emitter<T>> generator, Consumer<? super S> disposeState)
because there is no point in passing iterator
as disposeState
.
private Flowable<String> generateFlowable(File file) {
return Flowable.generate(
() -> Files.lines(Paths.get(file.toURI()), StandardCharsets.UTF_8).iterator(),
(iterator, emitter) -> {
if (iterator.hasNext()) {
emitter.onNext(iterator.next());
} else {
emitter.onComplete();
}
}
);
}
The lines are consumed and parsed one by one in the other method. When I did run lsof
I found that the handler was not closed. Can some suggest me how could we do that?