Scoop API

scoop:scoop.compression:0.1.0

src/streams/source_impl.scoop

Selected production source · src/streams/source_impl.scoop

1package scoop.compression
3import scoop.platform.io.ByteSource
4import scoop.platform.memory.NativeByteBuffer
6internal 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.totals
12 val failure: Throwable? get() = guard.failure
14 public override fun readInto(buffer: NativeByteBuffer, count: Long): Long {
15 guard.enter()
16 try {
17 requireCompressionTransfer(count, buffer.writableBytes)
18 if (count == 0L) return 0L
19 try {
20 while (true) {
21 if (state.finished) return 0L
22 if (state.needsInput) fill()
23 val step = state.step(buffer, count)
24 if (step.produced > 0L) return step.produced
25 }
26 } catch (error: Throwable) { guard.failure = Some(error); throw error }
27 } finally { guard.leave() }
28 }
30 private fun fill() {
31 val input = state.buffer
32 input.compact()
33 val before = input.writePosition
34 val requested = input.writableBytes
35 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() }