Skip to content

Instantly share code, notes, and snippets.

@GaetanoPiazzolla
Created September 18, 2021 11:40
Show Gist options
  • Save GaetanoPiazzolla/a8505cf39b1168bd20d2c0f5926d0076 to your computer and use it in GitHub Desktop.
Save GaetanoPiazzolla/a8505cf39b1168bd20d2c0f5926d0076 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import reactor.core.publisher.*;
import reactor.core.scheduler.Schedulers;
public Flux<String> searchReactiveStream() {
return Flux.create((FluxSink<String> fluxSink) -> {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(pathFile);
} catch (FileNotFoundException e) {
fluxSink.error(e);
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
Stream<String> linesStream = bufferedReader.lines();
AtomicBoolean found = new AtomicBoolean(false);
linesStream.forEach(l -> {
if (l.contains(SEARCH_TOKEN)) {
fluxSink.next(l);
found.set(true);
}
});
if (!found.get()) {
fluxSink.next("No results found");
}
fluxSink.complete();
});
}
@GaetanoPiazzolla
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment