Skip to content

Commit bb9ed18

Browse files
committed
fixed Unpacker#skip
1 parent 8ca01e4 commit bb9ed18

3 files changed

Lines changed: 92 additions & 6 deletions

File tree

src/main/java/org/msgpack/unpacker/MessagePackUnpacker.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,11 @@ public void skip() throws IOException {
571571
while (true) {
572572
while (stack.getTopCount() == 0) {
573573
stack.pop();
574-
if (stack.getTopCount() == 0) {
575-
stack.pop();
576-
if (stack.getDepth() <= targetDepth) {
577-
return;
578-
}
574+
if (stack.getDepth() <= targetDepth) {
575+
return;
579576
}
580577
}
581-
readOne(valueAccept);
578+
readOne(skipAccept);
582579
}
583580
}
584581

src/main/java/org/msgpack/unpacker/SkipAccept.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
//
1818
package org.msgpack.unpacker;
1919

20+
import java.io.IOException;
21+
import java.nio.ByteBuffer;
22+
2023
final class SkipAccept extends Accept {
2124
@Override
2225
void acceptBoolean(boolean v) {
@@ -62,6 +65,10 @@ void acceptRaw(byte[] raw) {
6265
void acceptEmptyRaw() {
6366
}
6467

68+
@Override
69+
public void refer(ByteBuffer bb, boolean gift) throws IOException {
70+
}
71+
6572
@Override
6673
void acceptArray(int size) {
6774
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.msgpack.unpacker;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.ByteArrayOutputStream;
7+
8+
import org.junit.Test;
9+
import org.msgpack.MessagePack;
10+
import org.msgpack.packer.BufferPacker;
11+
import org.msgpack.unpacker.BufferUnpacker;
12+
import org.msgpack.type.Value;
13+
import org.msgpack.type.ValueFactory;
14+
15+
public class TestUnpackerSkip {
16+
@Test
17+
public void testPrimitive() throws Exception {
18+
MessagePack msgpack = new MessagePack();
19+
20+
BufferPacker packer = msgpack.createBufferPacker();
21+
22+
for(int i=0; i < 10; i++) {
23+
packer.write(1);
24+
packer.write(i);
25+
}
26+
27+
byte[] bytes = packer.toByteArray();
28+
BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes);
29+
30+
for(int i=0; i < 10; i++) {
31+
unpacker.skip();
32+
int n = unpacker.readInt();
33+
assertEquals(i, n);
34+
}
35+
}
36+
37+
@Test
38+
public void testNested() throws Exception {
39+
MessagePack msgpack = new MessagePack();
40+
41+
BufferPacker packer = msgpack.createBufferPacker();
42+
43+
Value v1 = ValueFactory.createArrayValue(new Value[] {
44+
ValueFactory.createRawValue("a"),
45+
ValueFactory.createMapValue(new Value[] {
46+
ValueFactory.createRawValue("k1"),
47+
ValueFactory.createArrayValue(new Value[] { ValueFactory.createIntegerValue(1) })
48+
})
49+
});
50+
51+
Value v2 = ValueFactory.createArrayValue(new Value[] {
52+
ValueFactory.createMapValue(new Value[] {
53+
ValueFactory.createRawValue("k1"),
54+
ValueFactory.createArrayValue(new Value[] { ValueFactory.createIntegerValue(1) }),
55+
ValueFactory.createRawValue("k2"),
56+
ValueFactory.createArrayValue(new Value[] { ValueFactory.createIntegerValue(2) })
57+
}),
58+
ValueFactory.createMapValue(new Value[] {
59+
ValueFactory.createRawValue("k1"),
60+
ValueFactory.createArrayValue(new Value[] { ValueFactory.createIntegerValue(1) }),
61+
ValueFactory.createRawValue("k2"),
62+
ValueFactory.createArrayValue(new Value[] { ValueFactory.createIntegerValue(2) })
63+
}),
64+
ValueFactory.createRawValue("a")
65+
});
66+
67+
for(int i=0; i < 10; i++) {
68+
packer.write(v1);
69+
packer.write(v2);
70+
}
71+
72+
byte[] bytes = packer.toByteArray();
73+
BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes);
74+
75+
for(int i=0; i < 10; i++) {
76+
unpacker.skip();
77+
Value v2a = unpacker.readValue();
78+
assertEquals(v2, v2a);
79+
}
80+
}
81+
}
82+

0 commit comments

Comments
 (0)