Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.io |
| 6 | |
| 7 | import kotlinx.coroutines.experimental.io.internal.EmptyByteBuffer |
| 8 | |
| 9 | /** |
| 10 | * Channel for asynchronous reading and writing of sequences of bytes. |
| 11 | * This is a buffered **single-reader single-writer channel**. |
| 12 | * |
| 13 | * Read operations can be invoked concurrently with write operations, but multiple reads or multiple writes |
| 14 | * cannot be invoked concurrently with themselves. Exceptions are [close] and [flush] which can be invoked |
| 15 | * concurrently with any other operations and between themselves at any time. |
| 16 | */ |
| 17 | interface ByteChannel : ByteReadChannel, ByteWriteChannel |
| 18 | |
| 19 | /** |
| 20 | * Creates buffered channel for asynchronous reading and writing of sequences of bytes. |
| 21 | */ |
| 22 | public fun ByteChannel(autoFlush: Boolean = false): ByteChannel = |
| 23 | ByteBufferChannel(autoFlush) |
| 24 | |
| 25 | /** |
| 26 | * Creates channel for reading from the specified byte buffer. |
| 27 | */ |
| 28 | public fun ByteReadChannel(content: ByteBuffer): ByteReadChannel = |
| 29 | ByteBufferChannel(content) |
| 30 | |
| 31 | /** |
| 32 | * Creates channel for reading from the specified byte array. |
| 33 | */ |
| 34 | public fun ByteReadChannel(content: ByteArray): ByteReadChannel = |
| 35 | ByteBufferChannel(ByteBuffer.wrap(content)) |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Byte channel that is always empty. |
| 40 | */ |
| 41 | val EmptyByteReadChannel: ByteReadChannel = ByteReadChannel(EmptyByteBuffer) |