From 3d82a55059f71c3bce9cee6225dee5753d413146 Mon Sep 17 00:00:00 2001 From: Jules Ivanic Date: Mon, 2 Dec 2024 23:15:11 +1100 Subject: [PATCH] Optimise `zio.http.Body.FileBody.asStream` code (#3215) --- .../shared/src/main/scala/zio/http/Body.scala | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/zio-http/shared/src/main/scala/zio/http/Body.scala b/zio-http/shared/src/main/scala/zio/http/Body.scala index a72fdce456..00ba269aab 100644 --- a/zio-http/shared/src/main/scala/zio/http/Body.scala +++ b/zio-http/shared/src/main/scala/zio/http/Body.scala @@ -567,21 +567,27 @@ object Body { override def asStream(implicit trace: Trace): ZStream[Any, Throwable, Byte] = ZStream.unwrap { - for { - file <- ZIO.attempt(file) - fs <- ZIO.attemptBlocking(new FileInputStream(file)) - size <- ZIO.attemptBlocking(Math.min(chunkSize.toLong, file.length()).toInt) - } yield ZStream - .repeatZIOOption[Any, Throwable, Chunk[Byte]] { - for { - buffer <- ZIO.succeed(new Array[Byte](size)) - len <- ZIO.attemptBlocking(fs.read(buffer)).mapError(Some(_)) - bytes <- - if (len > 0) ZIO.succeed(Chunk.fromArray(buffer.slice(0, len))) - else ZIO.fail(None) - } yield bytes - } - .ensuring(ZIO.attemptBlocking(fs.close()).ignoreLogged) + ZIO.blocking { + for { + r <- ZIO.attempt { + val fs = new FileInputStream(file) + val size = Math.min(chunkSize.toLong, file.length()).toInt + + (fs, size) + } + (fs, size) = r + } yield ZStream + .repeatZIOOption[Any, Throwable, Chunk[Byte]] { + for { + buffer <- ZIO.succeed(new Array[Byte](size)) + len <- ZIO.attempt(fs.read(buffer)).mapError(Some(_)) + bytes <- + if (len > 0) ZIO.succeed(Chunk.fromArray(buffer.slice(0, len))) + else ZIO.fail(None) + } yield bytes + } + .ensuring(ZIO.attempt(fs.close()).ignoreLogged) + } }.flattenChunks override def contentType(newContentType: Body.ContentType): Body = copy(contentType = Some(newContentType))