diff --git a/akka-http-tests/src/test/scala/akka/http/scaladsl/coding/CodecSpecSupport.scala b/akka-http-tests/src/test/scala/akka/http/scaladsl/coding/CodecSpecSupport.scala index 2a4e3146fd..3e4a6929f9 100644 --- a/akka-http-tests/src/test/scala/akka/http/scaladsl/coding/CodecSpecSupport.scala +++ b/akka-http-tests/src/test/scala/akka/http/scaladsl/coding/CodecSpecSupport.scala @@ -22,9 +22,11 @@ trait CodecSpecSupport extends Matchers with BeforeAndAfterAll { self: Suite ⇒ i } + lazy val emptyTextBytes = ByteString(emptyText, "UTF8") lazy val smallTextBytes = ByteString(smallText, "UTF8") lazy val largeTextBytes = ByteString(largeText, "UTF8") + val emptyText = "" val smallText = "Yeah!" val largeText = """Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore diff --git a/akka-http-tests/src/test/scala/akka/http/scaladsl/coding/CoderSpec.scala b/akka-http-tests/src/test/scala/akka/http/scaladsl/coding/CoderSpec.scala index 5d016ee323..3529f58cd3 100644 --- a/akka-http-tests/src/test/scala/akka/http/scaladsl/coding/CoderSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/scaladsl/coding/CoderSpec.scala @@ -32,6 +32,18 @@ abstract class CoderSpec extends WordSpec with CodecSpecSupport with Inspectors def extraTests(): Unit = {} s"The ${Coder.encoding.value} codec" should { + "produce valid data on immediate finish" in { + streamDecode(Coder.newCompressor.finish()) should readAs(emptyText) + } + "properly encode an empty string" in { + streamDecode(ourEncode(emptyTextBytes)) should readAs(emptyText) + } + "properly decode an empty string" in { + ourDecode(streamEncode(emptyTextBytes)) should readAs(emptyText) + } + "properly round-trip encode/decode an empty string" in { + ourDecode(ourEncode(emptyTextBytes)) should readAs(emptyText) + } "properly encode a small string" in { streamDecode(ourEncode(smallTextBytes)) should readAs(smallText) } diff --git a/akka-http/src/main/scala/akka/http/scaladsl/coding/Deflate.scala b/akka-http/src/main/scala/akka/http/scaladsl/coding/Deflate.scala index 4762bfa152..887f8e27b2 100644 --- a/akka-http/src/main/scala/akka/http/scaladsl/coding/Deflate.scala +++ b/akka-http/src/main/scala/akka/http/scaladsl/coding/Deflate.scala @@ -55,7 +55,7 @@ class DeflateCompressor extends Compressor { res } - private def newTempBuffer(size: Int = 65536): Array[Byte] = + private def newTempBuffer(size: Int = 65536): Array[Byte] = { // The default size is somewhat arbitrary, we'd like to guess a better value but Deflater/zlib // is buffering in an unpredictable manner. // `compress` will only return any data if the buffered compressed data has some size in @@ -63,10 +63,15 @@ class DeflateCompressor extends Compressor { // `flush` and `finish` will return any size depending on the previous input. // This value will hopefully provide a good compromise between memory churn and // excessive fragmentation of ByteStrings. - new Array[Byte](size) + // We also make sure that buffer size stays within a reasonable range, to avoid + // draining deflator with too small buffer. + new Array[Byte](math.max(size, MinBufferSize)) + } } private[http] object DeflateCompressor { + val MinBufferSize = 1024 + // TODO: remove reflective call once Java 6 support is dropped /** * Compatibility mode: reflectively call deflate(..., flushMode) if available or use a hack otherwise