Skip to content

Commit 1a65c33

Browse files
committed
updated interface Packer and Unpacker
1 parent e1ee245 commit 1a65c33

File tree

8 files changed

+80
-68
lines changed

8 files changed

+80
-68
lines changed

src/main/java/org/msgpack/packer/AbstractPacker.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,15 @@ public Packer write(Value v) throws IOException {
218218

219219

220220
@Override
221-
public void writeArrayEnd() throws IOException {
221+
public Packer writeArrayEnd() throws IOException {
222222
writeArrayEnd(true);
223+
return this;
223224
}
224225

225226
@Override
226-
public void writeMapEnd() throws IOException {
227+
public Packer writeMapEnd() throws IOException {
227228
writeMapEnd(true);
229+
return this;
228230
}
229231

230232
@Override

src/main/java/org/msgpack/packer/MessagePackPacker.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,14 @@ protected void writeString(String s) throws IOException {
244244
}
245245

246246
@Override
247-
public void writeNil() throws IOException {
247+
public Packer writeNil() throws IOException {
248248
out.writeByte((byte)0xc0);
249249
stack.reduceCount();
250+
return this;
250251
}
251252

252253
@Override
253-
public void writeArrayBegin(int size) throws IOException {
254+
public Packer writeArrayBegin(int size) throws IOException {
254255
// TODO check size < 0?
255256
if(size < 16) {
256257
// FixArray
@@ -262,10 +263,11 @@ public void writeArrayBegin(int size) throws IOException {
262263
}
263264
stack.reduceCount();
264265
stack.pushArray(size);
266+
return this;
265267
}
266268

267269
@Override
268-
public void writeArrayEnd(boolean check) throws IOException {
270+
public Packer writeArrayEnd(boolean check) throws IOException {
269271
if(!stack.topIsArray()) {
270272
throw new MessageTypeException("writeArrayEnd() is called but writeArrayBegin() is not called");
271273
}
@@ -280,10 +282,11 @@ public void writeArrayEnd(boolean check) throws IOException {
280282
}
281283
}
282284
stack.pop();
285+
return this;
283286
}
284287

285288
@Override
286-
public void writeMapBegin(int size) throws IOException {
289+
public Packer writeMapBegin(int size) throws IOException {
287290
// TODO check size < 0?
288291
if(size < 16) {
289292
// FixMap
@@ -295,10 +298,11 @@ public void writeMapBegin(int size) throws IOException {
295298
}
296299
stack.reduceCount();
297300
stack.pushMap(size);
301+
return this;
298302
}
299303

300304
@Override
301-
public void writeMapEnd(boolean check) throws IOException {
305+
public Packer writeMapEnd(boolean check) throws IOException {
302306
if(!stack.topIsMap()) {
303307
throw new MessageTypeException("writeMapEnd() is called but writeMapBegin() is not called");
304308
}
@@ -313,6 +317,7 @@ public void writeMapEnd(boolean check) throws IOException {
313317
}
314318
}
315319
stack.pop();
320+
return this;
316321
}
317322

318323
public void reset() {

src/main/java/org/msgpack/packer/Packer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,18 @@ public interface Packer extends Closeable {
7474
public Packer write(Object o) throws IOException;
7575

7676

77-
public void writeNil() throws IOException;
77+
public Packer writeNil() throws IOException;
7878

79-
public void writeArrayBegin(int size) throws IOException;
79+
public Packer writeArrayBegin(int size) throws IOException;
8080

81-
public void writeArrayEnd(boolean check) throws IOException;
81+
public Packer writeArrayEnd(boolean check) throws IOException;
8282

83-
public void writeArrayEnd() throws IOException;
83+
public Packer writeArrayEnd() throws IOException;
8484

85-
public void writeMapBegin(int size) throws IOException;
85+
public Packer writeMapBegin(int size) throws IOException;
8686

87-
public void writeMapEnd(boolean check) throws IOException;
87+
public Packer writeMapEnd(boolean check) throws IOException;
8888

89-
public void writeMapEnd() throws IOException;
89+
public Packer writeMapEnd() throws IOException;
9090
}
9191

src/main/java/org/msgpack/packer/Unconverter.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ public void resetResult() {
5151
this.result = null;
5252
}
5353

54-
@Override
55-
public void writeNil() throws IOException {
56-
put(ValueFactory.nilValue());
57-
}
58-
5954
@Override
6055
public void writeBoolean(boolean v) throws IOException {
6156
put(ValueFactory.booleanValue(v));
@@ -112,7 +107,13 @@ public void writeString(String s) throws IOException {
112107
}
113108

114109
@Override
115-
public void writeArrayBegin(int size) throws IOException {
110+
public Packer writeNil() throws IOException {
111+
put(ValueFactory.nilValue());
112+
return this;
113+
}
114+
115+
@Override
116+
public Packer writeArrayBegin(int size) throws IOException {
116117
if(size == 0) {
117118
Value[] array = new Value[size];
118119
putContainer(ValueFactory.arrayValue());
@@ -124,10 +125,11 @@ public void writeArrayBegin(int size) throws IOException {
124125
stack.pushArray(size);
125126
values[stack.getDepth()] = array;
126127
}
128+
return this;
127129
}
128130

129131
@Override
130-
public void writeArrayEnd(boolean check) throws IOException {
132+
public Packer writeArrayEnd(boolean check) throws IOException {
131133
if(!stack.topIsArray()) {
132134
throw new MessageTypeException("writeArrayEnd() is called but writeArrayBegin() is not called");
133135
}
@@ -145,10 +147,11 @@ public void writeArrayEnd(boolean check) throws IOException {
145147
if(stack.getDepth() <= 0) {
146148
this.result = (Value) values[0];
147149
}
150+
return this;
148151
}
149152

150153
@Override
151-
public void writeMapBegin(int size) throws IOException {
154+
public Packer writeMapBegin(int size) throws IOException {
152155
stack.checkCount();
153156
if(size == 0) {
154157
putContainer(ValueFactory.mapValue());
@@ -160,10 +163,11 @@ public void writeMapBegin(int size) throws IOException {
160163
stack.pushMap(size);
161164
values[stack.getDepth()] = array;
162165
}
166+
return this;
163167
}
164168

165169
@Override
166-
public void writeMapEnd(boolean check) throws IOException {
170+
public Packer writeMapEnd(boolean check) throws IOException {
167171
if(!stack.topIsMap()) {
168172
throw new MessageTypeException("writeMapEnd() is called but writeMapBegin() is not called");
169173
}
@@ -181,6 +185,7 @@ public void writeMapEnd(boolean check) throws IOException {
181185
if(stack.getDepth() <= 0) {
182186
this.result = (Value) values[0];
183187
}
188+
return this;
184189
}
185190

186191
@Override

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ private void ensureValue() throws IOException {
5656
}
5757
}
5858

59-
@Override
60-
public boolean tryReadNil() throws IOException {
61-
stack.checkCount();
62-
if(getTop().isNil()) {
63-
stack.reduceCount();
64-
if(stack.getDepth() == 0) {
65-
value = null;
66-
}
67-
return true;
68-
}
69-
return false;
70-
}
59+
//@Override
60+
//public boolean tryReadNil() throws IOException {
61+
// stack.checkCount();
62+
// if(getTop().isNil()) {
63+
// stack.reduceCount();
64+
// if(stack.getDepth() == 0) {
65+
// value = null;
66+
// }
67+
// return true;
68+
// }
69+
// return false;
70+
//}
7171

7272
@Override
7373
public boolean trySkipNil() throws IOException {

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,19 @@ private void readRawBodyCont() throws IOException {
319319
}
320320
}
321321

322-
@Override
323-
public boolean tryReadNil() throws IOException {
324-
stack.checkCount();
325-
int b = getHeadByte() & 0xff;
326-
if(b == 0xc0) {
327-
// nil is read
328-
stack.reduceCount();
329-
headByte = REQUIRE_TO_READ_HEAD;
330-
return true;
331-
}
332-
// not nil
333-
return false;
334-
}
322+
//@Override
323+
//public boolean tryReadNil() throws IOException {
324+
// stack.checkCount();
325+
// int b = getHeadByte() & 0xff;
326+
// if(b == 0xc0) {
327+
// // nil is read
328+
// stack.reduceCount();
329+
// headByte = REQUIRE_TO_READ_HEAD;
330+
// return true;
331+
// }
332+
// // not nil
333+
// return false;
334+
//}
335335

336336
@Override
337337
public boolean trySkipNil() throws IOException {

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@
3030
* @version 0.6.0
3131
*/
3232
public interface Unpacker extends Iterable<Value>, Closeable {
33-
public boolean tryReadNil() throws IOException;
34-
35-
public boolean trySkipNil() throws IOException;
36-
37-
public void readNil() throws IOException;
38-
39-
4033
public boolean readBoolean() throws IOException;
4134

4235
public byte readByte() throws IOException;
@@ -57,33 +50,35 @@ public interface Unpacker extends Iterable<Value>, Closeable {
5750

5851
public ByteBuffer readByteBuffer() throws IOException;
5952

53+
public String readString() throws IOException;
54+
55+
public Value readValue() throws IOException;
56+
57+
58+
public void readNil() throws IOException;
6059

6160
public int readArrayBegin() throws IOException;
6261

6362
public void readArrayEnd(boolean check) throws IOException;
6463

6564
public void readArrayEnd() throws IOException;
6665

67-
6866
public int readMapBegin() throws IOException;
6967

7068
public void readMapEnd(boolean check) throws IOException;
7169

7270
public void readMapEnd() throws IOException;
7371

7472

75-
public String readString() throws IOException;
76-
77-
public UnpackerIterator iterator();
73+
public boolean trySkipNil() throws IOException;
7874

7975
public void skip() throws IOException;
8076

81-
82-
public Value readValue() throws IOException;
83-
84-
8577
public <T> T read(Class<T> klass) throws IOException;
8678

8779
public <T> T read(T to) throws IOException;
80+
81+
82+
public UnpackerIterator iterator();
8883
}
8984

src/main/java/org/msgpack/util/json/JSONPacker.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,25 @@ protected void writeString(String s) throws IOException {
187187
}
188188

189189
@Override
190-
public void writeNil() throws IOException {
190+
public Packer writeNil() throws IOException {
191191
beginElement();
192192
out.write(NULL, 0, NULL.length);
193193
endElement();
194+
return this;
194195
}
195196

196197
@Override
197-
public void writeArrayBegin(int size) throws IOException {
198+
public Packer writeArrayBegin(int size) throws IOException {
198199
beginElement();
199200
out.writeByte(LEFT_BR);
200201
endElement();
201202
stack.pushArray(size);
202203
flags[stack.getDepth()] = FLAG_FIRST_ELEMENT;
204+
return this;
203205
}
204206

205207
@Override
206-
public void writeArrayEnd(boolean check) throws IOException {
208+
public Packer writeArrayEnd(boolean check) throws IOException {
207209
if(!stack.topIsArray()) {
208210
throw new MessageTypeException("writeArrayEnd() is called but writeArrayBegin() is not called");
209211
}
@@ -220,19 +222,21 @@ public void writeArrayEnd(boolean check) throws IOException {
220222
stack.pop();
221223

222224
out.writeByte(RIGHT_BR);
225+
return this;
223226
}
224227

225228
@Override
226-
public void writeMapBegin(int size) throws IOException {
229+
public Packer writeMapBegin(int size) throws IOException {
227230
beginElement();
228231
out.writeByte(LEFT_WN);
229232
endElement();
230233
stack.pushMap(size);
231234
flags[stack.getDepth()] = FLAG_FIRST_ELEMENT | FLAG_MAP_KEY;
235+
return this;
232236
}
233237

234238
@Override
235-
public void writeMapEnd(boolean check) throws IOException {
239+
public Packer writeMapEnd(boolean check) throws IOException {
236240
if(!stack.topIsMap()) {
237241
throw new MessageTypeException("writeMapEnd() is called but writeMapBegin() is not called");
238242
}
@@ -249,6 +253,7 @@ public void writeMapEnd(boolean check) throws IOException {
249253
stack.pop();
250254

251255
out.writeByte(RIGHT_WN);
256+
return this;
252257
}
253258

254259
@Override

0 commit comments

Comments
 (0)