scoop:scoop.compression:0.1.0
src/codec.scoop
Selected production source · src/codec.scoop
1
package scoop.compression3
import scoop.platform.memory.NativeByteBuffer5
internal class CompressionCodec(6
private var handle: ULong, private var pressure: Long,7
val format: CompressionFormat, private val encoding: Boolean8
) : Closeable {9
private val busy: AtomicBoolean = AtomicBoolean(false)10
private var phase: Int = 011
private var inputEnded: Boolean = false12
var failure: Throwable? = None13
private var received: Long = 0L14
private var delivered: Long = 0L16
val totals: CompressionTotals get() = CompressionTotals(received, delivered)17
val finished: Boolean get() = phase == 319
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 = action33
account(native.memory)34
received = addCompressionCount(received, native.consumed)35
delivered = addCompressionCount(delivered, native.produced)36
if (!encoding && action == 2 && native.consumed == count) inputEnded = true37
checkFailure(native.kind, native.code)38
val state = when (native.status) {39
case 0 -> CompressionState.NeedInput40
case 1 -> CompressionState.NeedOutput41
case 2 -> CompressionState.Finished42
else -> throw IllegalStateException(Some("invalid compression progress"))43
}44
if (state == CompressionState.Finished) phase = 345
else if (phase == 1 && state == CompressionState.NeedInput) phase = 046
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 = 060
inputEnded = false61
received = 0L62
delivered = 0L63
} finally { busy.store(false) }64
}66
private fun checkFailure(kind: Int, code: Int) {67
if (kind == 0) return68
@Unsafe {69
val error = compressionError(kind, code, String.fromCString(compressionMessage(handle)), format)70
phase = 471
failure = Some(error)72
throw error73
}74
}76
private fun account(memory: Long) {77
@Unsafe { gcAdjustPressure(memory - pressure) }78
pressure = memory79
}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 = handle89
handle = 0UL90
phase = 591
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) } }96
}98
internal fun addCompressionCount(left: Long, right: Long): Long {99
if (right > Long.MAX_VALUE - left) throw ArithmeticException()100
return left + right101
}