PaddingOption

An enumeration of the possible padding options for Base64 encoding and decoding.

Constants of this enum class can be passed to the withPadding function to create a new Base64 instance with the specified padding option. Each padding option affects the encode and decode operations of the Base64 instance in the following way:

PaddingOptionOn encodeOn decode
PaddingOption.PRESENTEmit paddingPadding is required
PaddingOption.ABSENTOmit paddingPadding must not present
PaddingOption.PRESENT_OPTIONALEmit paddingPadding is optional
PaddingOption.ABSENT_OPTIONALOmit paddingPadding is optional

These options provide flexibility in handling the padding characters ('=') and enable compatibility with various Base64 libraries and protocols.

Since Kotlin

2.0

Samples

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

fun main() { 
   //sampleStart 
   val format = HexFormat {
    upperCase = true
    bytes.byteSeparator = " "
}

val bytes = byteArrayOf(0xDE.toByte(), 0x2D, 0x02, 0xC0.toByte())

// The Base64.Default instance is configured with PaddingOption.PRESENT
println(Base64.Default.encode(bytes)) // 3i0CwA==
println(Base64.Default.decode("3i0CwA==").toHexString(format)) // DE 2D 02 C0
// PaddingOption.PRESENT requires the decode input to be properly padded
// Base64.Default.decode("3i0CwA") // will fail with IllegalArgumentException

// Create a new instance with PaddingOption.ABSENT that uses the Base64.Default alphabet
val base64AbsentPadding = Base64.Default.withPadding(Base64.PaddingOption.ABSENT)

println(base64AbsentPadding.encode(bytes)) // 3i0CwA
println(base64AbsentPadding.decode("3i0CwA").toHexString(format)) // DE 2D 02 C0
// PaddingOption.ABSENT requires the decode input not to be padded
// base64AbsentPadding.decode("3i0CwA==") // will fail with IllegalArgumentException

// Create a new instance with PaddingOption.PRESENT_OPTIONAL that uses the Base64.Default alphabet
val base64PresentOptionalPadding = Base64.Default.withPadding(Base64.PaddingOption.PRESENT_OPTIONAL)

println(base64PresentOptionalPadding.encode(bytes)) // 3i0CwA==
// PaddingOption.PRESENT_OPTIONAL allows both padded and unpadded decode inputs
println(base64PresentOptionalPadding.decode("3i0CwA==").toHexString(format)) // DE 2D 02 C0
println(base64PresentOptionalPadding.decode("3i0CwA").toHexString(format)) // DE 2D 02 C0
// However, partially padded input is prohibited
// base64PresentOptionalPadding.decode("3i0CwA=") // will fail with IllegalArgumentException

// Create a new instance with PaddingOption.ABSENT_OPTIONAL that uses the Base64.Default alphabet
val base64AbsentOptionalPadding = Base64.Default.withPadding(Base64.PaddingOption.ABSENT_OPTIONAL)

println(base64AbsentOptionalPadding.encode(bytes)) // 3i0CwA
// PaddingOption.ABSENT_OPTIONAL 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
}

Entries

Link copied to clipboard

Pad on encode, require padding on decode.

Link copied to clipboard

Do not pad on encode, prohibit padding on decode.

Link copied to clipboard

Pad on encode, allow optional padding on decode.

Link copied to clipboard

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

Properties

Link copied to clipboard

Returns a Java Class instance of the enum the given constant belongs to.

Since Kotlin 1.7
Link copied to clipboard

Returns a representation of an immutable list of all enum entries, in the order they're declared.

Since Kotlin 2.0
Link copied to clipboard
Since Kotlin 2.0
Link copied to clipboard
Since Kotlin 2.0

Functions

Link copied to clipboard
infix inline fun <T> Comparable<T>.compareTo(other: T): Int

Compares this object with the specified object for order. Returns zero if this object is equal to the specified other object, a negative number if it's less than other, or a positive number if it's greater than other.

Since Kotlin 1.6
Link copied to clipboard

Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Since Kotlin 2.0
Link copied to clipboard

Returns an array containing the constants of this enum type, in the order they're declared.

Since Kotlin 2.0