Skip to content

Commit 8ca01e4

Browse files
committed
added Unpacker.getNextType()
1 parent 7087a2d commit 8ca01e4

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/main/java/org/msgpack/type/Value.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121
import org.msgpack.packer.Packer;
22+
import org.msgpack.type.ValueType;
2223

2324
public interface Value {
2425
public ValueType getType();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.msgpack.MessageTypeException;
2626
import org.msgpack.packer.Unconverter;
2727
import org.msgpack.type.Value;
28+
import org.msgpack.type.ValueType;
2829
import org.msgpack.type.ArrayValue;
2930
import org.msgpack.type.MapValue;
3031

@@ -402,6 +403,10 @@ public void skip() throws IOException {
402403
}
403404
}
404405

406+
public ValueType getNextType() throws IOException {
407+
return getTop().getType();
408+
}
409+
405410
public void reset() {
406411
stack.clear();
407412
value = null;

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.msgpack.MessagePack;
2828
import org.msgpack.MessageTypeException;
2929
import org.msgpack.packer.Unconverter;
30+
import org.msgpack.type.ValueType;
3031

3132
public class MessagePackUnpacker extends AbstractUnpacker {
3233
private static final byte REQUIRE_TO_READ_HEAD = (byte) 0xc6;
@@ -581,6 +582,55 @@ public void skip() throws IOException {
581582
}
582583
}
583584

585+
public ValueType getNextType() throws IOException {
586+
final int b = (int) getHeadByte();
587+
if ((b & 0x80) == 0) { // Positive Fixnum
588+
return ValueType.INTEGER;
589+
}
590+
if ((b & 0xe0) == 0xe0) { // Negative Fixnum
591+
return ValueType.INTEGER;
592+
}
593+
if ((b & 0xe0) == 0xa0) { // FixRaw
594+
return ValueType.RAW;
595+
}
596+
if ((b & 0xf0) == 0x90) { // FixArray
597+
return ValueType.ARRAY;
598+
}
599+
if ((b & 0xf0) == 0x80) { // FixMap
600+
return ValueType.MAP;
601+
}
602+
switch (b & 0xff) {
603+
case 0xc0: // nil
604+
return ValueType.NIL;
605+
case 0xc2: // false
606+
case 0xc3: // true
607+
return ValueType.BOOLEAN;
608+
case 0xca: // float
609+
case 0xcb: // double
610+
return ValueType.FLOAT;
611+
case 0xcc: // unsigned int 8
612+
case 0xcd: // unsigned int 16
613+
case 0xce: // unsigned int 32
614+
case 0xcf: // unsigned int 64
615+
case 0xd0: // signed int 8
616+
case 0xd1: // signed int 16
617+
case 0xd2: // signed int 32
618+
case 0xd3: // signed int 64
619+
return ValueType.INTEGER;
620+
case 0xda: // raw 16
621+
case 0xdb: // raw 32
622+
return ValueType.RAW;
623+
case 0xdc: // array 16
624+
case 0xdd: // array 32
625+
return ValueType.ARRAY;
626+
case 0xde: // map 16
627+
case 0xdf: // map 32
628+
return ValueType.MAP;
629+
default:
630+
throw new IOException("Invalid byte: " + b); // TODO error FormatException
631+
}
632+
}
633+
584634
public void reset() {
585635
raw = null;
586636
stack.clear();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.msgpack.template.Template;
2727
import org.msgpack.type.Value;
28+
import org.msgpack.type.ValueType;
2829

2930
/**
3031
* Standard deserializer.
@@ -82,6 +83,8 @@ public interface Unpacker extends Iterable<Value>, Closeable {
8283

8384
public Value readValue() throws IOException;
8485

86+
public ValueType getNextType() throws IOException;
87+
8588
public UnpackerIterator iterator();
8689

8790
public int getReadByteCount();

0 commit comments

Comments
 (0)