alsoWriteTo

suspend fun <T> Flow<T>.alsoWriteTo(file: Path, vararg options: OpenOption = arrayOf( StandardOpenOption.WRITE, StandardOpenOption.CREATE ), mapper: suspend (T) -> ByteArray): Flow<T>

Transforms the content of this Flow of type T using the given mapper to a Flow of ByteArrays, then writes the ByteArrays to the specified file while also returning the original content.

Return

A Flow of type T representing the original content.

Example usage:

val file: Path = ...
val stringFlow: Flow<String> = ...
stringFlow
.alsoWriteTo(file) { string -> string.toByteArray() }
.collect { originalString -> ... }

Parameters

file

The file path to write to.

options

The options specifying how the file is opened. Defaults to StandardOpenOption.WRITE and StandardOpenOption.CREATE.

mapper

A function to transform items of type T to ByteArray.