Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
Inappropriate blocking method call output stream write

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?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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)
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda