withPadding

Returns a new Base64 instance that is equivalent to this instance but configured with the specified padding option.

This method does not modify this instance. If the specified option is the same as the current padding option of this instance, this instance itself is returned. Otherwise, a new instance is created using the same alphabet but configured with the new padding option.

Since Kotlin

2.0

Samples

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

fun main() { 
   //sampleStart 
   // The predefined Base64.UrlSafe instance uses PaddingOption.PRESENT.
// Create a new instance with PaddingOption.ABSENT_OPTIONAL from the Base64.UrlSafe instance.
val base64UrlSafeCustomPadding = Base64.UrlSafe.withPadding(Base64.PaddingOption.ABSENT_OPTIONAL)

// The new instance continues using the same UrlSafe alphabet but omits padding when encoding
println(base64UrlSafeCustomPadding.encode(byteArrayOf(-1, 0, -2, 0))) // _wD-AA
// It allows decoding both padded and unpadded inputs
println(base64UrlSafeCustomPadding.decode("_wD-AA").contentToString()) // [-1, 0, -2, 0]
println(base64UrlSafeCustomPadding.decode("_wD-AA==").contentToString()) // [-1, 0, -2, 0] 
   //sampleEnd
}