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.
import kotlin.io.encoding.*
import kotlin.test.*
fun main(args: Array<String>) {
//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
}
Parameters
source
- the array to decode symbols from.
startIndex
- the beginning (inclusive) of the subrange to decode, 0 by default.
endIndex
- the end (exclusive) of the subrange to decode, size of the source array by default.
Exceptions
IndexOutOfBoundsException
- when startIndex or endIndex is out of range of source array indices.
IllegalArgumentException
- when startIndex > endIndex
.
IllegalArgumentException
- 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.
Return a ByteArray with the resulting bytes.
fun decode(
source: CharSequence,
startIndex: Int = 0,
endIndex: Int = source.length
): ByteArray
(source)
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.
import kotlin.io.encoding.*
import kotlin.test.*
fun main(args: Array<String>) {
//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
}
Parameters
source
- the char sequence to decode symbols from.
startIndex
- the beginning (inclusive) of the substring to decode, 0 by default.
endIndex
- the end (exclusive) of the substring to decode, length of the source by default.
Exceptions
IndexOutOfBoundsException
- when startIndex or endIndex is out of range of source indices.
IllegalArgumentException
- when startIndex > endIndex
.
IllegalArgumentException
- 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.
Return a ByteArray with the resulting bytes.