11package dev.silenium.libs.flows.test
22
33import dev.silenium.libs.flows.api.FlowItem
4- import dev.silenium.libs.flows.api.Transformer
5- import dev.silenium.libs.flows.base.SourceBase
6- import kotlinx.coroutines.*
4+ import dev.silenium.libs.flows.base.JobTransformerBase
5+ import kotlinx.coroutines.CoroutineScope
6+ import kotlinx.coroutines.Dispatchers
77import kotlinx.coroutines.channels.Channel
8- import java.util.*
8+ import kotlinx.coroutines.delay
99import kotlin.io.encoding.Base64
1010import kotlin.io.encoding.ExperimentalEncodingApi
1111import kotlin.random.Random
1212import kotlin.random.nextInt
1313import kotlin.time.Duration.Companion.milliseconds
1414
15- class Base64Decoder : SourceBase < ByteArray , DataType >(), Transformer<Base64Buffer, DataType, ByteArray, DataType> {
16- override val inputMetadata : MutableMap < UInt , DataType > =
17- Collections .synchronizedMap(mutableMapOf< UInt , DataType >())
15+ @OptIn( ExperimentalUnsignedTypes :: class )
16+ class Base64Decoder :
17+ JobTransformerBase < Base64Buffer , DataType , ByteArray , DataType >(CoroutineScope ( Dispatchers . IO ), 0u ) {
1818 private val queue = Channel <FlowItem <Base64Buffer , DataType >>(capacity = 4 )
1919
2020 @OptIn(ExperimentalEncodingApi ::class )
21- private val processor = CoroutineScope (Dispatchers .Default ).async {
21+ override suspend fun CoroutineScope.run () {
22+ println (" Base64Decoder.run" )
2223 for (item in queue) {
2324 delay(Random .nextInt(1 .. 250 ).milliseconds)
2425 val array = ByteArray (item.value.buffer.remaining())
@@ -28,11 +29,9 @@ class Base64Decoder : SourceBase<ByteArray, DataType>(), Transformer<Base64Buffe
2829 }
2930 }
3031
31- override fun configure (pad : UInt , metadata : DataType ): Result <Unit > {
32- check(metadata == DataType .BASE64 ) { " metadata must be BASE64" }
33- this .inputMetadata[pad] = metadata
34- this .outputMetadata_[pad] = DataType .PLAIN
35- return Result .success(Unit )
32+ override fun outputMetadata (inputMetadata : DataType ): DataType {
33+ check(inputMetadata == DataType .BASE64 ) { " metadata must be BASE64" }
34+ return DataType .PLAIN
3635 }
3736
3837 override suspend fun receive (item : FlowItem <Base64Buffer , DataType >): Result <Unit > = runCatching {
@@ -41,7 +40,6 @@ class Base64Decoder : SourceBase<ByteArray, DataType>(), Transformer<Base64Buffe
4140
4241 override fun close () {
4342 queue.close()
44- runBlocking { processor.join() }
4543 super .close()
4644 }
4745}
0 commit comments