MJegorovas
07/28/2023, 10:55 AMfun <T> BufferedSource.forEachBlock(blockSize: Long, operator: Buffer.() -> T): List<T> {
val result = mutableListOf<T>()
val blockBuffer = Buffer()
while (true) {
request(blockSize)
if (exhausted()) break
blockBuffer.write(this, minOf(blockSize, buffer.size))
result += blockBuffer.operator()
}
return result
}
fun main() {
FileSystem.SYSTEM.read("filePath".toPath()) {
forEachBlock(4) {
// do some processing with buffer that might fail, if it happens, I want to exit from whole read operation;
}
}
}
Now I would like some way to return from forEachBlock
method in case operation inside it fails. return@read
can't be used here and return@forEachBlock
only returns from iteration and not whole forEachBlock
method. Could throw exception, but that seems excessive. Any ideas how it could be done?MJegorovas
07/28/2023, 1:03 PMinline
modifier be appropriate here?