Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,10 @@ public MessagePacker writePayload(ByteBuffer src) throws IOException {
else {
// If the input source is small, simply copy the contents to the buffer
while(src.remaining() > 0) {
if(position >= buffer.size())
if(position >= buffer.size()) {
flush();
}
prepareBuffer();
int writeLen = Math.min(buffer.size() - position, src.remaining());
buffer.putByteBuffer(position, src, writeLen);
position += writeLen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.msgpack.core.buffer.{OutputStreamBufferOutput, ArrayBufferInput}
import xerial.core.io.IOUtil

import scala.util.Random
import org.msgpack.value.ValueFactory

/**
*
Expand Down Expand Up @@ -102,5 +103,32 @@ class MessagePackerTest extends MessagePackSpec {

}

"pack larger string array than byte buf" taggedAs ("larger-string-array-than-byte-buf") in {
// Based on https://github.com/msgpack/msgpack-java/issues/154

// TODO: Refactor this test code to fit other ones.
def test(bufferSize: Int, stringSize: Int): Boolean = {
val msgpack = new MessagePack(new MessagePack.ConfigBuilder().packerBufferSize(bufferSize).build)
val str = "a" * stringSize
val rawString = ValueFactory.newRawString(str.getBytes("UTF-8"))
val array = ValueFactory.newArray(rawString)
val out = new ByteArrayOutputStream()
val packer = msgpack.newPacker(out)
packer.packValue(array)
packer.close()
out.toByteArray
true
}

val testCases = List(
32 -> 30,
33 -> 31,
32 -> 31,
34 -> 32
)
testCases.foreach{
case (bufferSize, stringSize) => test(bufferSize, stringSize)
}
}
}
}