Scoop API

scoop:scoop.platform:0.1.0

src/io/buffered.scoop

Selected production source · src/io/buffered.scoop

1package scoop.platform.io
3import scoop.platform.memory.NativeByteBuffer
5/**
6 * A synchronous read-ahead adapter. Its ordinary constructor borrows the source; takeOwnership
7 * transfers source cleanup responsibility. Operations on one adapter must not overlap.
8 */
9public class BufferedSource private constructor(
10 private val source: ByteSource,
11 private val internalBuffer: NativeByteBuffer,
12 private val owner: Closeable?
13) : ByteSource, Closeable {
14 private var closed: Boolean = false
15 private var active: Boolean = false
16 private var eof: Boolean = false
17 /**
18 * Whether this owned resource has been closed.
19 */
20 public val isClosed: Boolean get() = closed
22 public constructor(source: ByteSource, capacity: Long = 8192L) : this(source, newIoBuffer(capacity), None) {}
24 /**
25 * Factories with explicit underlying-endpoint ownership transfer.
26 */
27 public companion object {
28 /**
29 * Creates a buffer that assumes close responsibility for the underlying source.
30 */
31 public fun <T> takeOwnership(source: T, capacity: Long = 8192L): BufferedSource
32 where T : ByteSource, T : Closeable = BufferedSource(source, newIoBuffer(capacity), Some(source))
33 }
35 /**
36 * Reads into the selected writable region, returning actual bytes and permitting short reads.
37 * Zero for a positive request denotes EOF.
38 */
39 public override fun readInto(buffer: NativeByteBuffer, count: Long): Long {
40 requireIdle(closed, active)
41 requireTransfer(count, buffer.writableBytes)
42 if (count == 0L) return 0L
43 active = true
44 try {
45 if (internalBuffer.readableBytes == 0L && !eof) {
46 internalBuffer.clear()
47 val received = source.readInto(internalBuffer)
48 requireProgress(received, internalBuffer.capacity)
49 eof = received == 0L
50 }
51 val available = internalBuffer.readableBytes
52 val copied = if (count < available) count else available
53 transferBytes(internalBuffer, buffer, copied)
54 return copied
55 } finally { active = false }
56 }
58 /**
59 * Closes internal buffering and the source only when ownership was transferred.
60 */
61 public override fun close() {
62 if (closed) return
63 requireIdle(closed, active)
64 closed = true
65 closeBufferAndOwner(internalBuffer, owner, None)
66 }
70/**
71 * A synchronous buffered output adapter. Its ordinary constructor borrows the sink; takeOwnership
72 * transfers cleanup responsibility. Close flushes pending output and cleans up owned resources
73 * even if flushing fails.
74 */
75public class BufferedSink private constructor(
76 private val sink: ByteSink,
77 private val internalBuffer: NativeByteBuffer,
78 private val owner: Closeable?
79) : ByteSink, Closeable {
80 private var closed: Boolean = false
81 private var active: Boolean = false
82 /**
83 * Whether this owned resource has been closed.
84 */
85 public val isClosed: Boolean get() = closed
87 public constructor(sink: ByteSink, capacity: Long = 8192L) : this(sink, newIoBuffer(capacity), None) {}
89 /**
90 * Factories with explicit underlying-endpoint ownership transfer.
91 */
92 public companion object {
93 /**
94 * Creates a buffer that assumes close responsibility for the underlying sink.
95 */
96 public fun <T> takeOwnership(sink: T, capacity: Long = 8192L): BufferedSink
97 where T : ByteSink, T : Closeable = BufferedSink(sink, newIoBuffer(capacity), Some(sink))
98 }
100 /**
101 * Consumes the selected readable region, returning the actual accepted byte count and
102 * permitting short writes.
103 */
104 public override fun writeFrom(buffer: NativeByteBuffer, count: Long): Long {
105 requireIdle(closed, active)
106 requireTransfer(count, buffer.readableBytes)
107 if (count == 0L) return 0L
108 active = true
109 try {
110 if (internalBuffer.writableBytes == 0L) drain()
111 val available = internalBuffer.writableBytes
112 val copied = if (count < available) count else available
113 transferBytes(buffer, internalBuffer, copied)
114 return copied
115 } finally { active = false }
116 }
118 private fun drain() {
119 try { sink.writeFully(internalBuffer) }
120 finally { internalBuffer.compact() }
121 }
123 /**
124 * Writes all pending buffered bytes and flushes the underlying sink.
125 */
126 public override fun flush() {
127 requireIdle(closed, active)
128 active = true
129 try { drain(); sink.flush() }
130 finally { active = false }
131 }
133 // Flushes pending output, then closes owned resources even when flushing fails.
134 /**
135 * Flushes output, then releases internal buffering and an owned sink even when flushing fails.
136 */
137 public override fun close() {
138 if (closed) return
139 requireIdle(closed, active)
140 closed = true
141 var failure: Throwable? = None
142 try { drain(); sink.flush() }
143 catch (error: Throwable) { failure = Some(error) }
144 closeBufferAndOwner(internalBuffer, owner, failure)
145 }
149private fun requireIdle(closed: Boolean, active: Boolean) {
150 if (closed) throw IllegalStateException(Some("stream is closed"))
151 if (active) throw IllegalStateException(Some("stream operation is active"))
154private fun closeBufferAndOwner(internalBuffer: NativeByteBuffer, owner: Closeable?, initial: Throwable?) {
155 var failure = initial
156 try { internalBuffer.close() }
157 catch (error: Throwable) { failure = Some(appendCleanupFailure(failure, error)) }
158 when (owner) {
159 case Some(val value) -> {
160 try { value.close() }
161 catch (error: Throwable) { failure = Some(appendCleanupFailure(failure, error)) }
162 }
163 case None -> {}
164 }
165 when (failure) {
166 case Some(val error) -> throw error
167 case None -> {}
168 }
171private fun appendCleanupFailure(previous: Throwable?, next: Throwable): Throwable = when (previous) {
172 case Some(val error) -> ResourceUseException(error, next)
173 case None -> next