Class StreamConverters
- java.lang.Object
-
- akka.stream.javadsl.StreamConverters
-
public class StreamConverters extends java.lang.Object
Converters for interacting with the blockingjava.io
streams APIs and Java 8 Streams
-
-
Constructor Summary
Constructors Constructor Description StreamConverters()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Sink<ByteString,java.io.InputStream>
asInputStream()
Creates a Sink which when materialized will return anInputStream
which it is possible to read the values produced by the stream this Sink is attached to.static Sink<ByteString,java.io.InputStream>
asInputStream(java.time.Duration readTimeout)
Creates a Sink which when materialized will return anInputStream
which it is possible to read the values produced by the stream this Sink is attached to.static Sink<ByteString,java.io.InputStream>
asInputStream(scala.concurrent.duration.FiniteDuration readTimeout)
Deprecated.Use the overloaded one which accepts java.time.Duration instead.static <T> Sink<T,java.util.stream.Stream<T>>
asJavaStream()
Creates a sink which materializes into Java 8Stream
that can be run to trigger demand through the sink.
static Source<ByteString,java.io.OutputStream>
asOutputStream()
Creates a Source which when materialized will return anOutputStream
which it is possible to write the ByteStrings to the stream this Source is attached to.static Source<ByteString,java.io.OutputStream>
asOutputStream(java.time.Duration writeTimeout)
Creates a Source which when materialized will return anOutputStream
which it is possible to write the ByteStrings to the stream this Source is attached to.static Source<ByteString,java.io.OutputStream>
asOutputStream(scala.concurrent.duration.FiniteDuration writeTimeout)
Deprecated.Use the overloaded one which accepts java.time.Duration instead.static Source<ByteString,java.util.concurrent.CompletionStage<IOResult>>
fromInputStream(Creator<java.io.InputStream> in)
Creates a Source from anInputStream
created by the given function.static Source<ByteString,java.util.concurrent.CompletionStage<IOResult>>
fromInputStream(Creator<java.io.InputStream> in, int chunkSize)
Creates a Source from anInputStream
created by the given function.static <O,S extends java.util.stream.BaseStream<O,S>>
Source<O,NotUsed>fromJavaStream(Creator<java.util.stream.BaseStream<O,S>> stream)
Creates a source that wraps a Java 8Stream
.
static Sink<ByteString,java.util.concurrent.CompletionStage<IOResult>>
fromOutputStream(Creator<java.io.OutputStream> f)
Sink which writes incomingByteString
s to anOutputStream
created by the given function.static Sink<ByteString,java.util.concurrent.CompletionStage<IOResult>>
fromOutputStream(Creator<java.io.OutputStream> f, boolean autoFlush)
Sink which writes incomingByteString
s to anOutputStream
created by the given function.static <T,R>
Sink<T,java.util.concurrent.CompletionStage<R>>javaCollector(Creator<java.util.stream.Collector<T,?,R>> collector)
Creates a sink which materializes into aCompletionStage
which will be completed with a result of the Java 8
Collector
transformation and reduction operations.
static <T,R>
Sink<T,java.util.concurrent.CompletionStage<R>>javaCollectorParallelUnordered(int parallelism, Creator<java.util.stream.Collector<T,?,R>> collector)
Creates a sink which materializes into aCompletionStage
which will be completed with a result of the Java 8
Collector
transformation and reduction operations.
-
-
-
Method Detail
-
fromOutputStream
public static Sink<ByteString,java.util.concurrent.CompletionStage<IOResult>> fromOutputStream(Creator<java.io.OutputStream> f)
Sink which writes incomingByteString
s to anOutputStream
created by the given function.Materializes a
CompletionStage
ofIOResult
that 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
akka.stream.materializer.blocking-io-dispatcher
or set it for a given Source by usingActorAttributes
.This method uses no auto flush for the
OutputStream
@see#fromOutputStream(function.Creator, Boolean)
if you want to override it.The
OutputStream
will be closed when the stream flowing into thisSink
is completed. TheSink
will cancel the stream when theOutputStream
is no longer writable.- Parameters:
f
- A Creator which creates an OutputStream to write to
-
fromOutputStream
public static Sink<ByteString,java.util.concurrent.CompletionStage<IOResult>> fromOutputStream(Creator<java.io.OutputStream> f, boolean autoFlush)
Sink which writes incomingByteString
s to anOutputStream
created by the given function.Materializes a
CompletionStage
ofIOResult
that 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
akka.stream.materializer.blocking-io-dispatcher
or set it for a given Source by usingActorAttributes
.The
OutputStream
will be closed when the stream flowing into thisSink
is completed. TheSink
will cancel the stream when theOutputStream
is no longer writable.- Parameters:
f
- A Creator which creates an OutputStream to write toautoFlush
- If true the OutputStream will be flushed whenever a byte array is written
-
asInputStream
public static Sink<ByteString,java.io.InputStream> asInputStream()
Creates a Sink which when materialized will return anInputStream
which it is possible to read the values produced by the stream this Sink is attached to.This method uses a default read timeout, use
#inputStream(FiniteDuration)
or#inputStream(java.time.Duration)
to explicitly configure the timeout.This Sink is intended for inter-operation with legacy APIs since it is inherently blocking.
You can configure the internal buffer size by using
ActorAttributes
.The
InputStream
will be closed when the stream flowing into thisSink
completes, and closing theInputStream
will cancel thisSink
.
-
asInputStream
public static Sink<ByteString,java.io.InputStream> asInputStream(scala.concurrent.duration.FiniteDuration readTimeout)
Deprecated.Use the overloaded one which accepts java.time.Duration instead. Since 2.5.12.Creates a Sink which when materialized will return anInputStream
which 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
ActorAttributes
.The
InputStream
will be closed when the stream flowing into thisSink
completes, and closing theInputStream
will cancel thisSink
.- Parameters:
readTimeout
- the max time the read operation on the materialized InputStream should block
-
asInputStream
public static Sink<ByteString,java.io.InputStream> asInputStream(java.time.Duration readTimeout)
Creates a Sink which when materialized will return anInputStream
which 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
ActorAttributes
.The
InputStream
will be closed when the stream flowing into thisSink
completes, and closing theInputStream
will cancel thisSink
.- Parameters:
readTimeout
- the max time the read operation on the materialized InputStream should block
-
fromInputStream
public static Source<ByteString,java.util.concurrent.CompletionStage<IOResult>> fromInputStream(Creator<java.io.InputStream> in, int chunkSize)
Creates a Source from anInputStream
created by the given function. Emitted elements are up tochunkSize
sizedByteString
elements. The actual size of the emitted elements depends on how much data the underlyingInputStream
returns 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
akka.stream.materializer.blocking-io-dispatcher
or set it for a given Source by usingActorAttributes
.It materializes a
CompletionStage
ofIOResult
containing 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
InputStream
will be closed when theSource
is cancelled.
-
fromInputStream
public static Source<ByteString,java.util.concurrent.CompletionStage<IOResult>> fromInputStream(Creator<java.io.InputStream> in)
Creates a Source from anInputStream
created by the given function. Emitted elements are up to 8192 bytes sizedByteString
elements. The actual size of the emitted elements depends on how much data the underlyingInputStream
returns 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
akka.stream.materializer.blocking-io-dispatcher
or set it for a given Source by usingActorAttributes
.It materializes a
CompletionStage
ofIOResult
containing the number of bytes read from the source file upon completion, and a possible exception if IO operation was not completed successfully.The created
InputStream
will be closed when theSource
is cancelled.
-
asOutputStream
public static Source<ByteString,java.io.OutputStream> asOutputStream(scala.concurrent.duration.FiniteDuration writeTimeout)
Deprecated.Use the overloaded one which accepts java.time.Duration instead. Since 2.5.12.Creates a Source which when materialized will return anOutputStream
which 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
ActorAttributes
.The created
OutputStream
will be closed when theSource
is cancelled, and closing theOutputStream
will complete thisSource
.- Parameters:
writeTimeout
- the max time the write operation on the materialized OutputStream should block
-
asOutputStream
public static Source<ByteString,java.io.OutputStream> asOutputStream(java.time.Duration writeTimeout)
Creates a Source which when materialized will return anOutputStream
which 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 default dispatcher for this Source by changing the
akka.stream.materializer.blocking-io-dispatcher
or set it for a given Source by usingActorAttributes
.The created
OutputStream
will be closed when theSource
is cancelled, and closing theOutputStream
will complete thisSource
.- Parameters:
writeTimeout
- the max time the write operation on the materialized OutputStream should block
-
asOutputStream
public static Source<ByteString,java.io.OutputStream> asOutputStream()
Creates a Source which when materialized will return anOutputStream
which it is possible to write the ByteStrings to the stream this Source is attached to. The write timeout for OutputStreams materialized will default to 5 seconds, @see#outputStream(FiniteDuration)
or#outputStream(java.time.Duration)
if you want to override it.This Source is intended for inter-operation with legacy APIs since it is inherently blocking.
You can configure the internal buffer size by using
ActorAttributes
.The created
OutputStream
will be closed when theSource
is cancelled, and closing theOutputStream
will complete thisSource
.
-
asJavaStream
public static <T> Sink<T,java.util.stream.Stream<T>> asJavaStream()
Creates a sink which materializes into Java 8Stream
that can be run to trigger demand through the sink. Elements emitted through the stream will be available for reading through the Java 8
Stream
.
The Java 8
Stream
will be ended when the stream flowing into this
Sink
completes, and closing the Java
Stream
will cancel the inflow of this
Sink
.
Java 8
Stream
throws exception in case reactive stream failed.
Be aware that Java
Stream
blocks 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 the
akka.stream.blocking-io-dispatcher
.
-
fromJavaStream
public static <O,S extends java.util.stream.BaseStream<O,S>> Source<O,NotUsed> fromJavaStream(Creator<java.util.stream.BaseStream<O,S>> stream)
Creates a source that wraps a Java 8Stream
.
Source
uses a stream iterator to get all its elements and send them downstream on demand.
Example usage:
Source.fromJavaStream(() -> IntStream.rangeClosed(1, 10))
You can use
Source.async
to create asynchronous boundaries between synchronous java stream and the rest of flow.
-
javaCollector
public static <T,R> Sink<T,java.util.concurrent.CompletionStage<R>> javaCollector(Creator<java.util.stream.Collector<T,?,R>> collector)
Creates a sink which materializes into aCompletionStage
which will be completed with a result of the Java 8
Collector
transformation and reduction operations. This allows usage of Java 8 streams transformations for reactive streams. The Collector
will 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. The
Collector
can also do reduction at the end. Reduction processing is performed sequentially
Note that a flow can be materialized multiple times, so the function producing the
Collector
must be able to handle multiple invocations.
-
javaCollectorParallelUnordered
public static <T,R> Sink<T,java.util.concurrent.CompletionStage<R>> javaCollectorParallelUnordered(int parallelism, Creator<java.util.stream.Collector<T,?,R>> collector)
Creates a sink which materializes into aCompletionStage
which will be completed with a result of the Java 8
Collector
transformation and reduction operations. This allows usage of Java 8 streams transformations for reactive streams. The
Collector
will 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.
Collector
can also do reduction at the end. Reduction processing is performed in parallel based on graph
Balance
.
Note that a flow can be materialized multiple times, so the function producing the
Collector
must be able to handle multiple invocations.
-
-