-
Notifications
You must be signed in to change notification settings - Fork 324
resetDecoder should be called once per decoding #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
msgpack-core/src/main/java/org/msgpack/core/buffer/SequenceMessageBufferInput.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
@@ -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 { | ||
|
|
@@ -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 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.