StreamConverters.asOutputStream

Create a source that materializes into an OutputStream.

Additional Sink and Source converters

Signature

StreamConverters.asOutputStreamStreamConverters.asOutputStream

Description

Create a source that materializes into an OutputStream. When bytes are written to the OutputStream they are emitted from the source.

The OutputStream will no longer be writable when the Source has been canceled from its downstream, and closing the OutputStream will complete the Source.

Reactive Streams semantics

emits when bytes are written to the OutputStream

completes when the OutputStream is closed

Example

Here is an example of a SourceSource that materializes into a java.io.OutputStream, and is connected to a Sink which concatenates the incoming ByteStringByteStrings

Scala
sourceval source: Source[ByteString, OutputStream] = StreamConverters.asOutputStream()
val sink: Sink[ByteString, Future[ByteString]] = Sink.fold[ByteString, ByteString](ByteString.empty)(_ ++ _)

val (outputStream, result): (OutputStream, Future[ByteString]) =
  source.toMat(sink)(Keep.both).run()
Java
sourcefinal Source<ByteString, OutputStream> source = StreamConverters.asOutputStream();
final Sink<ByteString, CompletionStage<ByteString>> sink =
    Sink.fold(emptyByteString(), (ByteString arg1, ByteString arg2) -> arg1.concat(arg2));

final Pair<OutputStream, CompletionStage<ByteString>> output =
    source.toMat(sink, Keep.both()).run(system);
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.