scoop:scoop.platform:0.1.0
src/io/buffered.scoop
Selected production source · src/io/buffered.scoop
1
package scoop.platform.io3
import scoop.platform.memory.NativeByteBuffer5
/**6
* A synchronous read-ahead adapter. Its ordinary constructor borrows the source; takeOwnership7
* transfers source cleanup responsibility. Operations on one adapter must not overlap.8
*/9
public 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 = false15
private var active: Boolean = false16
private var eof: Boolean = false17
/**18
* Whether this owned resource has been closed.19
*/20
public val isClosed: Boolean get() = closed22
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): BufferedSource32
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 0L43
active = true44
try {45
if (internalBuffer.readableBytes == 0L && !eof) {46
internalBuffer.clear()47
val received = source.readInto(internalBuffer)48
requireProgress(received, internalBuffer.capacity)49
eof = received == 0L50
}51
val available = internalBuffer.readableBytes52
val copied = if (count < available) count else available53
transferBytes(internalBuffer, buffer, copied)54
return copied55
} 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) return63
requireIdle(closed, active)64
closed = true65
closeBufferAndOwner(internalBuffer, owner, None)66
}68
}70
/**71
* A synchronous buffered output adapter. Its ordinary constructor borrows the sink; takeOwnership72
* transfers cleanup responsibility. Close flushes pending output and cleans up owned resources73
* even if flushing fails.74
*/75
public 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 = false81
private var active: Boolean = false82
/**83
* Whether this owned resource has been closed.84
*/85
public val isClosed: Boolean get() = closed87
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): BufferedSink97
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 and102
* 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 0L108
active = true109
try {110
if (internalBuffer.writableBytes == 0L) drain()111
val available = internalBuffer.writableBytes112
val copied = if (count < available) count else available113
transferBytes(buffer, internalBuffer, copied)114
return copied115
} 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 = true129
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) return139
requireIdle(closed, active)140
closed = true141
var failure: Throwable? = None142
try { drain(); sink.flush() }143
catch (error: Throwable) { failure = Some(error) }144
closeBufferAndOwner(internalBuffer, owner, failure)145
}147
}149
private 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"))152
}154
private fun closeBufferAndOwner(internalBuffer: NativeByteBuffer, owner: Closeable?, initial: Throwable?) {155
var failure = initial156
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 error167
case None -> {}168
}169
}171
private fun appendCleanupFailure(previous: Throwable?, next: Throwable): Throwable = when (previous) {172
case Some(val error) -> ResourceUseException(error, next)173
case None -> next174
}