encodingWith

JVM
1.8
@ExperimentalEncodingApi fun OutputStream.encodingWith(
    base64: Base64
): OutputStream

(source)

Returns an output stream that encodes bytes using the specified base64 encoding and writes the result to this output stream. Please refer to Base64 documentation for more details on the encoding itself.

The byte data written to the returned output stream is encoded using the specified base64 encoding and the resulting symbols are written to the underlying output stream. Bytes are encoded in 3-byte blocks.

The returned output stream should be closed in a timely manner. We suggest you try the use function, which closes the resource after a given block of code is executed. The close operation writes leftover symbols to the underlying output stream. Whether the leftover symbols are padded with '=' depends on the PaddingOption set for the base64 instance. Closing the returned output stream will close the underlying output stream.

import java.io.*
import kotlin.io.encoding.*

fun main(args: Array<String>) {
//sampleStart
ByteArrayOutputStream().also { out ->
    out.encodingWith(Base64.Default).use {
        it.write("Hello World!!".encodeToByteArray())
    }
    println(out.toString()) // SGVsbG8gV29ybGQhIQ==
}
//sampleEnd
}

See Also

Base64