=htp #19099 fix encoding of empty stings

This commit is contained in:
Anton Karamanov 2015-12-06 16:39:21 +03:00 committed by Konrad Malawski
parent 15cc65ce9d
commit 3dbb553b0e
3 changed files with 21 additions and 2 deletions

View file

@ -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

View file

@ -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)
}

View file

@ -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