ABSENT_OPTIONAL

Common
JVM
JS
Native
1.0
ABSENT_OPTIONAL
(source)

Do not pad on encode, allow optional padding on decode.

When encoding, the result is not padded. When decoding, the input may be either padded or unpadded. If the input contains a padding character, the correct amount of padding character(s) must be present. The padding character '=' marks the end of the encoded data, and subsequent symbols are prohibited.

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

fun main(args: Array<String>) {
//sampleStart
val format = HexFormat { upperCase = true; bytes.byteSeparator = " " }
val bytes = byteArrayOf(0xDE.toByte(), 0x2D, 0x02, 0xC0.toByte())

val base64AbsentOptionalPadding = Base64.Default.withPadding(Base64.PaddingOption.ABSENT_OPTIONAL)

// PaddingOption.ABSENT_OPTIONAL does not pad on encode
println(base64AbsentOptionalPadding.encode(bytes)) // 3i0CwA
// It allows both padded and unpadded decode inputs
println(base64AbsentOptionalPadding.decode("3i0CwA").toHexString(format)) // DE 2D 02 C0
println(base64AbsentOptionalPadding.decode("3i0CwA==").toHexString(format)) // DE 2D 02 C0
// However, partially padded input is prohibited
// base64AbsentOptionalPadding.decode("3i0CwA=") // will fail with IllegalArgumentException
//sampleEnd
}