|
15 | 15 | // |
16 | 16 | package org.msgpack.core.buffer |
17 | 17 |
|
| 18 | +import java.io.ByteArrayInputStream |
18 | 19 | import java.nio.ByteBuffer |
19 | 20 |
|
20 | 21 | import org.msgpack.core.MessagePackSpec |
@@ -184,6 +185,47 @@ class MessageBufferTest |
184 | 185 | } |
185 | 186 | } |
186 | 187 | } |
| 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 | + } |
187 | 229 | } |
188 | 230 | } |
189 | 231 |
|
|
0 commit comments