StreamConverters.fromJavaStream

Create a source that wraps a Java 8 java.util.stream.Stream.

Additional Sink and Source converters

Signature

StreamConvertersStreamConverters

Example

Here is an example of a SourceSource created from a java.util.stream.Stream.

Scala
sourceimport java.util.stream
import java.util.stream.IntStream

import org.apache.pekko
import pekko.NotUsed
import pekko.stream.scaladsl.Keep
import pekko.stream.scaladsl.Sink
import pekko.stream.scaladsl.Source
import pekko.stream.scaladsl.StreamConverters
def factory(): IntStream = IntStream.rangeClosed(0, 9)
val source: Source[Int, NotUsed] = StreamConverters.fromJavaStream(() => factory()).map(_.intValue())
val sink: Sink[Int, Future[immutable.Seq[Int]]] = Sink.seq[Int]

val futureInts: Future[immutable.Seq[Int]] = source.toMat(sink)(Keep.right).run()
Java
sourceimport org.apache.pekko.japi.function.Creator;
import org.apache.pekko.stream.Materializer;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.stream.javadsl.StreamConverters;

import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.BaseStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Creator<BaseStream<Integer, IntStream>> creator = () -> IntStream.rangeClosed(0, 9);
Source<Integer, NotUsed> source = StreamConverters.fromJavaStream(creator);

Sink<Integer, CompletionStage<Integer>> sink = Sink.last();

CompletionStage<Integer> integerCompletionStage = source.runWith(sink, system);