Skip to content

Commit 09f5d5e

Browse files
committed
Take care of offset of source ByteBuffer in MessageBuffer#putByteBuffer
1 parent a8f596d commit 09f5d5e

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferU.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void putByteBuffer(int index, ByteBuffer src, int len)
198198
assert (len <= src.remaining());
199199

200200
if (src.hasArray()) {
201-
putBytes(index, src.array(), src.position(), len);
201+
putBytes(index, src.array(), src.position() + src.arrayOffset(), len);
202202
src.position(src.position() + len);
203203
}
204204
else {

msgpack-core/src/test/scala/org/msgpack/core/buffer/MessageBufferTest.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//
1616
package org.msgpack.core.buffer
1717

18+
import java.io.ByteArrayInputStream
1819
import java.nio.ByteBuffer
1920

2021
import org.msgpack.core.MessagePackSpec
@@ -184,6 +185,47 @@ class MessageBufferTest
184185
}
185186
}
186187
}
188+
189+
"copy sliced buffer" in {
190+
def prepareBytes : Array[Byte] = {
191+
Array[Byte](0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07)
192+
}
193+
194+
def prepareDirectBuffer : ByteBuffer = {
195+
val directBuffer = ByteBuffer.allocateDirect(prepareBytes.length)
196+
directBuffer.put(prepareBytes)
197+
directBuffer
198+
}
199+
200+
def checkSliceAndCopyTo(srcBuffer: MessageBuffer, dstBuffer: MessageBuffer) = {
201+
val sliced = srcBuffer.slice(2, 5)
202+
203+
sliced.size() shouldBe 5
204+
sliced.getByte(0) shouldBe 0x02
205+
sliced.getByte(1) shouldBe 0x03
206+
sliced.getByte(2) shouldBe 0x04
207+
sliced.getByte(3) shouldBe 0x05
208+
sliced.getByte(4) shouldBe 0x06
209+
210+
sliced.copyTo(3, dstBuffer, 1, 2) // copy 0x05 and 0x06 to dstBuffer[1] and [2]
211+
212+
dstBuffer.getByte(0) shouldBe 0x00
213+
dstBuffer.getByte(1) shouldBe 0x05 // copied by sliced.getByte(3)
214+
dstBuffer.getByte(2) shouldBe 0x06 // copied by sliced.getByte(4)
215+
dstBuffer.getByte(3) shouldBe 0x03
216+
dstBuffer.getByte(4) shouldBe 0x04
217+
dstBuffer.getByte(5) shouldBe 0x05
218+
dstBuffer.getByte(6) shouldBe 0x06
219+
dstBuffer.getByte(7) shouldBe 0x07
220+
}
221+
222+
checkSliceAndCopyTo(new MessageBufferU(prepareBytes), new MessageBufferU(prepareBytes))
223+
checkSliceAndCopyTo(new MessageBufferU(ByteBuffer.wrap(prepareBytes)), new MessageBufferU(ByteBuffer.wrap(prepareBytes)))
224+
checkSliceAndCopyTo(new MessageBufferU(prepareDirectBuffer), new MessageBufferU(prepareDirectBuffer))
225+
checkSliceAndCopyTo(new MessageBufferBE(prepareBytes), new MessageBufferBE(prepareBytes))
226+
checkSliceAndCopyTo(new MessageBufferBE(ByteBuffer.wrap(prepareBytes)), new MessageBufferBE(ByteBuffer.wrap(prepareBytes)))
227+
checkSliceAndCopyTo(new MessageBufferBE(prepareDirectBuffer), new MessageBufferBE(prepareDirectBuffer))
228+
}
187229
}
188230
}
189231

0 commit comments

Comments
 (0)