blob: d2d7e96d71fdd9a61c600a855f504a61d3b1d9e7 [file] [log] [blame]
Roman Elizarov469cad32017-08-15 15:54:56 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov469cad32017-08-15 15:54:56 +03003 */
4
5package kotlinx.coroutines.experimental.io
6
7import 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 */
17interface ByteChannel : ByteReadChannel, ByteWriteChannel
18
19/**
20 * Creates buffered channel for asynchronous reading and writing of sequences of bytes.
21 */
22public fun ByteChannel(autoFlush: Boolean = false): ByteChannel =
23 ByteBufferChannel(autoFlush)
24
25/**
26 * Creates channel for reading from the specified byte buffer.
27 */
28public fun ByteReadChannel(content: ByteBuffer): ByteReadChannel =
29 ByteBufferChannel(content)
30
31/**
32 * Creates channel for reading from the specified byte array.
33 */
34public fun ByteReadChannel(content: ByteArray): ByteReadChannel =
35 ByteBufferChannel(ByteBuffer.wrap(content))
36
37
38/**
39 * Byte channel that is always empty.
40 */
41val EmptyByteReadChannel: ByteReadChannel = ByteReadChannel(EmptyByteBuffer)