@@ -17,11 +17,13 @@ package org.msgpack.core
1717
1818import java .io ._
1919import java .nio .ByteBuffer
20+ import java .util .Collections
2021
2122import org .msgpack .core .buffer ._
2223import org .msgpack .value .ValueType
2324import xerial .core .io .IOUtil ._
2425
26+ import scala .collection .JavaConversions ._
2527import scala .util .Random
2628
2729object MessageUnpackerTest {
@@ -205,6 +207,34 @@ class MessageUnpackerTest extends MessagePackSpec {
205207 builder.result()
206208 }
207209
210+ def unpackerCollectionWithVariousBuffers (data : Array [Byte ], chunkSize : Int ) : Seq [MessageUnpacker ] = {
211+ val seqBytes = Seq .newBuilder[MessageBufferInput ]
212+ val seqByteBuffers = Seq .newBuilder[MessageBufferInput ]
213+ val seqDirectBuffers = Seq .newBuilder[MessageBufferInput ]
214+ var left = data.length
215+ var position = 0
216+ while (left > 0 ) {
217+ val length = Math .min(chunkSize, left)
218+ seqBytes += new ArrayBufferInput (data, position, length)
219+ val bb = ByteBuffer .allocate(length)
220+ val db = ByteBuffer .allocateDirect(length)
221+ bb.put(data, position, length).flip()
222+ db.put(data, position, length).flip()
223+ seqByteBuffers += new ByteBufferInput (bb)
224+ seqDirectBuffers += new ByteBufferInput (db)
225+ left -= length
226+ position += length
227+ }
228+ val builder = Seq .newBuilder[MessageUnpacker ]
229+ builder += MessagePack .newDefaultUnpacker(new SequenceMessageBufferInput (Collections .enumeration(seqBytes.result())))
230+ builder += MessagePack .newDefaultUnpacker(new SequenceMessageBufferInput (Collections .enumeration(seqByteBuffers.result())))
231+ if (! universal) {
232+ builder += MessagePack .newDefaultUnpacker(new SequenceMessageBufferInput (Collections .enumeration(seqDirectBuffers.result())))
233+ }
234+
235+ builder.result()
236+ }
237+
208238 " MessageUnpacker" should {
209239
210240 " parse message packed data" taggedAs (" unpack" ) in {
@@ -330,21 +360,50 @@ class MessageUnpackerTest extends MessagePackSpec {
330360 new SplitTest {val data = testData3(30 )}.run
331361 }
332362
333- " read numeric data at buffer boundary " taggedAs(" boundary2 " ) in {
363+ " read integer at MessageBuffer boundaries " taggedAs(" integer-buffer-boundary " ) in {
334364 val packer = MessagePack .newDefaultBufferPacker()
335365 (0 until 1170 ).foreach{i =>
336366 packer.packLong(0x0011223344556677L)
337- packer.packString(" hello" )
338367 }
339368 packer.close
340369 val data = packer.toByteArray
341370
342- val unpacker = MessagePack .newDefaultUnpacker(new InputStreamBufferInput (new ByteArrayInputStream (data), 8192 ))
343- (0 until 1170 ).foreach { i =>
344- unpacker.unpackLong() shouldBe 0x0011223344556677L
345- unpacker.unpackString() shouldBe " hello"
371+ // Boundary test
372+ withResource(MessagePack .newDefaultUnpacker(new InputStreamBufferInput (new ByteArrayInputStream (data), 8192 ))) { unpacker =>
373+ (0 until 1170 ).foreach { i =>
374+ unpacker.unpackLong() shouldBe 0x0011223344556677L
375+ }
376+ }
377+
378+ // Boundary test for sequences of ByteBuffer, DirectByteBuffer backed MessageInput.
379+ for (unpacker <- unpackerCollectionWithVariousBuffers(data, 32 )) {
380+ (0 until 1170 ).foreach { i =>
381+ unpacker.unpackLong() shouldBe 0x0011223344556677L
382+ }
383+ }
384+ }
385+
386+ " read string at MessageBuffer boundaries" taggedAs(" string-buffer-boundary" ) in {
387+ val packer = MessagePack .newDefaultBufferPacker()
388+ (0 until 1170 ).foreach{i =>
389+ packer.packString(" hello world" )
390+ }
391+ packer.close
392+ val data = packer.toByteArray
393+
394+ // Boundary test
395+ withResource(MessagePack .newDefaultUnpacker(new InputStreamBufferInput (new ByteArrayInputStream (data), 8192 ))) { unpacker =>
396+ (0 until 1170 ).foreach { i =>
397+ unpacker.unpackString() shouldBe " hello world"
398+ }
399+ }
400+
401+ // Boundary test for sequences of ByteBuffer, DirectByteBuffer backed MessageInput.
402+ for (unpacker <- unpackerCollectionWithVariousBuffers(data, 32 )) {
403+ (0 until 1170 ).foreach { i =>
404+ unpacker.unpackString() shouldBe " hello world"
405+ }
346406 }
347- unpacker.close()
348407 }
349408
350409 " be faster then msgpack-v6 skip" taggedAs (" cmp-skip" ) in {
0 commit comments