scoop:scoop.charset:0.1.0
src/transcoder.scoop
Selected production source · src/transcoder.scoop
1
package scoop.charset3
import scoop.platform.memory.NativeByteBuffer5
/**6
* A stateful native converter between two encodings. Close it after use, including after a7
* conversion failure.8
*9
* Calls must not overlap. Once final input is accepted, no further nonempty input may be supplied;10
* continue draining output until [CharsetState.Finished].11
*/12
public class CharsetTranscoder public constructor(13
/**14
* The encoding of bytes read from the input buffer.15
*/16
public val source: Charset,17
/**18
* The encoding of bytes appended to the output buffer.19
*/20
public val destination: Charset,21
options: CharsetTranscodeOptions = CharsetTranscodeOptions()22
) : Closeable {23
private var handle: ULong = createCharset(source, destination, options)24
private var ending: Boolean = false25
private var finished: Boolean = false26
private var failed: Boolean = false27
private val busy: AtomicBoolean = AtomicBoolean(false)29
/**30
* Converts readable input into writable output and advances both buffers by the returned byte31
* counts. Set `endInput` for the final input; repeat calls as requested by [CharsetStep.state]32
* until finished.33
*34
* @throws IllegalStateException The converter is closed, failed, already in use, or receives35
* input after completion.36
* @throws CharsetException Conversion violates the selected text or BOM policy.37
*/38
public fun process(input: NativeByteBuffer, output: NativeByteBuffer, endInput: Boolean = false): CharsetStep {39
enter()40
try {41
if (handle == 0UL || failed) throw IllegalStateException(Some("transcoder is closed or failed"))42
val count = input.readableBytes43
if (ending && count > 0L) throw IllegalStateException(Some("charset input has ended"))44
if (finished) return CharsetStep(0L, 0L, CharsetState.Finished)45
val step = processCharset(handle, input, output, endInput || ending)46
if (endInput && step.consumed == count) ending = true47
if (step.code != 0) {48
failed = true49
checkCharset(step.code, step.backend, "transcode", source.name, destination.name, Some(step.offset))50
}51
val state = when (step.status) {52
case 0 -> CharsetState.NeedInput53
case 1 -> CharsetState.NeedOutput54
case 2 -> CharsetState.Finished55
else -> throw IllegalStateException(Some("invalid charset progress"))56
}57
finished = state == CharsetState.Finished58
return CharsetStep(step.consumed, step.produced, state)59
} finally { busy.store(false) }60
}62
private fun enter() {63
if (!busy.compareAndSet(false, true)) throw IllegalStateException(Some("transcoder is already in use"))64
}66
/**67
* Releases the native conversion state. Repeated closes are allowed, but a close must not68
* overlap a conversion call.69
*/70
public override fun close() {71
enter()72
try {73
val owned = handle74
handle = 0UL75
if (owned != 0UL) @Unsafe { charsetFree(owned) }76
} finally { busy.store(false) }77
}79
release { if (handle != 0UL) @Unsafe { charsetFree(handle) } }80
}