Scoop API

scoop:scoop.platform:0.1.0

src/io/streams.scoop

Selected production source · src/io/streams.scoop

1package scoop.platform.io
3import scoop.bytes.ByteArrayBuilder
4import scoop.platform.memory.NativeByteBuffer
5import scoop.platform.memory.nativeMemoryMove
7/**
8 * A synchronous byte source supporting short reads. Zero for a positive-size request denotes EOF.
9 */
10public 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): Long
18/**
19 * A synchronous byte sink supporting short writes and explicit flushing.
20 */
21public interface ByteSink {
22 /**
23 * Consumes the selected readable region, returning the actual accepted byte count and
24 * permitting short writes.
25 */
26 public fun writeFrom(buffer: NativeByteBuffer, count: Long): Long
27 /**
28 * Flushes pending output according to the sink's buffering contract.
29 */
30 public fun flush()
33/**
34 * An exact-size read reached EOF before the requested byte count.
35 */
36public class UnexpectedEofException public constructor() : Exception(Some("unexpected end of input"))
37/**
38 * An exact-size write could not make progress.
39 */
40public 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 and
44 * allowing short reads.
45 */
46public 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 and
49 * allowing short writes.
50 */
51public fun ByteSink.writeFrom(buffer: NativeByteBuffer): Long = writeFrom(buffer, buffer.readableBytes)
53/**
54 * Reads exactly the requested count or throws UnexpectedEofException on premature EOF.
55 */
56public fun ByteSource.readFully(buffer: NativeByteBuffer, count: Long = buffer.writableBytes) {
57 requireTransfer(count, buffer.writableBytes)
58 var remaining = count
59 while (remaining > 0L) {
60 val progress = readInto(buffer, remaining)
61 requireProgress(progress, remaining)
62 if (progress == 0L) throw UnexpectedEofException()
63 remaining -= progress
64 }
67/**
68 * Writes exactly the requested count or throws WriteZeroException when the sink makes no progress.
69 */
70public fun ByteSink.writeFully(buffer: NativeByteBuffer, count: Long = buffer.readableBytes) {
71 requireTransfer(count, buffer.readableBytes)
72 var remaining = count
73 while (remaining > 0L) {
74 val progress = writeFrom(buffer, remaining)
75 requireProgress(progress, remaining)
76 if (progress == 0L) throw WriteZeroException()
77 remaining -= progress
78 }
81/**
82 * Copies input to a destination and returns actual bytes transferred. Byte-source copies borrow
83 * both endpoints; path copies own their temporary handles and honor replaceExisting.
84 */
85public fun ByteSource.copyTo(destination: ByteSink, bufferSize: Long = 8192L): Long {
86 return newIoBuffer(bufferSize).use { buffer ->
87 var total = 0L
88 while (true) {
89 buffer.clear()
90 val count = readInto(buffer)
91 requireProgress(count, buffer.capacity)
92 if (count == 0L) break
93 if (count > Long.MAX_VALUE - total) throw ArithmeticException()
94 destination.writeFully(buffer, count)
95 total += count
96 }
97 total
98 }
101/**
102 * Collects all remaining input into an independent byte array. Path overloads open and close their
103 * own file handle.
104 */
105public 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) break
113 result.append(buffer.readBytes(count))
114 }
115 }
116 return result.toByteArray()
119/**
120 * Reads into the selected writable buffer or array region, returning the actual byte count and
121 * allowing short reads.
122 */
123public 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 0L
126 return NativeByteBuffer.allocate(count).use { buffer ->
127 val read = readInto(buffer, count)
128 requireProgress(read, count)
129 buffer.readInto(array, offset, read)
130 read
131 }
134/**
135 * Writes the selected readable buffer or array region, returning the accepted byte count and
136 * allowing short writes.
137 */
138public 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 0L
141 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 written
147 }
150/**
151 * Writes all supplied bytes. Path overloads create/open and close their own file handle, with
152 * explicit append selection.
153 */
154public fun ByteSink.writeAllBytes(bytes: List<Byte>) {
155 NativeByteBuffer.allocate(bytes.size).use { buffer -> buffer.writeBytes(bytes); writeFully(buffer) }
158internal fun requireTransfer(count: Long, available: Long) {
159 if (count < 0L) throw IllegalArgumentException(Some("negative transfer count"))
160 if (count > available) throw IndexOutOfBoundsException()
163internal 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()
168internal fun requireProgress(count: Long, requested: Long) {
169 if (count < 0L || count > requested) throw IllegalStateException(Some("invalid IO progress"))
172internal fun newIoBuffer(capacity: Long): NativeByteBuffer {
173 if (capacity <= 0L) throw IllegalArgumentException(Some("buffer capacity must be positive"))
174 return NativeByteBuffer.allocate(capacity)
177internal fun transferBytes(source: NativeByteBuffer, destination: NativeByteBuffer, count: Long) {
178 if (count == 0L) return
179 @Unsafe {
180 destination.withWritableBytes { output, _ ->
181 @Unsafe {
182 source.withReadableBytes { input, _ -> @Unsafe { nativeMemoryMove(output!!, input!!, count) } }
183 }
184 count
185 }
186 }
187 source.skip(count)