scoop:scoop.compression:0.1.0
src/streams/source_impl.scoop
Selected production source · src/streams/source_impl.scoop
1
package scoop.compression3
import scoop.platform.io.ByteSource4
import scoop.platform.memory.NativeByteBuffer6
internal class SyncCompressionSource(7
private val source: ByteSource, resources: CompressionIoResources, owner: Closeable?8
) : ByteSource, Closeable {9
private val state: CompressionReadState = CompressionReadState(resources)10
private val guard: SyncCompressionOwner = SyncCompressionOwner(resources, owner)11
val totals: CompressionTotals get() = state.totals12
val failure: Throwable? get() = guard.failure14
public override fun readInto(buffer: NativeByteBuffer, count: Long): Long {15
guard.enter()16
try {17
requireCompressionTransfer(count, buffer.writableBytes)18
if (count == 0L) return 0L19
try {20
while (true) {21
if (state.finished) return 0L22
if (state.needsInput) fill()23
val step = state.step(buffer, count)24
if (step.produced > 0L) return step.produced25
}26
} catch (error: Throwable) { guard.failure = Some(error); throw error }27
} finally { guard.leave() }28
}30
private fun fill() {31
val input = state.buffer32
input.compact()33
val before = input.writePosition34
val requested = input.writableBytes35
val count = source.readInto(input, requested)36
requireCompressionProgress(count, requested, input.writePosition - before)37
state.acceptInput(count)38
}40
public override fun close() { guard.close() }41
}