scoop:scoop.platform:0.1.0
src/io/streams.scoop
Selected production source · src/io/streams.scoop
1
package scoop.platform.io3
import scoop.bytes.ByteArrayBuilder4
import scoop.platform.memory.NativeByteBuffer5
import scoop.platform.memory.nativeMemoryMove7
/**8
* A synchronous byte source supporting short reads. Zero for a positive-size request denotes EOF.9
*/10
public interface ByteSource {11
/**12
* Reads into the selected writable region, returning actual bytes and permitting short reads.13
* Zero for a positive request denotes EOF.14
*/15
public fun readInto(buffer: NativeByteBuffer, count: Long): Long16
}18
/**19
* A synchronous byte sink supporting short writes and explicit flushing.20
*/21
public interface ByteSink {22
/**23
* Consumes the selected readable region, returning the actual accepted byte count and24
* permitting short writes.25
*/26
public fun writeFrom(buffer: NativeByteBuffer, count: Long): Long27
/**28
* Flushes pending output according to the sink's buffering contract.29
*/30
public fun flush()31
}33
/**34
* An exact-size read reached EOF before the requested byte count.35
*/36
public class UnexpectedEofException public constructor() : Exception(Some("unexpected end of input"))37
/**38
* An exact-size write could not make progress.39
*/40
public class WriteZeroException public constructor() : Exception(Some("output made no progress"))42
/**43
* Reads into the selected writable buffer or array region, returning the actual byte count and44
* allowing short reads.45
*/46
public fun ByteSource.readInto(buffer: NativeByteBuffer): Long = readInto(buffer, buffer.writableBytes)47
/**48
* Writes the selected readable buffer or array region, returning the accepted byte count and49
* allowing short writes.50
*/51
public fun ByteSink.writeFrom(buffer: NativeByteBuffer): Long = writeFrom(buffer, buffer.readableBytes)53
/**54
* Reads exactly the requested count or throws UnexpectedEofException on premature EOF.55
*/56
public fun ByteSource.readFully(buffer: NativeByteBuffer, count: Long = buffer.writableBytes) {57
requireTransfer(count, buffer.writableBytes)58
var remaining = count59
while (remaining > 0L) {60
val progress = readInto(buffer, remaining)61
requireProgress(progress, remaining)62
if (progress == 0L) throw UnexpectedEofException()63
remaining -= progress64
}65
}67
/**68
* Writes exactly the requested count or throws WriteZeroException when the sink makes no progress.69
*/70
public fun ByteSink.writeFully(buffer: NativeByteBuffer, count: Long = buffer.readableBytes) {71
requireTransfer(count, buffer.readableBytes)72
var remaining = count73
while (remaining > 0L) {74
val progress = writeFrom(buffer, remaining)75
requireProgress(progress, remaining)76
if (progress == 0L) throw WriteZeroException()77
remaining -= progress78
}79
}81
/**82
* Copies input to a destination and returns actual bytes transferred. Byte-source copies borrow83
* both endpoints; path copies own their temporary handles and honor replaceExisting.84
*/85
public fun ByteSource.copyTo(destination: ByteSink, bufferSize: Long = 8192L): Long {86
return newIoBuffer(bufferSize).use { buffer ->87
var total = 0L88
while (true) {89
buffer.clear()90
val count = readInto(buffer)91
requireProgress(count, buffer.capacity)92
if (count == 0L) break93
if (count > Long.MAX_VALUE - total) throw ArithmeticException()94
destination.writeFully(buffer, count)95
total += count96
}97
total98
}99
}101
/**102
* Collects all remaining input into an independent byte array. Path overloads open and close their103
* own file handle.104
*/105
public fun ByteSource.readAllBytes(): Array<Byte> {106
val result = ByteArrayBuilder()107
NativeByteBuffer.allocate(8192L).use { buffer ->108
while (true) {109
buffer.clear()110
val count = readInto(buffer)111
requireProgress(count, buffer.capacity)112
if (count == 0L) break113
result.append(buffer.readBytes(count))114
}115
}116
return result.toByteArray()117
}119
/**120
* Reads into the selected writable buffer or array region, returning the actual byte count and121
* allowing short reads.122
*/123
public fun ByteSource.readInto(array: MutableArray<Byte>, offset: Long = 0L, count: Long = array.size - offset): Long {124
requireArrayTransfer(array.size, offset, count)125
if (count == 0L) return 0L126
return NativeByteBuffer.allocate(count).use { buffer ->127
val read = readInto(buffer, count)128
requireProgress(read, count)129
buffer.readInto(array, offset, read)130
read131
}132
}134
/**135
* Writes the selected readable buffer or array region, returning the accepted byte count and136
* allowing short writes.137
*/138
public fun ByteSink.writeFrom(array: List<Byte>, offset: Long = 0L, count: Long = array.size - offset): Long {139
requireArrayTransfer(array.size, offset, count)140
if (count == 0L) return 0L141
return NativeByteBuffer.allocate(count).use { buffer ->142
buffer.writeBytes(array, offset, count)143
val written = writeFrom(buffer, count)144
requireProgress(written, count)145
if (written == 0L) throw WriteZeroException()146
written147
}148
}150
/**151
* Writes all supplied bytes. Path overloads create/open and close their own file handle, with152
* explicit append selection.153
*/154
public fun ByteSink.writeAllBytes(bytes: List<Byte>) {155
NativeByteBuffer.allocate(bytes.size).use { buffer -> buffer.writeBytes(bytes); writeFully(buffer) }156
}158
internal fun requireTransfer(count: Long, available: Long) {159
if (count < 0L) throw IllegalArgumentException(Some("negative transfer count"))160
if (count > available) throw IndexOutOfBoundsException()161
}163
internal fun requireArrayTransfer(size: Long, offset: Long, count: Long) {164
if (count < 0L) throw IllegalArgumentException(Some("negative transfer count"))165
if (offset < 0L || offset > size || count > size - offset) throw IndexOutOfBoundsException()166
}168
internal fun requireProgress(count: Long, requested: Long) {169
if (count < 0L || count > requested) throw IllegalStateException(Some("invalid IO progress"))170
}172
internal fun newIoBuffer(capacity: Long): NativeByteBuffer {173
if (capacity <= 0L) throw IllegalArgumentException(Some("buffer capacity must be positive"))174
return NativeByteBuffer.allocate(capacity)175
}177
internal fun transferBytes(source: NativeByteBuffer, destination: NativeByteBuffer, count: Long) {178
if (count == 0L) return179
@Unsafe {180
destination.withWritableBytes { output, _ ->181
@Unsafe {182
source.withReadableBytes { input, _ -> @Unsafe { nativeMemoryMove(output!!, input!!, count) } }183
}184
count185
}186
}187
source.skip(count)188
}