1
1
package dev.silenium.libs.flows.test
2
2
3
3
import 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
7
7
import kotlinx.coroutines.channels.Channel
8
- import java.util.*
8
+ import kotlinx.coroutines.delay
9
9
import kotlin.io.encoding.Base64
10
10
import kotlin.io.encoding.ExperimentalEncodingApi
11
11
import kotlin.random.Random
12
12
import kotlin.random.nextInt
13
13
import kotlin.time.Duration.Companion.milliseconds
14
14
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 ) {
18
18
private val queue = Channel <FlowItem <Base64Buffer , DataType >>(capacity = 4 )
19
19
20
20
@OptIn(ExperimentalEncodingApi ::class )
21
- private val processor = CoroutineScope (Dispatchers .Default ).async {
21
+ override suspend fun CoroutineScope.run () {
22
+ println (" Base64Decoder.run" )
22
23
for (item in queue) {
23
24
delay(Random .nextInt(1 .. 250 ).milliseconds)
24
25
val array = ByteArray (item.value.buffer.remaining())
@@ -28,11 +29,9 @@ class Base64Decoder : SourceBase<ByteArray, DataType>(), Transformer<Base64Buffe
28
29
}
29
30
}
30
31
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
36
35
}
37
36
38
37
override suspend fun receive (item : FlowItem <Base64Buffer , DataType >): Result <Unit > = runCatching {
@@ -41,7 +40,6 @@ class Base64Decoder : SourceBase<ByteArray, DataType>(), Transformer<Base64Buffe
41
40
42
41
override fun close () {
43
42
queue.close()
44
- runBlocking { processor.join() }
45
43
super .close()
46
44
}
47
45
}
0 commit comments