decodeIntoByteArray
Decodes symbols from the specified source array or its subrange and writes resulting bytes into the destination array. Returns the number of bytes written.
The requirement, prohibition, or optionality of padding in the input symbols is determined by the PaddingOption set for this Base64 instance.
Since Kotlin
1.8Return
the number of bytes written into destination array.
Parameters
the array to decode symbols from.
the array to write bytes into.
the starting index in the destination array to write bytes to, 0 by default.
the beginning (inclusive) of the subrange to decode, 0 by default.
the end (exclusive) of the subrange to decode, size of the source array by default.
Throws
when the resulting bytes 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 the symbols for decoding are not padded as required by the PaddingOption set for this Base64 instance, or when there are extra symbols after the padding.
Samples
import kotlin.io.encoding.*
import kotlin.test.*
fun main() {
//sampleStart
val outputBuffer = ByteArray(1024)
// {data:\"ZW5jb2RlZA==\"}
val data = byteArrayOf(
123, 100, 97, 116, 97,
58, 34, 90, 87, 53, 106,
98, 50, 82, 108, 90, 65,
61, 61, 34, 125
)
val from = data.indexOf('"'.code.toByte()) + 1
val until = data.lastIndexOf('"'.code.toByte())
// decode subrange of input data into buffer and remember how many bytes were written
val bytesWritten = Base64.decodeIntoByteArray(data, destination = outputBuffer, startIndex = from, endIndex = until)
println(outputBuffer.decodeToString(endIndex = bytesWritten)) // encoded
//sampleEnd
}
Decodes symbols from the specified source char sequence or its substring and writes resulting bytes into the destination array. Returns the number of bytes written.
The requirement, prohibition, or optionality of padding in the input symbols is determined by the PaddingOption set for this Base64 instance.
Since Kotlin
1.8Return
the number of bytes written into destination array.
Parameters
the char sequence to decode symbols from.
the array to write bytes into.
the starting index in the destination array to write bytes to, 0 by default.
the beginning (inclusive) of the substring to decode, 0 by default.
the end (exclusive) of the substring to decode, length of the source by default.
Throws
when the resulting bytes 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 the symbols for decoding are not padded as required by the PaddingOption set for this Base64 instance, or when there are extra symbols after the padding.
Samples
import kotlin.io.encoding.*
import kotlin.test.*
fun main() {
//sampleStart
val outputBuffer = ByteArray(1024)
val inputChunks = listOf("Q2h1bmsx", "U2Vjb25kQ2h1bms=", "Y2h1bmsjMw==")
var bufferPosition = 0
val chunkIterator = inputChunks.iterator()
while (bufferPosition < outputBuffer.size && chunkIterator.hasNext()) {
val chunk = chunkIterator.next()
// fill buffer with data decoded from base64-encoded chunks
// and increment current position by the number of decoded bytes
bufferPosition += Base64.decodeIntoByteArray(
chunk,
destination = outputBuffer,
destinationOffset = bufferPosition
)
}
// consume outputBuffer
println(outputBuffer.decodeToString(endIndex = bufferPosition)) // Chunk1SecondChunkchunk#3
//sampleEnd
}