Kotlin Coroutines make asynchronous programming simple and efficient but there is no official library that provides IO operations in a coroutine. These operations, such as reading a file or sending data over a TCP connection, benefit heavily from coroutines because they can take relatively long and use few CPU resources. This library seeks to wrap existing Java non-blocking IO libraries to provide a uniform and simple API with coroutines.
- Simple, lean stream interfaces with suspend read and write functions using ByteBuffers
- Supports file access
- Supports TCP servers and clients
- Supports UDP unicast and multicast
- Wrapping any stream with TLS
- Official Kotlin coroutine IO - Does not exist yet.
- Java blocking IO - Must be used with care in coroutines because the blocking operations can consume your worker threads. Consider using the IO Dispatcher.
- Java NIO - Efficient non-blocking operation available but steep learning curve and quite a bit of code needed to build coroutines.
- Java NIO2 - Nice asynchronous methods that can be easily wrapped into a coroutine.
val server = CoIOTcp.listen(8775)
for (connection in server) {
launch {
val buf = ByteBuffer.allocate(1024)
connection.readFully(buf)
buf.flip()
connection.writeFully(buf)
}
}