From 986a8d861b47250082385f181e4662435edf03cc Mon Sep 17 00:00:00 2001 From: Jules Ivanic Date: Tue, 19 Nov 2024 18:13:04 +1100 Subject: [PATCH 1/2] Optimise `zio.http.Body.FileBody.asStream` code --- .../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..4736dba1d5 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)) From 1d28d104d2445fb7fa5195931f2ae8868b7a480f Mon Sep 17 00:00:00 2001 From: Jules Ivanic Date: Tue, 26 Nov 2024 18:20:53 +1100 Subject: [PATCH 2/2] fmt --- zio-http/shared/src/main/scala/zio/http/Body.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 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 4736dba1d5..00ba269aab 100644 --- a/zio-http/shared/src/main/scala/zio/http/Body.scala +++ b/zio-http/shared/src/main/scala/zio/http/Body.scala @@ -570,7 +570,7 @@ object Body { ZIO.blocking { for { r <- ZIO.attempt { - val fs = new FileInputStream(file) + val fs = new FileInputStream(file) val size = Math.min(chunkSize.toLong, file.length()).toInt (fs, size) @@ -580,8 +580,8 @@ object Body { .repeatZIOOption[Any, Throwable, Chunk[Byte]] { for { buffer <- ZIO.succeed(new Array[Byte](size)) - len <- ZIO.attempt(fs.read(buffer)).mapError(Some(_)) - bytes <- + 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