diff --git a/core/src/main/scala/sttp/client4/compression/Compressor.scala b/core/src/main/scala/sttp/client4/compression/Compressor.scala index 837633287d..3a008d79d6 100644 --- a/core/src/main/scala/sttp/client4/compression/Compressor.scala +++ b/core/src/main/scala/sttp/client4/compression/Compressor.scala @@ -4,7 +4,6 @@ import sttp.client4._ import sttp.model.Encodings import Compressor._ -import java.io.FileInputStream import java.nio.ByteBuffer import java.util.zip.DeflaterInputStream import java.util.zip.Deflater @@ -30,7 +29,7 @@ class GZipDefaultCompressor[R] extends Compressor[R] { InputStreamBody(new GZIPCompressingInputStream(b), defaultContentType) case StreamBody(b) => streamsNotSupported case FileBody(f, defaultContentType) => - InputStreamBody(new GZIPCompressingInputStream(new FileInputStream(f.toFile)), defaultContentType) + InputStreamBody(new GZIPCompressingInputStream(f.openStream()), defaultContentType) case MultipartStreamBody(parts) => compressingMultipartBodiesNotSupported case BasicMultipartBody(parts) => compressingMultipartBodiesNotSupported } @@ -59,7 +58,7 @@ class DeflateDefaultCompressor[R] extends Compressor[R] { InputStreamBody(new DeflaterInputStream(b), defaultContentType) case StreamBody(b) => streamsNotSupported case FileBody(f, defaultContentType) => - InputStreamBody(new DeflaterInputStream(new FileInputStream(f.toFile)), defaultContentType) + InputStreamBody(new DeflaterInputStream(f.openStream()), defaultContentType) case MultipartStreamBody(parts) => compressingMultipartBodiesNotSupported case BasicMultipartBody(parts) => compressingMultipartBodiesNotSupported } @@ -114,7 +113,7 @@ private[client4] object Compressor { case ByteArrayBody(b, _) => Some(b.length.toLong) case ByteBufferBody(b, _) => None case InputStreamBody(b, _) => None - case FileBody(f, _) => Some(f.toFile.length()) + case FileBody(f, _) => Some(f.length()) case StreamBody(_) => None case MultipartStreamBody(parts) => None case BasicMultipartBody(parts) => None diff --git a/core/src/main/scala/sttp/client4/testing/package.scala b/core/src/main/scala/sttp/client4/testing/package.scala index bf727143cf..c562714f01 100644 --- a/core/src/main/scala/sttp/client4/testing/package.scala +++ b/core/src/main/scala/sttp/client4/testing/package.scala @@ -15,7 +15,7 @@ package object testing { case ByteArrayBody(b, _) => new String(b) case ByteBufferBody(b, _) => new String(b.array()) case InputStreamBody(b, _) => new String(toByteArray(b)) - case FileBody(f, _) => f.readAsString + case FileBody(f, _) => f.readAsString() case StreamBody(_) => throw new IllegalArgumentException("The body of this request is a stream, cannot convert to String") case _: MultipartBody[_] => @@ -32,7 +32,7 @@ package object testing { case ByteArrayBody(b, _) => b case ByteBufferBody(b, _) => b.array() case InputStreamBody(b, _) => toByteArray(b) - case FileBody(f, _) => f.readAsByteArray + case FileBody(f, _) => f.readAsByteArray() case StreamBody(_) => throw new IllegalArgumentException("The body of this request is a stream, cannot convert to String") case _: MultipartBody[_] => diff --git a/core/src/main/scalajs/sttp/client4/internal/SttpFileExtensions.scala b/core/src/main/scalajs/sttp/client4/internal/SttpFileExtensions.scala index 55230f161f..285e9dd9b5 100644 --- a/core/src/main/scalajs/sttp/client4/internal/SttpFileExtensions.scala +++ b/core/src/main/scalajs/sttp/client4/internal/SttpFileExtensions.scala @@ -2,13 +2,18 @@ package sttp.client4.internal import org.scalajs.dom.File +import java.io.FileInputStream +import java.io.InputStream + // wrap a DomFile trait SttpFileExtensions { self: SttpFile => def toDomFile: File = underlying.asInstanceOf[File] - def readAsString: String = throw new UnsupportedOperationException() - def readAsByteArray: Array[Byte] = throw new UnsupportedOperationException() + def readAsString(): String = throw new UnsupportedOperationException() + def readAsByteArray(): Array[Byte] = throw new UnsupportedOperationException() + def openStream(): InputStream = throw new UnsupportedOperationException() + def length(): Long = throw new UnsupportedOperationException() } trait SttpFileCompanionExtensions { diff --git a/core/src/main/scalajvm/sttp/client4/internal/SttpFileExtensions.scala b/core/src/main/scalajvm/sttp/client4/internal/SttpFileExtensions.scala index 09af97cd42..bf1e433cbb 100644 --- a/core/src/main/scalajvm/sttp/client4/internal/SttpFileExtensions.scala +++ b/core/src/main/scalajvm/sttp/client4/internal/SttpFileExtensions.scala @@ -4,6 +4,8 @@ import java.nio.file.Files import java.nio.file.Path import scala.io.Source +import java.io.FileInputStream +import java.io.InputStream // wrap a Path trait SttpFileExtensions { self: SttpFile => @@ -11,13 +13,14 @@ trait SttpFileExtensions { self: SttpFile => def toPath: Path = underlying.asInstanceOf[Path] def toFile: java.io.File = toPath.toFile - def readAsString: String = { + def readAsString(): String = { val s = Source.fromFile(toFile, "UTF-8"); try s.getLines().mkString("\n") finally s.close() } - - def readAsByteArray: Array[Byte] = Files.readAllBytes(toPath) + def readAsByteArray(): Array[Byte] = Files.readAllBytes(toPath) + def openStream(): InputStream = new FileInputStream(toFile) + def length(): Long = toFile.length() } trait SttpFileCompanionExtensions { diff --git a/core/src/main/scalajvm/sttp/client4/internal/httpclient/BodyToHttpClient.scala b/core/src/main/scalajvm/sttp/client4/internal/httpclient/BodyToHttpClient.scala index 57f18d8eef..591fb7c532 100644 --- a/core/src/main/scalajvm/sttp/client4/internal/httpclient/BodyToHttpClient.scala +++ b/core/src/main/scalajvm/sttp/client4/internal/httpclient/BodyToHttpClient.scala @@ -3,7 +3,7 @@ package sttp.client4.internal.httpclient import sttp.capabilities.Streams import sttp.client4.internal.SttpToJavaConverters.toJavaSupplier import sttp.client4.internal.{throwNestedMultipartNotAllowed, Utf8} -import sttp.client4.internal.compression.{Compressor, DeflateDefaultCompressor, GZipDefaultCompressor} +import sttp.client4.compression.{Compressor, DeflateDefaultCompressor, GZipDefaultCompressor} import sttp.client4._ import sttp.model.{Header, HeaderNames, Part} import sttp.monad.MonadError diff --git a/core/src/main/scalanative/sttp/client4/internal/SttpFileExtensions.scala b/core/src/main/scalanative/sttp/client4/internal/SttpFileExtensions.scala index f5902158b8..e50dbbecaa 100644 --- a/core/src/main/scalanative/sttp/client4/internal/SttpFileExtensions.scala +++ b/core/src/main/scalanative/sttp/client4/internal/SttpFileExtensions.scala @@ -5,17 +5,21 @@ import java.nio.file.Path import scala.io.Source +import java.io.FileInputStream +import java.io.InputStream + trait SttpFileExtensions { self: SttpFile => def toPath: Path = underlying.asInstanceOf[Path] def toFile: java.io.File = toPath.toFile - def readAsString: String = { + def readAsString(): String = { val s = Source.fromFile(toFile, "UTF-8"); try s.getLines().mkString("\n") finally s.close() } - - def readAsByteArray: Array[Byte] = Files.readAllBytes(toPath) + def readAsByteArray(): Array[Byte] = Files.readAllBytes(toPath) + def openStream(): InputStream = new FileInputStream(toFile) + def length(): Long = toFile.length() } trait SttpFileCompanionExtensions { diff --git a/effects/cats/src/main/scalajvm/sttp/client4/httpclient/cats/HttpClientCatsBackend.scala b/effects/cats/src/main/scalajvm/sttp/client4/httpclient/cats/HttpClientCatsBackend.scala index 605bdccfd4..e57c3b5bb2 100644 --- a/effects/cats/src/main/scalajvm/sttp/client4/httpclient/cats/HttpClientCatsBackend.scala +++ b/effects/cats/src/main/scalajvm/sttp/client4/httpclient/cats/HttpClientCatsBackend.scala @@ -40,7 +40,7 @@ class HttpClientCatsBackend[F[_]: Async] private ( override protected def createSequencer: F[Sequencer[F]] = CatsSequencer.create - override protected val bodyToHttpClient: BodyToHttpClient[F, Nothing, R] = new BodyToHttpClient[F, Nothing, Ra] { + override protected val bodyToHttpClient: BodyToHttpClient[F, Nothing, R] = new BodyToHttpClient[F, Nothing, R] { override val streams: NoStreams = NoStreams override implicit val monad: MonadError[F] = self.monad