Somewhat related to okio, so posting here hoping someone might have any ideas.
I have this kind of code:
fun <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?