File tree Expand file tree Collapse file tree 1 file changed +12
-10
lines changed
firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/common/util Expand file tree Collapse file tree 1 file changed +12
-10
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ import kotlinx.coroutines.cancel
25
25
import kotlinx.coroutines.currentCoroutineContext
26
26
import kotlinx.coroutines.flow.Flow
27
27
import kotlinx.coroutines.flow.flow
28
+ import kotlinx.coroutines.flow.fold
28
29
29
30
/* *
30
31
* Removes the last character from the [StringBuilder].
@@ -67,18 +68,19 @@ internal fun Flow<ByteArray>.accumulateUntil(
67
68
minSize : Int ,
68
69
emitLeftOvers : Boolean = false
69
70
): Flow <ByteArray > = flow {
70
- val buffer = ByteArrayOutputStream ()
71
-
72
- collect {
73
- buffer.write(it, 0 , it.size)
74
- if (buffer.size() >= minSize) {
75
- emit(buffer.toByteArray())
76
- buffer.reset()
71
+ val remaining =
72
+ fold(ByteArrayOutputStream ()) { buffer, it ->
73
+ buffer.apply {
74
+ write(it, 0 , it.size)
75
+ if (size() >= minSize) {
76
+ emit(toByteArray())
77
+ reset()
78
+ }
79
+ }
77
80
}
78
- }
79
81
80
- if (emitLeftOvers && buffer .size() > 0 ) {
81
- emit(buffer .toByteArray())
82
+ if (emitLeftOvers && remaining .size() > 0 ) {
83
+ emit(remaining .toByteArray())
82
84
}
83
85
}
84
86
You can’t perform that action at this time.
0 commit comments