CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-js

Kotlin Standard Library for JavaScript target with comprehensive W3C DOM bindings and JavaScript interoperability features.

Pending
Overview
Eval results
Files

io-encoding.mddocs/

IO and Encoding

Base64 encoding support and I/O operations optimized for JavaScript runtime environments. This module provides efficient binary data encoding and decoding capabilities.

Capabilities

Base64 Encoding

Platform-optimized Base64 encoding and decoding operations.

/**
 * Base64 encoding and decoding utilities
 */
object Base64 {
    /**
     * Encode byte array to Base64 byte array
     */
    fun encode(source: ByteArray): ByteArray
    
    /**
     * Decode Base64 byte array to original byte array
     */
    fun decode(source: ByteArray): ByteArray
    
    /**
     * Encode byte array to Base64 string
     */
    fun encodeToString(source: ByteArray): String
    
    /**
     * Decode Base64 string to byte array
     */
    fun decodeFromString(source: String): ByteArray
    
    /**
     * Platform-specific character to byte conversion
     */
    internal fun platformCharsToBytes(source: CharArray, startIndex: Int, endIndex: Int): ByteArray
    
    /**
     * Platform-specific string encoding
     */
    internal fun platformEncodeToString(source: ByteArray, startIndex: Int, endIndex: Int): String
}

Usage Examples:

import kotlin.io.encoding.Base64

// Encode string to Base64
val originalText = "Hello, World!"
val bytes = originalText.encodeToByteArray()
val encoded = Base64.encodeToString(bytes)
console.log("Encoded: $encoded") // "SGVsbG8sIFdvcmxkIQ=="

// Decode Base64 back to string
val decoded = Base64.decodeFromString(encoded)
val decodedText = decoded.decodeToString()
console.log("Decoded: $decodedText") // "Hello, World!"

// Binary data encoding
val binaryData = byteArrayOf(0x48, 0x65, 0x6C, 0x6C, 0x6F)
val encodedBinary = Base64.encode(binaryData)
val decodedBinary = Base64.decode(encodedBinary)

// URL-safe Base64 (if needed, manual replacement)
val urlSafeEncoded = encoded.replace('+', '-').replace('/', '_').replace("=", "")

Console I/O

Console output operations for debugging and logging.

/**
 * Print message to console
 */
fun print(message: Any?)

/**
 * Print message with newline to console
 */
fun println(message: Any?)

/**
 * Print message with newline (no argument version)
 */
fun println()

/**
 * Read line from console (Node.js environments)
 */
fun readLine(): String?

Usage Examples:

// Console output
print("Hello, ")
println("World!")
println() // Empty line

// Variables and objects
val name = "Kotlin"
val version = 1.9
println("Language: $name")
println("Version: $version")

// Objects
data class Person(val name: String, val age: Int)
val person = Person("Alice", 30)
println(person) // Person(name=Alice, age=30)

// Debug output
val list = listOf(1, 2, 3, 4, 5)
println("List contents: $list")

Character Encoding

Character encoding and decoding utilities for text processing.

/**
 * Character encoding exceptions
 */
class CharacterCodingException : Exception {
    constructor()
    constructor(message: String?)
}

/**
 * UTF-8 encoding utilities
 */
object Charsets {
    val UTF_8: Charset
    val UTF_16: Charset
    val ISO_8859_1: Charset
}

/**
 * String encoding extensions
 */
fun String.encodeToByteArray(): ByteArray
fun String.encodeToByteArray(charset: Charset): ByteArray

/**
 * ByteArray decoding extensions
 */
fun ByteArray.decodeToString(): String
fun ByteArray.decodeToString(charset: Charset): String

Usage Examples:

// UTF-8 encoding (default)
val text = "Hello, 世界! 🌍"
val utf8Bytes = text.encodeToByteArray()
val decodedText = utf8Bytes.decodeToString()
console.log("Original: $text")
console.log("Decoded: $decodedText")

// Explicit charset
val isoBytes = text.encodeToByteArray(Charsets.ISO_8859_1) // May throw exception for non-Latin characters
val utf16Bytes = text.encodeToByteArray(Charsets.UTF_16)

// Error handling
try {
    val problematicText = "含有中文"
    val isoEncoded = problematicText.encodeToByteArray(Charsets.ISO_8859_1)
} catch (e: CharacterCodingException) {
    console.error("Encoding failed: ${e.message}")
}

Types

// Base64 encoding
object Base64 {
    fun encode(source: ByteArray): ByteArray
    fun decode(source: ByteArray): ByteArray
    fun encodeToString(source: ByteArray): String
    fun decodeFromString(source: String): ByteArray
}

// Character encoding
class CharacterCodingException : Exception
interface Charset
object Charsets {
    val UTF_8: Charset
    val UTF_16: Charset  
    val ISO_8859_1: Charset
}

// Console I/O functions
fun print(message: Any?)
fun println(message: Any?)
fun println()
fun readLine(): String?

// String encoding extensions
fun String.encodeToByteArray(): ByteArray
fun String.encodeToByteArray(charset: Charset): ByteArray
fun ByteArray.decodeToString(): String
fun ByteArray.decodeToString(charset: Charset): String

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-js

docs

browser-integration.md

collections.md

coroutines.md

index.md

io-encoding.md

javascript-interop.md

math-time.md

reflection.md

uuid.md

w3c-dom-apis.md

tile.json