0
# Ktor I/O for watchOS ARM64
1
2
Ktor I/O is a comprehensive asynchronous I/O library for Kotlin multiplatform, specifically compiled for the watchOS ARM64 platform. It provides coroutine-based non-blocking byte channels, stream operations, memory management utilities, and character encoding support optimized for Apple Watch devices.
3
4
## Package Information
5
6
- **Package Name**: io.ktor:ktor-io-watchosarm64
7
- **Package Type**: maven
8
- **Language**: Kotlin
9
- **Platform**: watchOS ARM64
10
- **Installation**: Add to your `build.gradle.kts` dependencies:
11
```kotlin
12
implementation("io.ktor:ktor-io-watchosarm64:2.3.13")
13
```
14
15
## Core Imports
16
17
```kotlin
18
import io.ktor.utils.io.*
19
import io.ktor.utils.io.core.*
20
import io.ktor.utils.io.bits.*
21
import io.ktor.utils.io.pool.*
22
import io.ktor.utils.io.charsets.*
23
```
24
25
## Basic Usage
26
27
```kotlin
28
import io.ktor.utils.io.*
29
import io.ktor.utils.io.core.*
30
import io.ktor.utils.io.bits.*
31
import kotlinx.coroutines.*
32
33
// Create channels for async I/O
34
val readChannel = ByteReadChannel("Hello, watchOS!".toByteArray())
35
val writeChannel = ByteChannel(autoFlush = true) // Note: ByteChannel is deprecated
36
37
// Read data asynchronously
38
runBlocking {
39
val text = readChannel.readUTF8Line()
40
val remainingPacket = readChannel.readRemaining()
41
42
println("Text: $text")
43
}
44
45
// Write data to channel
46
runBlocking {
47
writeChannel.writeStringUtf8("Hello, watchOS!")
48
writeChannel.writeByte(42)
49
writeChannel.close()
50
}
51
52
// Work with byte packets (immutable)
53
val packet = buildPacket {
54
writeText("Packet data")
55
writeInt(12345)
56
}
57
58
// Read from packet
59
val text = packet.readText()
60
val number = packet.readInt()
61
62
// Memory operations with temporary allocation
63
withMemory(1024) { memory ->
64
memory.storeAt(0, 0xFF.toByte())
65
val value = memory.loadAt(0)
66
println("Stored and loaded: ${value.toUByte()}")
67
}
68
```
69
70
## Architecture
71
72
Ktor I/O is built around several key components:
73
74
- **Async Channels**: `ByteReadChannel` and `ByteWriteChannel` interfaces for coroutine-based non-blocking I/O
75
- **Packet System**: Immutable `ByteReadPacket` and mutable `BytePacketBuilder` for structured data handling
76
- **Memory Management**: Low-level `Memory` API for direct byte manipulation with platform-specific optimizations
77
- **Object Pooling**: `ObjectPool` interface for efficient resource management and memory reuse
78
- **Character Encoding**: UTF-8 and ISO-8859-1 charset support with encoder/decoder abstractions
79
- **watchOS Optimization**: Architecture-specific optimizations for ARM64 and watchOS device constraints
80
81
## Capabilities
82
83
### Async Byte Channels
84
85
Core asynchronous I/O functionality providing single-reader/single-writer channels for non-blocking byte operations with coroutine integration.
86
87
```kotlin { .api }
88
interface ByteReadChannel {
89
val availableForRead: Int
90
val isClosedForRead: Boolean
91
suspend fun readByte(): Byte
92
suspend fun readAvailable(dst: ByteArray, offset: Int, length: Int): Int
93
}
94
95
interface ByteWriteChannel {
96
val availableForWrite: Int
97
val isClosedForWrite: Boolean
98
suspend fun writeByte(b: Byte)
99
suspend fun writeAvailable(src: ByteArray, offset: Int, length: Int): Int
100
}
101
```
102
103
[Async Byte Channels](./async-channels.md)
104
105
### Packet I/O System
106
107
Immutable byte packets and builders for structured data handling with automatic memory management and efficient serialization.
108
109
```kotlin { .api }
110
class ByteReadPacket : Input {
111
fun copy(): ByteReadPacket
112
companion object {
113
val Empty: ByteReadPacket
114
}
115
}
116
117
class BytePacketBuilder : Output {
118
val size: Int
119
fun build(): ByteReadPacket
120
fun append(value: Char): BytePacketBuilder
121
}
122
123
fun buildPacket(block: BytePacketBuilder.() -> Unit): ByteReadPacket
124
```
125
126
[Packet I/O System](./packet-io.md)
127
128
### Memory Management
129
130
Low-level memory operations providing direct byte access, primitive type loading/storing, and platform-optimized memory utilities.
131
132
```kotlin { .api }
133
expect class Memory {
134
val size: Long
135
fun loadAt(index: Int): Byte
136
fun storeAt(index: Int, value: Byte)
137
fun slice(offset: Int, length: Int): Memory
138
operator fun get(index: Int): Byte
139
operator fun set(index: Int, value: Byte)
140
}
141
```
142
143
[Memory Management](./memory-management.md)
144
145
### Object Pooling
146
147
Resource management system providing efficient object reuse patterns with configurable capacity and lifecycle management.
148
149
```kotlin { .api }
150
interface ObjectPool<T> : Closeable {
151
val capacity: Int
152
fun borrow(): T
153
fun recycle(instance: T)
154
}
155
156
object ByteArrayPool : ObjectPool<ByteArray>
157
158
inline fun <T : Any, R> ObjectPool<T>.useInstance(block: (T) -> R): R
159
```
160
161
[Object Pooling](./object-pooling.md)
162
163
### Character Encoding
164
165
Character encoding and decoding support with UTF-8 and ISO-8859-1 charsets, providing encoder/decoder abstractions for text processing.
166
167
```kotlin { .api }
168
abstract class Charset {
169
val name: String
170
abstract fun newEncoder(): CharsetEncoder
171
abstract fun newDecoder(): CharsetDecoder
172
}
173
174
object Charsets {
175
val UTF_8: Charset
176
val ISO_8859_1: Charset
177
}
178
```
179
180
[Character Encoding](./character-encoding.md)
181
182
### Byte Order Utilities
183
184
Byte order manipulation and primitive type conversion utilities supporting both big-endian and little-endian operations.
185
186
```kotlin { .api }
187
fun Short.reverseByteOrder(): Short
188
fun Int.reverseByteOrder(): Int
189
fun Long.reverseByteOrder(): Long
190
fun Float.reverseByteOrder(): Float
191
fun Double.reverseByteOrder(): Double
192
193
val Short.highByte: Byte
194
val Short.lowByte: Byte
195
val Int.highShort: Short
196
val Int.lowShort: Short
197
```
198
199
[Byte Order Utilities](./byte-order.md)
200
201
## Types
202
203
### Core Channel Types
204
205
```kotlin { .api }
206
interface ByteReadChannel {
207
val availableForRead: Int
208
val isClosedForRead: Boolean
209
val isClosedForWrite: Boolean
210
val closedCause: Throwable?
211
val totalBytesRead: Long
212
213
suspend fun readAvailable(dst: ByteArray, offset: Int, length: Int): Int
214
suspend fun readFully(dst: ByteArray, offset: Int, length: Int)
215
suspend fun readByte(): Byte
216
suspend fun readShort(): Short
217
suspend fun readInt(): Int
218
suspend fun readLong(): Long
219
suspend fun readFloat(): Float
220
suspend fun readDouble(): Double
221
suspend fun readBoolean(): Boolean
222
suspend fun readUTF8Line(limit: Int = Int.MAX_VALUE): String?
223
suspend fun readPacket(size: Int): ByteReadPacket
224
suspend fun discard(max: Long = Long.MAX_VALUE): Long
225
fun cancel(cause: Throwable? = null): Boolean
226
227
companion object {
228
val Empty: ByteReadChannel
229
}
230
}
231
232
interface ByteWriteChannel {
233
val availableForWrite: Int
234
val isClosedForWrite: Boolean
235
val autoFlush: Boolean
236
val totalBytesWritten: Long
237
val closedCause: Throwable?
238
239
suspend fun writeAvailable(src: ByteArray, offset: Int, length: Int): Int
240
suspend fun writeFully(src: ByteArray, offset: Int, length: Int)
241
suspend fun writeByte(b: Byte)
242
suspend fun writeShort(s: Short)
243
suspend fun writeInt(i: Int)
244
suspend fun writeLong(l: Long)
245
suspend fun writeFloat(f: Float)
246
suspend fun writeDouble(d: Double)
247
suspend fun writePacket(packet: ByteReadPacket)
248
fun close(cause: Throwable? = null): Boolean
249
fun flush()
250
suspend fun awaitFreeSpace()
251
}
252
253
@Deprecated("Use ByteReadChannel and ByteWriteChannel instead")
254
interface ByteChannel : ByteReadChannel, ByteWriteChannel
255
```
256
257
### Packet Types
258
259
```kotlin { .api }
260
class ByteReadPacket : Input {
261
val remaining: Long
262
val endOfInput: Boolean
263
264
fun copy(): ByteReadPacket
265
266
companion object {
267
val Empty: ByteReadPacket
268
}
269
}
270
271
class BytePacketBuilder : Output, Appendable {
272
val size: Int
273
val isEmpty: Boolean
274
val isNotEmpty: Boolean
275
276
fun build(): ByteReadPacket
277
override fun append(value: Char): BytePacketBuilder
278
override fun append(value: CharSequence?): BytePacketBuilder
279
}
280
```
281
282
### Memory Types
283
284
```kotlin { .api }
285
expect class Memory {
286
val size: Long
287
val size32: Int
288
289
fun loadAt(index: Int): Byte
290
fun loadAt(index: Long): Byte
291
fun storeAt(index: Int, value: Byte)
292
fun storeAt(index: Long, value: Byte)
293
294
fun slice(offset: Int, length: Int): Memory
295
fun slice(offset: Long, length: Long): Memory
296
297
fun copyTo(destination: Memory, offset: Int, length: Int, destinationOffset: Int)
298
fun copyTo(destination: ByteArray, offset: Int, length: Int, destinationOffset: Int)
299
300
operator fun get(index: Int): Byte
301
operator fun get(index: Long): Byte
302
operator fun set(index: Int, value: Byte)
303
operator fun set(index: Long, value: Byte)
304
305
fun fill(offset: Int, count: Int, value: Byte)
306
fun fill(offset: Long, count: Long, value: Byte)
307
308
companion object {
309
val Empty: Memory
310
}
311
}
312
```
313
314
### Exception Types
315
316
```kotlin { .api }
317
expect open class IOException(message: String, cause: Throwable? = null) : Exception
318
319
expect open class EOFException(message: String) : IOException
320
321
class ClosedWriteChannelException(message: String? = null) : CancellationException(message)
322
323
class InsufficientSpaceException(message: String) : Exception(message)
324
325
abstract class MalformedInputException(message: String) : Throwable(message)
326
327
class TooLongLineException(message: String) : MalformedInputException(message)
328
```