encodingWith
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.
Since Kotlin
1.8See also
Samples
import java.io.*
import kotlin.io.encoding.*
fun main() {
//sampleStart
ByteArrayOutputStream().also { out ->
out.encodingWith(Base64.Default).use {
it.write("Hello World!!".encodeToByteArray())
}
println(out.toString()) // SGVsbG8gV29ybGQhIQ==
}
//sampleEnd
}