or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

browser-integration.mdcollections.mdcoroutines.mdindex.mdio-encoding.mdjavascript-interop.mdmath-time.mdreflection.mduuid.mdw3c-dom-apis.md

io-encoding.mddocs/

0

# IO and Encoding

1

2

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

3

4

## Capabilities

5

6

### Base64 Encoding

7

8

Platform-optimized Base64 encoding and decoding operations.

9

10

```kotlin { .api }

11

/**

12

* Base64 encoding and decoding utilities

13

*/

14

object Base64 {

15

/**

16

* Encode byte array to Base64 byte array

17

*/

18

fun encode(source: ByteArray): ByteArray

19

20

/**

21

* Decode Base64 byte array to original byte array

22

*/

23

fun decode(source: ByteArray): ByteArray

24

25

/**

26

* Encode byte array to Base64 string

27

*/

28

fun encodeToString(source: ByteArray): String

29

30

/**

31

* Decode Base64 string to byte array

32

*/

33

fun decodeFromString(source: String): ByteArray

34

35

/**

36

* Platform-specific character to byte conversion

37

*/

38

internal fun platformCharsToBytes(source: CharArray, startIndex: Int, endIndex: Int): ByteArray

39

40

/**

41

* Platform-specific string encoding

42

*/

43

internal fun platformEncodeToString(source: ByteArray, startIndex: Int, endIndex: Int): String

44

}

45

```

46

47

**Usage Examples:**

48

49

```kotlin

50

import kotlin.io.encoding.Base64

51

52

// Encode string to Base64

53

val originalText = "Hello, World!"

54

val bytes = originalText.encodeToByteArray()

55

val encoded = Base64.encodeToString(bytes)

56

console.log("Encoded: $encoded") // "SGVsbG8sIFdvcmxkIQ=="

57

58

// Decode Base64 back to string

59

val decoded = Base64.decodeFromString(encoded)

60

val decodedText = decoded.decodeToString()

61

console.log("Decoded: $decodedText") // "Hello, World!"

62

63

// Binary data encoding

64

val binaryData = byteArrayOf(0x48, 0x65, 0x6C, 0x6C, 0x6F)

65

val encodedBinary = Base64.encode(binaryData)

66

val decodedBinary = Base64.decode(encodedBinary)

67

68

// URL-safe Base64 (if needed, manual replacement)

69

val urlSafeEncoded = encoded.replace('+', '-').replace('/', '_').replace("=", "")

70

```

71

72

### Console I/O

73

74

Console output operations for debugging and logging.

75

76

```kotlin { .api }

77

/**

78

* Print message to console

79

*/

80

fun print(message: Any?)

81

82

/**

83

* Print message with newline to console

84

*/

85

fun println(message: Any?)

86

87

/**

88

* Print message with newline (no argument version)

89

*/

90

fun println()

91

92

/**

93

* Read line from console (Node.js environments)

94

*/

95

fun readLine(): String?

96

```

97

98

**Usage Examples:**

99

100

```kotlin

101

// Console output

102

print("Hello, ")

103

println("World!")

104

println() // Empty line

105

106

// Variables and objects

107

val name = "Kotlin"

108

val version = 1.9

109

println("Language: $name")

110

println("Version: $version")

111

112

// Objects

113

data class Person(val name: String, val age: Int)

114

val person = Person("Alice", 30)

115

println(person) // Person(name=Alice, age=30)

116

117

// Debug output

118

val list = listOf(1, 2, 3, 4, 5)

119

println("List contents: $list")

120

```

121

122

### Character Encoding

123

124

Character encoding and decoding utilities for text processing.

125

126

```kotlin { .api }

127

/**

128

* Character encoding exceptions

129

*/

130

class CharacterCodingException : Exception {

131

constructor()

132

constructor(message: String?)

133

}

134

135

/**

136

* UTF-8 encoding utilities

137

*/

138

object Charsets {

139

val UTF_8: Charset

140

val UTF_16: Charset

141

val ISO_8859_1: Charset

142

}

143

144

/**

145

* String encoding extensions

146

*/

147

fun String.encodeToByteArray(): ByteArray

148

fun String.encodeToByteArray(charset: Charset): ByteArray

149

150

/**

151

* ByteArray decoding extensions

152

*/

153

fun ByteArray.decodeToString(): String

154

fun ByteArray.decodeToString(charset: Charset): String

155

```

156

157

**Usage Examples:**

158

159

```kotlin

160

// UTF-8 encoding (default)

161

val text = "Hello, 世界! 🌍"

162

val utf8Bytes = text.encodeToByteArray()

163

val decodedText = utf8Bytes.decodeToString()

164

console.log("Original: $text")

165

console.log("Decoded: $decodedText")

166

167

// Explicit charset

168

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

169

val utf16Bytes = text.encodeToByteArray(Charsets.UTF_16)

170

171

// Error handling

172

try {

173

val problematicText = "含有中文"

174

val isoEncoded = problematicText.encodeToByteArray(Charsets.ISO_8859_1)

175

} catch (e: CharacterCodingException) {

176

console.error("Encoding failed: ${e.message}")

177

}

178

```

179

180

## Types

181

182

```kotlin { .api }

183

// Base64 encoding

184

object Base64 {

185

fun encode(source: ByteArray): ByteArray

186

fun decode(source: ByteArray): ByteArray

187

fun encodeToString(source: ByteArray): String

188

fun decodeFromString(source: String): ByteArray

189

}

190

191

// Character encoding

192

class CharacterCodingException : Exception

193

interface Charset

194

object Charsets {

195

val UTF_8: Charset

196

val UTF_16: Charset

197

val ISO_8859_1: Charset

198

}

199

200

// Console I/O functions

201

fun print(message: Any?)

202

fun println(message: Any?)

203

fun println()

204

fun readLine(): String?

205

206

// String encoding extensions

207

fun String.encodeToByteArray(): ByteArray

208

fun String.encodeToByteArray(charset: Charset): ByteArray

209

fun ByteArray.decodeToString(): String

210

fun ByteArray.decodeToString(charset: Charset): String

211

```