Skip to content

Commit 9eff0ca

Browse files
frsyukikomamitsu
authored andcommitted
optimized slightly MessageUnpacker.readCastBuffer
1 parent 6390efc commit 9eff0ca

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,18 +253,26 @@ private MessageBuffer readCastBuffer(int length)
253253
throw new MessageInsufficientBufferException();
254254
}
255255

256-
// TODO this doesn't work if MessageBuffer is allocated by newDirectBuffer.
257-
// add copy method to MessageBuffer to solve this issue.
258-
castBuffer.putBytes(0, buffer.array(), buffer.arrayOffset() + position, remaining);
259-
castBuffer.putBytes(remaining, next.array(), next.arrayOffset(), length - remaining);
260-
261256
totalReadBytes += buffer.size();
262257

263-
buffer = next;
264-
position = length - remaining;
265-
readCastBufferPosition = 0;
258+
if (remaining > 0) {
259+
// TODO this doesn't work if MessageBuffer is allocated by newDirectBuffer.
260+
// add copy method to MessageBuffer to solve this issue.
261+
castBuffer.putBytes(0, buffer.array(), buffer.arrayOffset() + position, remaining);
262+
castBuffer.putBytes(remaining, next.array(), next.arrayOffset(), length - remaining);
263+
264+
buffer = next;
265+
position = length - remaining;
266+
readCastBufferPosition = 0;
266267

267-
return castBuffer;
268+
return castBuffer;
269+
}
270+
else {
271+
buffer = next;
272+
position = length;
273+
readCastBufferPosition = 0;
274+
return buffer;
275+
}
268276
}
269277
}
270278

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public MessageBuffer next()
7676
if (readLen == -1) {
7777
return null;
7878
}
79-
return MessageBuffer.wrap(buffer).slice(0, readLen);
79+
return MessageBuffer.wrap(buffer, 0, readLen);
8080
}
8181

8282
@Override

0 commit comments

Comments
 (0)