Skip to content

Commit b705c6b

Browse files
committed
Merge pull request #86 from msgpack/v07-remove-EOF-type
Removing EOF and UNKNOWN types
2 parents 1923797 + 7359116 commit b705c6b

File tree

6 files changed

+63
-51
lines changed

6 files changed

+63
-51
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
*/
1010
public enum MessageFormat {
1111

12-
// End of file
13-
EOF(ValueType.EOF) {
14-
@Override
15-
int skip(MessageUnpacker unpacker) throws IOException{
16-
// do nothing
17-
return 0;
18-
}
19-
},
2012
// INT7
2113
POSFIXINT(ValueType.INTEGER) {
2214
@Override
@@ -57,7 +49,7 @@ int skip(MessageUnpacker unpacker) throws IOException{
5749
return 0;
5850
}
5951
},
60-
UNKNOWN(ValueType.UNKNOWN) {
52+
NEVER_USED(null) {
6153
@Override
6254
int skip(MessageUnpacker unpacker) throws IOException{
6355
throw new MessageFormatException(String.format("unknown code: %02x is found", unpacker.lookAhead()));
@@ -293,11 +285,18 @@ int skip(MessageUnpacker unpacker) throws IOException{
293285

294286
private final ValueType valueType;
295287

296-
private MessageFormat(ValueType family) {
297-
this.valueType = family;
288+
private MessageFormat(ValueType valueType) {
289+
this.valueType = valueType;
298290
}
299291

300-
public ValueType getValueType() {
292+
/**
293+
*
294+
* @return
295+
* @throws MessageFormatException if this == NEVER_USED type
296+
*/
297+
public ValueType getValueType() throws MessageFormatException {
298+
if(this == NEVER_USED)
299+
throw new MessageFormatException("Cannot convert NEVER_USED to ValueType");
301300
return valueType;
302301
}
303302

@@ -402,7 +401,7 @@ static MessageFormat toMessageFormat(final byte b) {
402401
case Code.MAP32:
403402
return MAP32;
404403
default:
405-
return UNKNOWN;
404+
return NEVER_USED;
406405
}
407406
}
408407

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ private boolean ensure(int readSize) throws IOException {
130130
ArrayList<MessageBuffer> bufferList = new ArrayList<MessageBuffer>();
131131
while(bufferTotal < readSize) {
132132
MessageBuffer next = in.next();
133-
if(next == null)
133+
if(next == null) {
134+
reachedEOF = true;
134135
throw new EOFException();
136+
}
135137
bufferTotal += next.limit();
136138
bufferList.add(next);
137139
}
@@ -165,26 +167,34 @@ private CharsetDecoder getCharsetDecoder() {
165167

166168

167169
private static ValueType getTypeFromHead(final byte b) throws MessageFormatException {
170+
MessageFormat mf = MessageFormat.valueOf(b);
168171
ValueType vt = MessageFormat.valueOf(b).getValueType();
169-
if(vt == ValueType.UNKNOWN)
170-
throw new MessageFormatException(String.format("Invalid format code: %02x", b));
171172
return vt;
172173
}
173174

174-
public ValueType getNextType() throws IOException {
175+
/**
176+
* Returns true true if this unpacker has more elements. If true, {@link #getNextFormat()} returns an
177+
* MessageFormat instead of throwing an exception.
178+
*
179+
* @return true if this unpacker has more elements to read
180+
*/
181+
public boolean hasNext() throws IOException {
182+
if(ensure(1)) {
183+
return true;
184+
}
185+
else {
186+
return false;
187+
}
188+
}
189+
190+
public ValueType getNextValueType() throws IOException {
175191
byte b = lookAhead();
176-
if(reachedEOF)
177-
return ValueType.EOF;
178-
else
179-
return ValueType.valueOf(b);
192+
return getTypeFromHead(b);
180193
}
181194

182195
public MessageFormat getNextFormat() throws IOException {
183196
byte b = lookAhead();
184-
if(b == READ_NEXT || reachedEOF)
185-
return MessageFormat.EOF;
186-
else
187-
return MessageFormat.valueOf(b);
197+
return MessageFormat.valueOf(b);
188198
}
189199

190200
/**
@@ -197,6 +207,8 @@ byte lookAhead() throws IOException {
197207
if(head == READ_NEXT) {
198208
if(ensure(1))
199209
head = buffer.getByte(position);
210+
else
211+
throw new EOFException();
200212
}
201213
return head;
202214
}
@@ -295,8 +307,6 @@ public boolean skipValue() throws IOException {
295307
int remainingValues = 1;
296308
while(remainingValues > 0) {
297309
MessageFormat f = getNextFormat();
298-
if(f == MessageFormat.EOF)
299-
return false;
300310
remainingValues += f.skip(this);
301311
remainingValues--;
302312
}
@@ -311,8 +321,6 @@ protected void skipValueWithSwitch() throws IOException {
311321
byte b = lookAhead();
312322
consume();
313323
switch(f) {
314-
case EOF:
315-
return;
316324
case POSFIXINT:
317325
case NEGFIXINT:
318326
case BOOLEAN:
@@ -403,7 +411,7 @@ protected void skipValueWithSwitch() throws IOException {
403411
remainingValues += readNextLength32() * 2; // TODO check int overflow
404412
consume(2);
405413
break;
406-
case UNKNOWN:
414+
case NEVER_USED:
407415
throw new MessageFormatException(String.format("unknown code: %02x is found", b));
408416
}
409417

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
*/
2323
public enum ValueType {
2424

25-
EOF(false, false),
26-
UNKNOWN(false, false),
2725
NIL(false, false),
2826
BOOLEAN(false, false),
2927
INTEGER(true, false),
@@ -86,9 +84,8 @@ public boolean isExtendedType() {
8684
return this == EXTENDED;
8785
}
8886

89-
public static ValueType valueOf(final byte b) {
87+
public static ValueType valueOf(byte b) {
9088
return MessageFormat.valueOf(b).getValueType();
9189
}
9290

93-
9491
}

msgpack-core/src/test/scala/org/msgpack/core/MessageFormatTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class MessageFormatTest extends MessagePackSpec {
4545

4646
check(Code.NIL, ValueType.NIL, MessageFormat.NIL)
4747

48-
check(Code.NEVER_USED, ValueType.UNKNOWN, MessageFormat.UNKNOWN)
48+
MessageFormat.valueOf(Code.NEVER_USED) shouldBe MessageFormat.NEVER_USED
4949

5050
for(i <- Seq(Code.TRUE, Code.FALSE))
5151
check(i, ValueType.BOOLEAN, MessageFormat.BOOLEAN)

msgpack-core/src/test/scala/org/msgpack/core/MessageUnpackerTest.scala

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ class MessageUnpackerTest extends MessagePackSpec {
5252

5353
"MessageUnpacker" should {
5454

55-
"parse message packed data" in {
55+
"parse message packed data" taggedAs("unpack") in {
5656
val arr = testData
5757

5858
val unpacker = MessagePack.newUnpacker(arr)
5959

60-
var f : MessageFormat = null
61-
do {
62-
f = unpacker.getNextFormat()
60+
while(unpacker.hasNext) {
61+
val f = unpacker.getNextFormat()
6362
f.getValueType match {
6463
case ValueType.ARRAY =>
6564
val arrLen = unpacker.unpackArrayHeader()
@@ -73,21 +72,19 @@ class MessageUnpackerTest extends MessagePackSpec {
7372
case ValueType.STRING =>
7473
val s = unpacker.unpackString()
7574
debug(s"str value: $s")
76-
case ValueType.EOF =>
77-
debug(s"reached EOF")
7875
case other =>
79-
unpacker.skipValue();
76+
unpacker.skipValue()
8077
debug(s"unknown type: $f")
8178
}
8279
}
83-
while (f != MessageFormat.EOF)
8480
}
8581

8682
"skip reading values" in {
8783

8884
val unpacker = MessagePack.newUnpacker(testData)
8985
var skipCount = 0
90-
while(unpacker.skipValue()) {
86+
while(unpacker.hasNext) {
87+
unpacker.skipValue()
9188
skipCount += 1
9289
}
9390

@@ -101,9 +98,8 @@ class MessageUnpackerTest extends MessagePackSpec {
10198
val ib = Seq.newBuilder[Int]
10299

103100
val unpacker = MessagePack.newUnpacker(testData2)
104-
var f : MessageFormat = null
105-
do {
106-
f = unpacker.getNextFormat
101+
while(unpacker.hasNext) {
102+
val f = unpacker.getNextFormat
107103
f.getValueType match {
108104
case ValueType.INTEGER =>
109105
val i = unpacker.unpackInt()
@@ -115,7 +111,7 @@ class MessageUnpackerTest extends MessagePackSpec {
115111
case other =>
116112
unpacker.skipValue()
117113
}
118-
} while(f != MessageFormat.EOF)
114+
}
119115

120116
ib.result shouldBe intSeq
121117

msgpack-core/src/test/scala/org/msgpack/core/ValueTypeTest.scala

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.msgpack.core
33
import MessagePack.Code._
44
import scala.util.Random
55
import xerial.core.log.LogLevel
6+
import org.msgpack.core.MessagePack.Code
67

78

89
/**
@@ -28,7 +29,16 @@ class ValueTypeTest extends MessagePackSpec {
2829
check(i.toByte, ValueType.ARRAY)
2930

3031
check(NIL, ValueType.NIL)
31-
check(NEVER_USED, ValueType.UNKNOWN)
32+
33+
try {
34+
ValueType.valueOf(NEVER_USED)
35+
fail("NEVER_USED type should not have ValueType")
36+
}
37+
catch {
38+
case e:MessageFormatException =>
39+
// OK
40+
}
41+
3242
check(TRUE, ValueType.BOOLEAN)
3343
check(FALSE, ValueType.BOOLEAN)
3444

@@ -60,8 +70,10 @@ class ValueTypeTest extends MessagePackSpec {
6070
val N = 1000000
6171
val idx = {
6272
val b = Array.newBuilder[Byte]
63-
for(i <- 0 until N)
64-
b += (Random.nextInt(256)).toByte
73+
for(i <- 0 until N) {
74+
val r = Iterator.continually(Random.nextInt(256)).find(_.toByte != Code.NEVER_USED).get
75+
b += r.toByte
76+
}
6577
b.result()
6678
}
6779

0 commit comments

Comments
 (0)