3

I am using BufferedOutputStream

suspend fun write(byteArray: ByteArray) {    
        bos.write(byteArray)
}

But when I add suspend keyword I got warning:

Inappropriate blocking method call

What is the correct way to use output stream with coroutines?

Pavel Poley
  • 5,307
  • 4
  • 35
  • 66
  • You would need to rely on Java NIO (https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/package-summary.html). – João Dias Jan 18 '22 at 15:32
  • 1
    Why do you want to make this function `suspend`? – Joffrey Jan 18 '22 at 15:43
  • I want to make it suspend, to let know other classes know that this method should run inside coroutine/worker thread – Pavel Poley Jan 18 '22 at 16:06
  • Coroutines and worker threads are completely different things, though. You don't use coroutines to indicate they should be called off the main GUI thread; quite the opposite -- you declare they are _usable_ from the GUI thread without the danger of blocking it. – Marko Topolnik Jan 19 '22 at 13:15

2 Answers2

4

OutputStream.write is a blocking function, and by convention suspend functions must never block. You can wrap it in withContext so it uses the IO dispatcher to do it appropriately. However, it is possible this won't make the warning go away because the Kotlin lint is kind of buggy about false positives for this issue.

suspend fun write(byteArray: ByteArray) = withContext(Dispatchers.IO) {    
    bos.write(byteArray)
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • Yes, the warning not go away – Pavel Poley Jan 18 '22 at 16:19
  • 2
    Try using Kotlin 1.6.10 and Kotlin coroutines version 1.6.0. I think they've fixed the false positives recently. Since it's telling you "inappropriate blocking method call" instead of "Possibly blocking call in non-blocking context", I think you're using an older version of the coroutines library. – Tenfour04 Jan 18 '22 at 16:24
4

In general if there is a truly async alternative, it better suits the coroutines model. You could find callback-based APIs and wrap them into suspend functions using suspendCoroutine or suspendCancellableCoroutine.

However, more often than not, you need to deal with actual blocking IO.

In that case, the easiest is to simply run the blocking IO on the IO dispatcher using withContext(Dispatchers.IO):

suspend fun write(byteArray: ByteArray) = withContext(Dispatchers.IO) {    
    bos.write(byteArray)
}

However, you have to think about which level you're using the IO dispatcher at. If this method is quite low-level, maybe you should use withContext higher in the call stack, and just keep this method non-suspend.

Joffrey
  • 32,348
  • 6
  • 68
  • 100