10BC0 resetDecoder should be called once per decoding by miniway · Pull Request #387 · msgpack/msgpack-java · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -940,12 +940,13 @@ public String unpackString()
if (len > stringSizeLimit) {
throw new MessageSizeException(String.format("cannot unpack a String of size larger than %,d: %,d", stringSizeLimit, len), len);
}

resetDecoder(); // should be invoked only once per value

if (buffer.size() - position >= len) {
return decodeStringFastPath(len);
}

resetDecoder();

try {
int rawRemaining = len;
while (rawRemaining > 0) {
Expand Down Expand Up @@ -1039,10 +1040,7 @@ private String decodeStringFastPath(int length)
return s;
}
else {
resetDecoder();
ByteBuffer bb = buffer.sliceAsByteBuffer();
bb.limit(position + length);
bb.position(position);
ByteBuffer bb = buffer.sliceAsByteBuffer(position, length);
CharBuffer cb;
try {
cb = decoder.decode(bb);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// MessagePack for Java
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.core.buffer;

import java.io.IOException;
import java.util.Enumeration;

import static org.msgpack.core.Preconditions.checkNotNull;

/**
* {@link MessageBufferInput} adapter for {@link MessageBufferInput} Enumeration
*/
public class SequenceMessageBufferInput
implements MessageBufferInput
{
private Enumeration<? extends MessageBufferInput> sequence;
private MessageBufferInput input;

public SequenceMessageBufferInput(Enumeration<? extends MessageBufferInput> sequence)
Copy link
Member
@frsyuki frsyuki Sep 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use Iterator, Stream, Iterable, or Collection instead of Enumeration? or how about moving this to test package only for now? Because Enumeration is considered as a deprecated interface used only by old classes such as Vector or Hashtable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Iterator<MessageBufferInput> would work. And this should be in test package.

{
this.sequence = checkNotNull(sequence, "input sequence is null");
try {
nextInput();
}
catch (IOException ignore) {
}
}

@Override
public MessageBuffer next() throws IOException
{
if (input == null) {
return null;
}
MessageBuffer buffer = input.next();
if (buffer == null) {
nextInput();
return next();
}

return buffer;
}

private void nextInput() throws IOException
{
if (input != null) {
input.close();
}

if (sequence.hasMoreElements()) {
input = sequence.nextElement();
if (input == null) {
throw new NullPointerException();
}
}
else {
input = null;
}
}

@Override
public void close() throws IOException
{
do {
nextInput();
} while (input != null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ package org.msgpack.core

import java.io._
import java.nio.ByteBuffer
import java.util.Collections

import org.msgpack.core.buffer._
import org.msgpack.value.ValueType
import xerial.core.io.IOUtil._

import scala.collection.JavaConversions._
import scala.util.Random

object MessageUnpackerTest {
Expand Down Expand Up @@ -205,6 +207,34 @@ class MessageUnpackerTest extends MessagePackSpec {
builder.result()
}

def sequenceUnpackers(data: Array[Byte], size: Int) : Seq[MessageUnpacker] = {
val seqBytes = Seq.newBuilder[MessageBufferInput]
val seqByteBuffers = Seq.newBuilder[MessageBufferInput]
val seqDirectBuffers = Seq.newBuilder[MessageBufferInput]
var left = data.length
var position = 0
while (left > 0) {
val length = Math.min(size, left)
seqBytes += new ArrayBufferInput(data, position, length);
val bb = ByteBuffer.allocate(length)
val db = ByteBuffer.allocateDirect(length)
bb.put(data, position, length).flip()
db.put(data, position, length).flip()
seqByteBuffers += new ByteBufferInput(bb);
seqDirectBuffers += new ByteBufferInput(db);
left -= length
position += length
}
val builder = Seq.newBuilder[MessageUnpacker]
builder += MessagePack.newDefaultUnpacker(new SequenceMessageBufferInput(Collections.enumeration(seqBytes.result())))
builder += MessagePack.newDefaultUnpacker(new SequenceMessageBufferInput(Collections.enumeration(seqByteBuffers.result())))
if (!universal) {
builder += MessagePack.newDefaultUnpacker(new SequenceMessageBufferInput(Collections.enumeration(seqDirectBuffers.result())))
}

builder.result()
}

"MessageUnpacker" should {

"parse message packed data" taggedAs ("unpack") in {
Expand Down Expand Up @@ -330,21 +360,28 @@ class MessageUnpackerTest extends MessagePackSpec {
new SplitTest {val data = testData3(30)}.run
}

"read numeric data at buffer boundary" taggedAs("boundary2") in {
"read data at buffer boundary" taggedAs("boundary2") in {
Copy link
Member
@xerial xerial Sep 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is intended for checking a bug on unpackInteger that found in the past. We should have another test case for string.

val packer = MessagePack.newDefaultBufferPacker()
(0 until 1170).foreach{i =>
packer.packLong(0x0011223344556677L)
packer.packString("hello")
packer.packString("hello world")
}
packer.close
val data = packer.toByteArray

val unpacker = MessagePack.newDefaultUnpacker(new InputStreamBufferInput(new ByteArrayInputStream(data), 8192))
var unpacker = MessagePack.newDefaultUnpacker(new InputStreamBufferInput(new ByteArrayInputStream(data), 8192))
(0 until 1170).foreach { i =>
unpacker.unpackLong() shouldBe 0x0011223344556677L
unpacker.unpackString() shouldBe "hello"
unpacker.unpackString() shouldBe "hello world"
}
unpacker.close()

for (unpacker <- sequenceUnpackers(data, 32)) {
(0 until 1170).foreach { i =>
unpacker.unpackLong() shouldBe 0x0011223344556677L
unpacker.unpackString() shouldBe "hello world"
}
}
}

"be faster then msgpack-v6 skip" taggedAs ("cmp-skip") in {
Expand Down
0