Class StreamConverters
- java.lang.Object
-
- org.apache.pekko.stream.scaladsl.StreamConverters
-
public class StreamConverters extends java.lang.ObjectConverters for interacting with the blockingjava.iostreams APIs and Java 8 Streams
-
-
Constructor Summary
Constructors Constructor Description StreamConverters()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Sink<ByteString,java.io.InputStream>asInputStream(scala.concurrent.duration.FiniteDuration readTimeout)Creates a Sink which when materialized will return anInputStreamwhich it is possible to read the values produced by the stream this Sink is attached to.static scala.concurrent.duration.FiniteDurationasInputStream$default$1()static <T> Sink<T,java.util.stream.Stream<T>>asJavaStream()Creates a sink which materializes into Java 8Streamthat can be run to trigger demand through the sink.static Source<ByteString,java.io.OutputStream>asOutputStream(scala.concurrent.duration.FiniteDuration writeTimeout)Creates a Source which when materialized will return anOutputStreamwhich it is possible to write the ByteStrings to the stream this Source is attached to.static scala.concurrent.duration.FiniteDurationasOutputStream$default$1()static Source<ByteString,scala.concurrent.Future<IOResult>>fromInputStream(scala.Function0<java.io.InputStream> in, int chunkSize)Creates a Source from anInputStreamcreated by the given function.static intfromInputStream$default$2()static <T,S extends java.util.stream.BaseStream<T,S>>
Source<T,NotUsed>fromJavaStream(scala.Function0<java.util.stream.BaseStream<T,S>> stream)Creates a source that wraps a Java 8Stream.static Sink<ByteString,scala.concurrent.Future<IOResult>>fromOutputStream(scala.Function0<java.io.OutputStream> out, boolean autoFlush)Creates a Sink which writes incomingByteStrings to anOutputStreamcreated by the given function.static booleanfromOutputStream$default$2()static <T,R>
Sink<T,scala.concurrent.Future<R>>javaCollector(scala.Function0<java.util.stream.Collector<T,?,R>> collectorFactory)Creates a sink which materializes into aFuturewhich will be completed with result of the Java 8Collectortransformation and reduction operations.static <T,R>
Sink<T,scala.concurrent.Future<R>>javaCollectorParallelUnordered(int parallelism, scala.Function0<java.util.stream.Collector<T,?,R>> collectorFactory)Creates a sink which materializes into aFuturewhich will be completed with result of the Java 8Collectortransformation and reduction operations.
-
-
-
Method Detail
-
fromInputStream
public static Source<ByteString,scala.concurrent.Future<IOResult>> fromInputStream(scala.Function0<java.io.InputStream> in, int chunkSize)
Creates a Source from anInputStreamcreated by the given function. Emitted elements are up tochunkSizesizedpekko.util.ByteStringelements. The actual size of the emitted elements depends on how much data the underlyingInputStreamreturns on each read invocation. Such chunks will never be larger than chunkSize though.You can configure the default dispatcher for this Source by changing the
pekko.stream.materializer.blocking-io-dispatcheror set it for a given Source by usingpekko.stream.ActorAttributes.It materializes a
FutureofIOResultcontaining the number of bytes read from the source file upon completion, and a possible exception if IO operation was not completed successfully. Note that bytes having been read by the source does not give any guarantee that the bytes were seen by downstream stages.The created
InputStreamwill be closed when theSourceis cancelled.- Parameters:
in- a function which creates the InputStream to read fromchunkSize- the size of each read operation, defaults to 8192
-
fromInputStream$default$2
public static int fromInputStream$default$2()
-
asOutputStream
public static Source<ByteString,java.io.OutputStream> asOutputStream(scala.concurrent.duration.FiniteDuration writeTimeout)
Creates a Source which when materialized will return anOutputStreamwhich it is possible to write the ByteStrings to the stream this Source is attached to.This Source is intended for inter-operation with legacy APIs since it is inherently blocking.
You can configure the internal buffer size by using
pekko.stream.ActorAttributes.The created
OutputStreamwill be closed when theSourceis cancelled, and closing theOutputStreamwill complete thisSource.- Parameters:
writeTimeout- the max time the write operation on the materialized OutputStream should block, defaults to 5 seconds
-
asOutputStream$default$1
public static scala.concurrent.duration.FiniteDuration asOutputStream$default$1()
-
fromOutputStream
public static Sink<ByteString,scala.concurrent.Future<IOResult>> fromOutputStream(scala.Function0<java.io.OutputStream> out, boolean autoFlush)
Creates a Sink which writes incomingByteStrings to anOutputStreamcreated by the given function.Materializes a
FutureofIOResultthat will be completed with the size of the file (in bytes) at the streams completion, and a possible exception if IO operation was not completed successfully.You can configure the default dispatcher for this Source by changing the
pekko.stream.materializer.blocking-io-dispatcheror set it for a given Source by usingpekko.stream.ActorAttributes. IfautoFlushis true the OutputStream will be flushed whenever a byte array is written, defaults to false.The
OutputStreamwill be closed when the stream flowing into thisSinkis completed. TheSinkwill cancel the stream when theOutputStreamis no longer writable.
-
fromOutputStream$default$2
public static boolean fromOutputStream$default$2()
-
asInputStream
public static Sink<ByteString,java.io.InputStream> asInputStream(scala.concurrent.duration.FiniteDuration readTimeout)
Creates a Sink which when materialized will return anInputStreamwhich it is possible to read the values produced by the stream this Sink is attached to.This Sink is intended for inter-operation with legacy APIs since it is inherently blocking.
You can configure the internal buffer size by using
pekko.stream.ActorAttributes.The
InputStreamwill be closed when the stream flowing into thisSinkcompletes, and closing theInputStreamwill cancel thisSink.- Parameters:
readTimeout- the max time the read operation on the materialized InputStream should block
-
asInputStream$default$1
public static scala.concurrent.duration.FiniteDuration asInputStream$default$1()
-
javaCollector
public static <T,R> Sink<T,scala.concurrent.Future<R>> javaCollector(scala.Function0<java.util.stream.Collector<T,?,R>> collectorFactory)
Creates a sink which materializes into aFuturewhich will be completed with result of the Java 8Collectortransformation and reduction operations. This allows usage of Java 8 streams transformations for reactive streams. TheCollectorwill trigger demand downstream. Elements emitted through the stream will be accumulated into a mutable result container, optionally transformed into a final representation after all input elements have been processed. TheCollectorcan also do reduction at the end. Reduction processing is performed sequentiallyNote that a flow can be materialized multiple times, so the function producing the
Collectormust be able to handle multiple invocations.
-
javaCollectorParallelUnordered
public static <T,R> Sink<T,scala.concurrent.Future<R>> javaCollectorParallelUnordered(int parallelism, scala.Function0<java.util.stream.Collector<T,?,R>> collectorFactory)
Creates a sink which materializes into aFuturewhich will be completed with result of the Java 8Collectortransformation and reduction operations. This allows usage of Java 8 streams transformations for reactive streams. TheCollectorwill trigger demand downstream. Elements emitted through the stream will be accumulated into a mutable result container, optionally transformed into a final representation after all input elements have been processed. TheCollectorcan also do reduction at the end. Reduction processing is performed in parallel based on graphBalance.Note that a flow can be materialized multiple times, so the function producing the
Collectormust be able to handle multiple invocations.
-
asJavaStream
public static <T> Sink<T,java.util.stream.Stream<T>> asJavaStream()
Creates a sink which materializes into Java 8Streamthat can be run to trigger demand through the sink. Elements emitted through the stream will be available for reading through the Java 8Stream.The Java 8
Streamwill be ended when the stream flowing into thisSinkcompletes, and closing the JavaStreamwill cancel the inflow of thisSink.If the Java 8
Streamthrows exception the Pekko stream is cancelled.Be aware that Java
Streamblocks current thread while waiting on next element from downstream. As it is interacting wit blocking API the implementation runs on a separate dispatcher configured through thepekko.stream.blocking-io-dispatcher.
-
fromJavaStream
public static <T,S extends java.util.stream.BaseStream<T,S>> Source<T,NotUsed> fromJavaStream(scala.Function0<java.util.stream.BaseStream<T,S>> stream)
Creates a source that wraps a Java 8Stream.Sourceuses a stream iterator to get all its elements and send them downstream on demand.Example usage:
StreamConverters.fromJavaStream(() => IntStream.rangeClosed(1, 10))You can use
Source.asyncto create asynchronous boundaries between synchronous JavaStreamand the rest of flow.
-
-