-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
102 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 1 addition & 76 deletions
77
core/src/main/scala/sttp/client4/compression/Compressor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
core/src/main/scalajs/sttp/client4/compression/CompressorExtensions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package sttp.client4.compression | ||
|
||
trait CompressorExtensions { | ||
def default[R]: List[Compressor[R]] = Nil | ||
} |
5 changes: 5 additions & 0 deletions
5
core/src/main/scalajvm/sttp/client4/compression/CompressorExtensions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package sttp.client4.compression | ||
|
||
trait CompressorExtensions { | ||
def default[R]: List[Compressor[R]] = List(new GZipDefaultCompressor[R](), new DeflateDefaultCompressor[R]()) | ||
} |
File renamed without changes.
78 changes: 78 additions & 0 deletions
78
core/src/main/scalajvm/sttp/client4/compression/defaultCompressors.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package sttp.client4.compression | ||
|
||
import sttp.client4._ | ||
import sttp.model.Encodings | ||
|
||
import Compressor._ | ||
import java.util.zip.Deflater | ||
import java.util.zip.DeflaterInputStream | ||
import java.io.ByteArrayOutputStream | ||
|
||
class GZipDefaultCompressor[R] extends Compressor[R] { | ||
val encoding: String = Encodings.Gzip | ||
|
||
def apply(body: GenericRequestBody[R], encoding: String): GenericRequestBody[R] = | ||
body match { | ||
case NoBody => NoBody | ||
case StringBody(s, encoding, defaultContentType) => | ||
ByteArrayBody(byteArray(s.getBytes(encoding)), defaultContentType) | ||
case ByteArrayBody(b, defaultContentType) => ByteArrayBody(byteArray(b), defaultContentType) | ||
case ByteBufferBody(b, defaultContentType) => | ||
ByteArrayBody(byteArray(byteBufferToArray(b)), defaultContentType) | ||
case InputStreamBody(b, defaultContentType) => | ||
InputStreamBody(new GZIPCompressingInputStream(b), defaultContentType) | ||
case StreamBody(b) => streamsNotSupported | ||
case FileBody(f, defaultContentType) => | ||
InputStreamBody(new GZIPCompressingInputStream(f.openStream()), defaultContentType) | ||
case MultipartStreamBody(parts) => compressingMultipartBodiesNotSupported | ||
case BasicMultipartBody(parts) => compressingMultipartBodiesNotSupported | ||
} | ||
|
||
private def byteArray(bytes: Array[Byte]): Array[Byte] = { | ||
val bos = new java.io.ByteArrayOutputStream() | ||
val gzip = new java.util.zip.GZIPOutputStream(bos) | ||
gzip.write(bytes) | ||
gzip.close() | ||
bos.toByteArray() | ||
} | ||
} | ||
|
||
class DeflateDefaultCompressor[R] extends Compressor[R] { | ||
val encoding: String = Encodings.Deflate | ||
|
||
def apply(body: GenericRequestBody[R], encoding: String): GenericRequestBody[R] = | ||
body match { | ||
case NoBody => NoBody | ||
case StringBody(s, encoding, defaultContentType) => | ||
ByteArrayBody(byteArray(s.getBytes(encoding)), defaultContentType) | ||
case ByteArrayBody(b, defaultContentType) => ByteArrayBody(byteArray(b), defaultContentType) | ||
case ByteBufferBody(b, defaultContentType) => | ||
ByteArrayBody(byteArray(byteBufferToArray(b)), defaultContentType) | ||
case InputStreamBody(b, defaultContentType) => | ||
InputStreamBody(new DeflaterInputStream(b), defaultContentType) | ||
case StreamBody(b) => streamsNotSupported | ||
case FileBody(f, defaultContentType) => | ||
InputStreamBody(new DeflaterInputStream(f.openStream()), defaultContentType) | ||
case MultipartStreamBody(parts) => compressingMultipartBodiesNotSupported | ||
case BasicMultipartBody(parts) => compressingMultipartBodiesNotSupported | ||
} | ||
|
||
private def byteArray(bytes: Array[Byte]): Array[Byte] = { | ||
val deflater = new Deflater() | ||
try { | ||
deflater.setInput(bytes) | ||
deflater.finish() | ||
val byteArrayOutputStream = new ByteArrayOutputStream() | ||
val readBuffer = new Array[Byte](1024) | ||
|
||
while (!deflater.finished()) { | ||
val readCount = deflater.deflate(readBuffer) | ||
if (readCount > 0) { | ||
byteArrayOutputStream.write(readBuffer, 0, readCount) | ||
} | ||
} | ||
|
||
byteArrayOutputStream.toByteArray | ||
} finally deflater.end() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
core/src/main/scalanative/sttp/client4/compression/CompressorExtensions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package sttp.client4.compression | ||
|
||
trait CompressorExtensions { | ||
def default[R]: List[Compressor[R]] = Nil | ||
} |
3 changes: 1 addition & 2 deletions
3
...sion/GZIPCompressingInputStreamTest.scala → ...sion/GZIPCompressingInputStreamTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters