encodeToAppendable

fun <A : Appendable> encodeToAppendable(source: ByteArray, destination: A, startIndex: Int = 0, endIndex: Int = source.size): A(source)

Encodes bytes from the specified source array or its subrange and appends resulting symbols to the destination appendable. Returns the destination appendable.

Whether the encoding result is padded with '=' depends on the PaddingOption set for this Base64 instance.

Since Kotlin

1.8

Return

the destination appendable.

Parameters

source

the array to encode bytes from.

destination

the appendable to append symbols to.

startIndex

the beginning (inclusive) of the subrange to encode, 0 by default.

endIndex

the end (exclusive) of the subrange to encode, size of the source array by default.

Throws

when startIndex or endIndex is out of range of source array indices.

when startIndex > endIndex.

Samples

import kotlin.io.encoding.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val data = byteArrayOf(-1, 0, -2, 0, -3)

val encoded = buildString {
    append("{ \"data\": \"")
    Base64.encodeToAppendable(data, destination = this)
    append("\" }")
}
println(encoded) // { \"data\": \"/wD+AP0=\" }

val encodedFromSubRange = buildString {
    append("{ \"data\": \"")
    Base64.encodeToAppendable(data, destination = this, startIndex = 1, endIndex = 3)
    append("\" }")
}
println(encodedFromSubRange) // { \"data\": \"AP4=\" } 
   //sampleEnd
}