Skip to content

Commit 0189247

Browse files
committed
Merge pull request msgpack#348 from naritta/MikePlayle-develop-add-test
Merge "Copy old data before fetching new data, to avoid overwriting it" and add test for that
2 parents ff9ffe8 + 59b4e48 commit 0189247

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -208,32 +208,36 @@ private MessageBuffer prepareNumberBuffer(int readLength)
208208
position += readLength; // here assumes following buffer.getXxx never throws exception
209209
return buffer; // Return the default buffer
210210
}
211+
else if(remaining == 0) {
212+
buffer = getNextBuffer();
213+
position = readLength;
214+
nextReadPosition = 0;
215+
return buffer;
216+
}
211217
else {
212218
// When the default buffer doesn't contain the whole length
213219

220+
// TODO This doesn't work if MessageBuffer is allocated by newDirectBuffer.
221+
// Add copy method to MessageBuffer to solve this issue.
222+
223+
// Copy the data fragment from the current buffer
224+
225+
numberBuffer.putBytes(0,
226+
buffer.array(), buffer.arrayOffset() + position,
227+
remaining);
228+
214229
// TODO loop this method until castBuffer is filled
215230
MessageBuffer next = getNextBuffer();
216231

217-
if (remaining > 0) {
218-
// TODO This doesn't work if MessageBuffer is allocated by newDirectBuffer.
219-
// Add copy method to MessageBuffer to solve this issue.
220-
221-
// Copy the data fragment from the current buffer
222-
numberBuffer.putBytes(0, buffer.array(), buffer.arrayOffset() + position, remaining);
223-
numberBuffer.putBytes(remaining, next.array(), next.arrayOffset(), readLength - remaining);
232+
numberBuffer.putBytes(remaining,
233+
next.array(), next.arrayOffset(),
234+
readLength - remaining);
224235

225-
buffer = next;
226-
position = readLength - remaining;
227-
nextReadPosition = 0;
236+
buffer = next;
237+
position = readLength - remaining;
238+
nextReadPosition = 0;
228239

229-
return numberBuffer; // Return the numberBuffer
230-
}
231-
else {
232-
buffer = next;
233-
position = readLength;
234-
nextReadPosition = 0;
235-
return buffer;
236-
}
240+
return numberBuffer; // Return the numberBuffer
237241
}
238242
}
239243

msgpack-core/src/test/scala/org/msgpack/core/MessageUnpackerTest.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class MessageUnpackerTest extends MessagePackSpec {
147147
val mapLen = unpacker.unpackMapHeader()
148148
debug(s"map size: $mapLen")
149149
case ValueType.INTEGER =>
150-
val i = unpacker.unpackInt()
150+
val i = unpacker.unpackLong()
151151
debug(s"int value: $i")
152152
case ValueType.STRING =>
153153
val s = unpacker.unpackString()
@@ -296,7 +296,23 @@ class MessageUnpackerTest extends MessagePackSpec {
296296

297297
new SplitTest {val data = testData}.run
298298
new SplitTest {val data = testData3(30)}.run
299+
}
300+
301+
"read numeric data at buffer boundary" taggedAs("boundary2") in {
302+
val packer = MessagePack.newDefaultBufferPacker()
303+
(0 until 1170).foreach{i =>
304+
packer.packLong(0x0011223344556677L)
305+
packer.packString("hello")
306+
}
307+
packer.close
308+
val data = packer.toByteArray
299309

310+
val unpacker = MessagePack.newDefaultUnpacker(new InputStreamBufferInput(new ByteArrayInputStream(data), 8192))
311+
(0 until 1170).foreach { i =>
312+
unpacker.unpackLong() shouldBe 0x0011223344556677L
313+
unpacker.unpackString() shouldBe "hello"
314+
}
315+
unpacker.close()
300316
}
301317

302318
"be faster then msgpack-v6 skip" taggedAs ("cmp-skip") in {

0 commit comments

Comments
 (0)