Scoop API

scoop:scoop.compression:0.1.0

src/codec.scoop

Selected production source · src/codec.scoop

1package scoop.compression
3import scoop.platform.memory.NativeByteBuffer
5internal class CompressionCodec(
6 private var handle: ULong, private var pressure: Long,
7 val format: CompressionFormat, private val encoding: Boolean
8) : Closeable {
9 private val busy: AtomicBoolean = AtomicBoolean(false)
10 private var phase: Int = 0
11 private var inputEnded: Boolean = false
12 var failure: Throwable? = None
13 private var received: Long = 0L
14 private var delivered: Long = 0L
16 val totals: CompressionTotals get() = CompressionTotals(received, delivered)
17 val finished: Boolean get() = phase == 3
19 fun step(input: NativeByteBuffer?, count: Long, output: NativeByteBuffer, capacity: Long, action: Int): CompressionStep {
20 enter()
21 try {
22 if (phase >= 4) throw IllegalStateException(Some("compression codec is closed or failed"))
23 if (count < 0L || capacity < 0L) throw IllegalArgumentException(Some("negative codec buffer count"))
24 val readable = when (input) { case Some(val buffer) -> buffer.readableBytes; case None -> 0L }
25 if (count > readable || capacity > output.writableBytes) throw IndexOutOfBoundsException()
26 if ((inputEnded || phase == 3) && count > 0L) throw IllegalStateException(Some("compression input has ended"))
27 if (phase == 3) return CompressionStep(0L, 0L, CompressionState.Finished)
28 if (encoding && ((phase == 1 && action != 1) || (phase == 2 && action != 2))) {
29 throw IllegalStateException(Some("complete the active flush or finish first"))
30 }
31 val native = callCompression(handle, input, count, output, capacity, if (inputEnded) 2 else action)
32 if (encoding && action != 0) phase = action
33 account(native.memory)
34 received = addCompressionCount(received, native.consumed)
35 delivered = addCompressionCount(delivered, native.produced)
36 if (!encoding && action == 2 && native.consumed == count) inputEnded = true
37 checkFailure(native.kind, native.code)
38 val state = when (native.status) {
39 case 0 -> CompressionState.NeedInput
40 case 1 -> CompressionState.NeedOutput
41 case 2 -> CompressionState.Finished
42 else -> throw IllegalStateException(Some("invalid compression progress"))
43 }
44 if (state == CompressionState.Finished) phase = 3
45 else if (phase == 1 && state == CompressionState.NeedInput) phase = 0
46 return CompressionStep(native.consumed, native.produced, state)
47 } finally { busy.store(false) }
48 }
50 fun nextMessage() {
51 enter()
52 try {
53 if (phase != 3) throw IllegalStateException(Some("finish the current DEFLATE message first"))
54 @Unsafe {
55 val info = compressionNextMessage(handle)
56 account(info.memory)
57 checkFailure(info.kind, info.code)
58 }
59 phase = 0
60 inputEnded = false
61 received = 0L
62 delivered = 0L
63 } finally { busy.store(false) }
64 }
66 private fun checkFailure(kind: Int, code: Int) {
67 if (kind == 0) return
68 @Unsafe {
69 val error = compressionError(kind, code, String.fromCString(compressionMessage(handle)), format)
70 phase = 4
71 failure = Some(error)
72 throw error
73 }
74 }
76 private fun account(memory: Long) {
77 @Unsafe { gcAdjustPressure(memory - pressure) }
78 pressure = memory
79 }
81 private fun enter() {
82 if (!busy.compareAndSet(false, true)) throw IllegalStateException(Some("compression codec is already in use"))
83 }
85 public override fun close() {
86 enter()
87 try {
88 val owned = handle
89 handle = 0UL
90 phase = 5
91 if (owned != 0UL) @Unsafe { compressionFree(owned); gcAdjustPressure(-pressure); pressure = 0L }
92 } finally { busy.store(false) }
93 }
95 release { if (handle != 0UL) @Unsafe { compressionFree(handle); gcAdjustPressure(-pressure) } }
98internal fun addCompressionCount(left: Long, right: Long): Long {
99 if (right > Long.MAX_VALUE - left) throw ArithmeticException()
100 return left + right