scoop:scoop.asyncio:0.1.0
src/streams.scoop
Selected production source · src/streams.scoop
1
package scoop.asyncio3
import scoop.platform.io.UnexpectedEofException4
import scoop.platform.io.WriteZeroException5
import scoop.platform.memory.NativeByteBuffer7
/**8
* A suspending byte source. A positive-size read returns the number written to the buffer; zero9
* means EOF, and short reads are permitted.10
*/11
public interface AsyncByteSource {12
/**13
* Reads at most count bytes into the writable region and advances its write position. Zero for14
* a positive request denotes EOF.15
*/16
public suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long17
}19
/**20
* A suspending byte sink accepting short writes and an explicit flush operation.21
*/22
public interface AsyncByteSink {23
/**24
* Consumes at most count readable bytes, advances the read position, and returns the accepted25
* count.26
*/27
public suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long28
/**29
* Waits for accepted buffered output to reach the underlying sink's flush boundary.30
*/31
public suspend fun flush()32
}34
/**35
* Combines suspending byte input and output without prescribing ownership or half-close behavior.36
*/37
public interface AsyncByteStream : AsyncByteSource, AsyncByteSink {}39
/**40
* Reads into the buffer's entire writable region, allowing a short read.41
*/42
public 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
*/46
public 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 invalid50
* count is rejected before transfer.51
*/52
public suspend fun AsyncByteSource.readFully(buffer: NativeByteBuffer, count: Long = buffer.writableBytes) {53
ensureActive()54
requireTransfer(count, buffer.writableBytes)55
var remaining = count56
while (remaining > 0L) {57
val progress = readInto(buffer, remaining)58
requireProgress(progress, remaining)59
if (progress == 0L) throw UnexpectedEofException()60
remaining -= progress61
}62
}64
/**65
* Loops until count bytes have been accepted. A zero-progress write throws WriteZeroException.66
*/67
public suspend fun AsyncByteSink.writeFully(buffer: NativeByteBuffer, count: Long = buffer.readableBytes) {68
ensureActive()69
requireTransfer(count, buffer.readableBytes)70
var remaining = count71
while (remaining > 0L) {72
val progress = writeFrom(buffer, remaining)73
requireProgress(progress, remaining)74
if (progress == 0L) throw WriteZeroException()75
remaining -= progress76
}77
}79
/**80
* Copies until EOF through a positive-size bounded buffer and returns the transferred byte count.81
* Both endpoints remain borrowed.82
*/83
public 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 = 0L89
while (true) {90
buffer.clear()91
val count = readInto(buffer, bufferSize)92
requireProgress(count, bufferSize)93
if (count == 0L) return total94
if (count > Long.MAX_VALUE - total) throw ArithmeticException()95
destination.writeFully(buffer, count)96
total += count97
}98
} finally { buffer.dispose() }99
}101
internal fun requireTransfer(count: Long, available: Long) {102
if (count < 0L) throw IllegalArgumentException(Some("negative transfer count"))103
if (count > available) throw IndexOutOfBoundsException()104
}106
internal fun requireProgress(count: Long, requested: Long) {107
if (count < 0L || count > requested) throw IllegalStateException(Some("invalid I/O progress"))108
}