encodeIntoByteArray

fun encodeIntoByteArray(source: ByteArray, destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = source.size): Int(source)

Encodes bytes from the specified source array or its subrange and writes resulting symbols into the destination array. Returns the number of symbols written.

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

Since Kotlin

1.8

Return

the number of symbols written into destination array.

Parameters

source

the array to encode bytes from.

destination

the array to write symbols into.

destinationOffset

the starting index in the destination array to write symbols to, 0 by default.

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 the resulting symbols don't fit into the destination array starting at the specified destinationOffset, or when that index is out of the destination array indices range.

when startIndex > endIndex.

Samples

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

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

var bufferPosition = 0
// encode data into buffer using Base64 encoding
// and keep track of the number of bytes written
bufferPosition += Base64.encodeIntoByteArray(data, outputBuffer)
println(outputBuffer.decodeToString(endIndex = bufferPosition)) // /wD+AP0=

outputBuffer[bufferPosition++] = '|'.code.toByte()

// encode data subrange to the buffer, writing it from the given offset
bufferPosition += Base64.encodeIntoByteArray(
    data,
    destination = outputBuffer,
    destinationOffset = bufferPosition,
    startIndex = 1,
    endIndex = 3
)
println(outputBuffer.decodeToString(endIndex = bufferPosition)) // /wD+AP0=|AP4= 
   //sampleEnd
}