I need to decode a base64 string from rest api int...
# getting-started
u
I need to decode a base64 string from rest api into a android bitmap I can display
String.toByteArray() → Base64.decode(ByteArray): ByteArray → Android Bitmap.decodeByteArray
however the base64 decode allocates a new buffer, so maybe I can do away with it? so I want thinking of decoding in place with
decodeIntoByteArray
passing in source and dest as the same thing
Copy code
val bytes = "...".toByteArray(UTF_8)
val decodedLength = Base64.decodeIntoByteArray(source = bytes, destination = bytes)
BitmapFactory.decodeByteArray(bytes, 0, decodedLength, options)
seems to work is it a good idea though?
e
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.io.encoding/-base64/ decode from the string without the intermediate bytearray
u
im looking at the source and it does create the instance
Copy code
public fun decode(source: CharSequence, startIndex: Int = 0, endIndex: Int = source.length): ByteArray {
    val byteSource = platformCharsToBytes(source, startIndex, endIndex)
    return decode(byteSource)
}
public fun decode(source: ByteArray, startIndex: Int = 0, endIndex: Int = source.size): ByteArray {
    checkSourceBounds(source.size, startIndex, endIndex)

    val decodeSize = decodeSize(source, startIndex, endIndex)
    val destination = ByteArray(decodeSize) <----

    val bytesWritten = decodeImpl(source, destination, 0, startIndex, endIndex)

    check(bytesWritten == destination.size)

    return destination
}
the string overload you mention just extracts the
string.getBytes
for me, afterwards its the same