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)) diff --git a/zio-http/shared/src/main/scala/zio/http/codec/internal/EncoderDecoder.scala b/zio-http/shared/src/main/scala/zio/http/codec/internal/EncoderDecoder.scala index 705b432cbf..44d99b72dd 100644 --- a/zio-http/shared/src/main/scala/zio/http/codec/internal/EncoderDecoder.scala +++ b/zio-http/shared/src/main/scala/zio/http/codec/internal/EncoderDecoder.scala @@ -419,18 +419,21 @@ private[codec] object EncoderDecoder { private def decodeBody(config: CodecConfig, body: Body, inputs: Array[Any])(implicit trace: Trace, ): Task[Unit] = { - val codecs = flattened.content + val isNonMultiPart = inputs.length < 2 + if (isNonMultiPart) { + val codecs = flattened.content - if (inputs.length < 2) { - // non multi-part - codecs.headOption.map { codec => + // noinspection SimplifyUnlessInspection + if (codecs.isEmpty) ZIO.unit + else { + val codec = codecs.head codec .decodeFromBody(body, config) .mapBoth( - { err => HttpCodecError.MalformedBody(err.getMessage(), Some(err)) }, + { err => HttpCodecError.MalformedBody(err.getMessage, Some(err)) }, result => inputs(0) = result, ) - }.getOrElse(ZIO.unit) + } } else { // multi-part decodeForm(body.asMultipartFormStream, inputs, config) *> check(inputs)