decode
Decodes symbols from the specified source array or its subrange. Returns a ByteArray containing the resulting bytes.
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
a ByteArray with the resulting bytes.
Parameters
the array to decode symbols from.
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 startIndex or endIndex is out of range of source array indices.
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
// get a byte array filled with data, for instance, by reading it from a network
val data = byteArrayOf(0x61, 0x47, 0x56, 0x73, 0x62, 0x47, 0x38, 0x3d)
// decode data from the array
println("Base64.decode(data).contentEquals(\"hello\".encodeToByteArray()) is ${Base64.decode(data).contentEquals("hello".encodeToByteArray())}") // true
val dataInTheMiddle = byteArrayOf(0x00, 0x00, 0x61, 0x47, 0x56, 0x73, 0x62, 0x47, 0x38, 0x3d, 0x00, 0x00)
// decode base64-encoded data from the middle of a buffer
val decoded = Base64.decode(dataInTheMiddle, startIndex = 2, endIndex = 10)
println("decoded.contentEquals(\"hello\".encodeToByteArray()) is ${decoded.contentEquals("hello".encodeToByteArray())}") // true
//sampleEnd
}
Decodes symbols from the specified source char sequence or its substring. Returns a ByteArray containing the resulting bytes.
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
a ByteArray with the resulting bytes.
Parameters
the char sequence to decode symbols from.
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 startIndex or endIndex is out of range of source indices.
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
println("Base64.decode(\"/wD+AP0=\").contentEquals(byteArrayOf(-1, 0, -2, 0, -3)) is ${Base64.decode("/wD+AP0=").contentEquals(byteArrayOf(-1, 0, -2, 0, -3))}") // true
val embeddedB64 = "Data is: \"/wD+AP0=\""
// find '"' indices and extract base64-encoded data in between
val decoded = Base64.decode(
embeddedB64,
startIndex = embeddedB64.indexOf('"') + 1,
endIndex = embeddedB64.lastIndexOf('"')
)
println("decoded.contentEquals(byteArrayOf(-1, 0, -2, 0, -3)) is ${decoded.contentEquals(byteArrayOf(-1, 0, -2, 0, -3))}") // true
//sampleEnd
}