Default

The "base64" encoding specified by RFC 4648 section 4, Base 64 Encoding.

Uses "The Base 64 Alphabet" as specified in Table 1 of RFC 4648 for encoding and decoding, consisting of 'A'..'Z', 'a'..'z', '+' and '/' characters.

This instance is configured with the padding option set to PaddingOption.PRESENT. Use the withPadding function to create a new instance with a different padding option if necessary.

Encode operation does not add any line separator character. Decode operation throws if it encounters a character outside the base64 alphabet.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   val encoded = Base64.Default.encode("Hello? :> ".encodeToByteArray())
println(encoded) // SGVsbG8/IDo+IA== 
   //sampleEnd
}

Properties

Link copied to clipboard

The encoding specified by RFC 2045 section 6.8, Base64 Content-Transfer-Encoding.

Since Kotlin 1.8
Link copied to clipboard

The "base64url" encoding specified by RFC 4648 section 5, Base 64 Encoding with URL and Filename Safe Alphabet.

Since Kotlin 1.8

Functions

Link copied to clipboard
fun decode(source: ByteArray, startIndex: Int = 0, endIndex: Int = source.size): ByteArray

Decodes symbols from the specified source array or its subrange. Returns a ByteArray containing the resulting bytes.

Since Kotlin 1.8
fun decode(source: CharSequence, startIndex: Int = 0, endIndex: Int = source.length): ByteArray

Decodes symbols from the specified source char sequence or its substring. Returns a ByteArray containing the resulting bytes.

Since Kotlin 1.8
Link copied to clipboard
fun decodeIntoByteArray(source: ByteArray, destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = source.size): Int

Decodes symbols from the specified source array or its subrange and writes resulting bytes into the destination array. Returns the number of bytes written.

Since Kotlin 1.8
fun decodeIntoByteArray(source: CharSequence, destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = source.length): Int

Decodes symbols from the specified source char sequence or its substring and writes resulting bytes into the destination array. Returns the number of bytes written.

Since Kotlin 1.8
Link copied to clipboard
fun encode(source: ByteArray, startIndex: Int = 0, endIndex: Int = source.size): String

Encodes bytes from the specified source array or its subrange. Returns a string with the resulting symbols.

Since Kotlin 1.8
Link copied to clipboard
fun encodeIntoByteArray(source: ByteArray, destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = source.size): Int

Encodes bytes from the specified source array or its subrange and writes resulting symbols into the destination array. Returns the number of symbols written.

Since Kotlin 1.8
Link copied to clipboard
fun <A : Appendable> encodeToAppendable(source: ByteArray, destination: A, startIndex: Int = 0, endIndex: Int = source.size): A

Encodes bytes from the specified source array or its subrange and appends resulting symbols to the destination appendable. Returns the destination appendable.

Since Kotlin 1.8
Link copied to clipboard
fun encodeToByteArray(source: ByteArray, startIndex: Int = 0, endIndex: Int = source.size): ByteArray

Encodes bytes from the specified source array or its subrange. Returns a ByteArray containing the resulting symbols.

Since Kotlin 1.8
Link copied to clipboard

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

Since Kotlin 2.0