Scoop API

scoop:scoop.asyncio:0.1.0

src/streams.scoop

Selected production source · src/streams.scoop

1package scoop.asyncio
3import scoop.platform.io.UnexpectedEofException
4import scoop.platform.io.WriteZeroException
5import scoop.platform.memory.NativeByteBuffer
7/**
8 * A suspending byte source. A positive-size read returns the number written to the buffer; zero
9 * means EOF, and short reads are permitted.
10 */
11public interface AsyncByteSource {
12 /**
13 * Reads at most count bytes into the writable region and advances its write position. Zero for
14 * a positive request denotes EOF.
15 */
16 public suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long
19/**
20 * A suspending byte sink accepting short writes and an explicit flush operation.
21 */
22public interface AsyncByteSink {
23 /**
24 * Consumes at most count readable bytes, advances the read position, and returns the accepted
25 * count.
26 */
27 public suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long
28 /**
29 * Waits for accepted buffered output to reach the underlying sink's flush boundary.
30 */
31 public suspend fun flush()
34/**
35 * Combines suspending byte input and output without prescribing ownership or half-close behavior.
36 */
37public interface AsyncByteStream : AsyncByteSource, AsyncByteSink {}
39/**
40 * Reads into the buffer's entire writable region, allowing a short read.
41 */
42public suspend fun AsyncByteSource.readInto(buffer: NativeByteBuffer): Long = readInto(buffer, buffer.writableBytes)
43/**
44 * Writes from the buffer's entire readable region, allowing a short write.
45 */
46public suspend fun AsyncByteSink.writeFrom(buffer: NativeByteBuffer): Long = writeFrom(buffer, buffer.readableBytes)
48/**
49 * Loops until count bytes have been read. Premature EOF throws UnexpectedEofException; an invalid
50 * count is rejected before transfer.
51 */
52public suspend fun AsyncByteSource.readFully(buffer: NativeByteBuffer, count: Long = buffer.writableBytes) {
53 ensureActive()
54 requireTransfer(count, buffer.writableBytes)
55 var remaining = count
56 while (remaining > 0L) {
57 val progress = readInto(buffer, remaining)
58 requireProgress(progress, remaining)
59 if (progress == 0L) throw UnexpectedEofException()
60 remaining -= progress
61 }
64/**
65 * Loops until count bytes have been accepted. A zero-progress write throws WriteZeroException.
66 */
67public suspend fun AsyncByteSink.writeFully(buffer: NativeByteBuffer, count: Long = buffer.readableBytes) {
68 ensureActive()
69 requireTransfer(count, buffer.readableBytes)
70 var remaining = count
71 while (remaining > 0L) {
72 val progress = writeFrom(buffer, remaining)
73 requireProgress(progress, remaining)
74 if (progress == 0L) throw WriteZeroException()
75 remaining -= progress
76 }
79/**
80 * Copies until EOF through a positive-size bounded buffer and returns the transferred byte count.
81 * Both endpoints remain borrowed.
82 */
83public suspend fun AsyncByteSource.copyTo(destination: AsyncByteSink, bufferSize: Long = 8192L): Long {
84 ensureActive()
85 if (bufferSize <= 0L) throw IllegalArgumentException(Some("buffer capacity must be positive"))
86 val buffer = NativeByteBuffer.allocate(bufferSize)
87 try {
88 var total = 0L
89 while (true) {
90 buffer.clear()
91 val count = readInto(buffer, bufferSize)
92 requireProgress(count, bufferSize)
93 if (count == 0L) return total
94 if (count > Long.MAX_VALUE - total) throw ArithmeticException()
95 destination.writeFully(buffer, count)
96 total += count
97 }
98 } finally { buffer.dispose() }
101internal fun requireTransfer(count: Long, available: Long) {
102 if (count < 0L) throw IllegalArgumentException(Some("negative transfer count"))
103 if (count > available) throw IndexOutOfBoundsException()
106internal fun requireProgress(count: Long, requested: Long) {
107 if (count < 0L || count > requested) throw IllegalStateException(Some("invalid I/O progress"))