Skip to content

Commit a590028

Browse files
committed
redesigned Packer and Unpacker interfaces
1 parent 5baaff6 commit a590028

File tree

69 files changed

+1456
-1294
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1456
-1294
lines changed

src/main/java/org/msgpack/MessagePack.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
import java.io.OutputStream;
2222
import java.io.IOException;
2323
import java.nio.ByteBuffer;
24-
2524
import org.msgpack.template.Template;
2625
import org.msgpack.template.TemplateRegistry;
27-
import org.msgpack.packer.StreamPacker;
26+
import org.msgpack.packer.Packer;
2827
import org.msgpack.packer.BufferPacker;
28+
import org.msgpack.packer.MessagePackPacker;
29+
import org.msgpack.packer.MessagePackBufferPacker;
2930
import org.msgpack.packer.Unconverter;
30-
import org.msgpack.unpacker.StreamUnpacker;
31+
import org.msgpack.unpacker.Unpacker;
3132
import org.msgpack.unpacker.BufferUnpacker;
33+
import org.msgpack.unpacker.MessagePackUnpacker;
34+
import org.msgpack.unpacker.MessagePackBufferUnpacker;
3235
import org.msgpack.unpacker.Converter;
3336
import org.msgpack.type.Value;
3437

@@ -45,32 +48,36 @@ public MessagePack(MessagePack msgpack) {
4548
}
4649

4750

48-
public StreamPacker createStreamPacker(OutputStream stream) {
49-
return new StreamPacker(this, stream);
51+
public Packer createPacker(OutputStream stream) {
52+
return new MessagePackPacker(this, stream);
5053
}
5154

5255
public BufferPacker createBufferPacker() {
53-
return new BufferPacker(this);
56+
return new MessagePackBufferPacker(this);
5457
}
5558

5659
public BufferPacker createBufferPacker(int bufferSize) {
57-
return new BufferPacker(this, bufferSize);
60+
return new MessagePackBufferPacker(this, bufferSize);
61+
}
62+
63+
public Unpacker createUnpacker(InputStream stream) {
64+
return new MessagePackUnpacker(this, stream);
5865
}
5966

60-
public StreamUnpacker createStreamUnpacker(InputStream stream) {
61-
return new StreamUnpacker(this, stream);
67+
public BufferUnpacker createBufferUnpacker() {
68+
return new MessagePackBufferUnpacker();
6269
}
6370

6471
public BufferUnpacker createBufferUnpacker(byte[] b) {
65-
return new BufferUnpacker(this).wrap(b);
72+
return createBufferUnpacker().wrap(b);
6673
}
6774

6875
public BufferUnpacker createBufferUnpacker(byte[] b, int off, int len) {
69-
return new BufferUnpacker(this).wrap(b, off, len);
76+
return createBufferUnpacker().wrap(b, off, len);
7077
}
7178

7279
public BufferUnpacker createBufferUnpacker(ByteBuffer bb) {
73-
return new BufferUnpacker(this).wrap(bb);
80+
return createBufferUnpacker().wrap(bb);
7481
}
7582

7683

@@ -89,7 +96,7 @@ public void write(OutputStream out, Object v) throws IOException {
8996
}
9097

9198
public <T> void write(OutputStream out, T v, Template<T> tmpl) throws IOException {
92-
StreamPacker pk = createStreamPacker(out);
99+
Packer pk = createPacker(out);
93100
tmpl.write(pk, v);
94101
}
95102

@@ -113,7 +120,7 @@ public Value read(ByteBuffer buf) throws IOException { // TODO IOException
113120
}
114121

115122
public Value read(InputStream in) throws IOException {
116-
return createStreamUnpacker(in).readValue();
123+
return createUnpacker(in).readValue();
117124
}
118125

119126
public <T> T read(byte[] b, T v) throws IOException { // TODO IOException
@@ -143,12 +150,12 @@ public <T> T read(ByteBuffer b, Class<T> c) { // TODO IOException
143150

144151
public <T> T read(InputStream in, T v) throws IOException {
145152
Template<T> tmpl = registry.lookup(v.getClass());
146-
return tmpl.read(createStreamUnpacker(in), v);
153+
return tmpl.read(createUnpacker(in), v);
147154
}
148155

149156
public <T> T read(InputStream in, Class<T> c) throws IOException {
150157
Template<T> tmpl = registry.lookup(c);
151-
return tmpl.read(createStreamUnpacker(in), null);
158+
return tmpl.read(createUnpacker(in), null);
152159
}
153160

154161
public <T> T convert(Value v, T to) throws IOException { // TODO IOException
@@ -214,13 +221,13 @@ public static Value unpack(byte[] buffer) throws IOException {
214221

215222
@Deprecated
216223
public static <T> T unpack(byte[] buffer, Template<T> tmpl) throws IOException {
217-
BufferUnpacker u = new BufferUnpacker(globalMessagePack).wrap(buffer);
224+
BufferUnpacker u = new MessagePackBufferUnpacker(globalMessagePack).wrap(buffer);
218225
return tmpl.read(u, null);
219226
}
220227

221228
@Deprecated
222229
public static <T> T unpack(byte[] buffer, Template<T> tmpl, T to) throws IOException {
223-
BufferUnpacker u = new BufferUnpacker(globalMessagePack).wrap(buffer);
230+
BufferUnpacker u = new MessagePackBufferUnpacker(globalMessagePack).wrap(buffer);
224231
return tmpl.read(u, to);
225232
}
226233

@@ -241,12 +248,12 @@ public static Value unpack(InputStream in) throws IOException {
241248

242249
@Deprecated
243250
public static <T> T unpack(InputStream in, Template<T> tmpl) throws IOException, MessageTypeException {
244-
return tmpl.read(new StreamUnpacker(globalMessagePack, in), null);
251+
return tmpl.read(new MessagePackUnpacker(globalMessagePack, in), null);
245252
}
246253

247254
@Deprecated
248255
public static <T> T unpack(InputStream in, Template<T> tmpl, T to) throws IOException, MessageTypeException {
249-
return (T) tmpl.read(new StreamUnpacker(globalMessagePack, in), to);
256+
return (T) tmpl.read(new MessagePackUnpacker(globalMessagePack, in), to);
250257
}
251258

252259
@Deprecated
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
package org.msgpack.packer;
19+
20+
import java.math.BigInteger;
21+
import java.io.IOException;
22+
import java.nio.ByteBuffer;
23+
import org.msgpack.type.Value;
24+
import org.msgpack.MessagePack;
25+
import org.msgpack.template.Template;
26+
27+
28+
public abstract class AbstractPacker implements Packer {
29+
protected MessagePack msgpack;
30+
31+
protected AbstractPacker(MessagePack msgpack) {
32+
this.msgpack = msgpack;
33+
}
34+
35+
public void writeByteArray(byte[] b) throws IOException {
36+
writeByteArray(b, 0, b.length);
37+
}
38+
39+
40+
public void writeArrayEnd() throws IOException {
41+
writeArrayEnd(true);
42+
}
43+
44+
public void writeMapEnd() throws IOException {
45+
writeMapEnd(true);
46+
}
47+
48+
49+
public Packer write(Object o) throws IOException {
50+
Template tmpl = msgpack.lookup(o.getClass());
51+
tmpl.write(this, o);
52+
return this;
53+
}
54+
55+
public Packer writeOptional(Object o) throws IOException {
56+
if(o == null) {
57+
writeNil();
58+
return this;
59+
}
60+
return write(o);
61+
}
62+
63+
public Packer write(Value v) throws IOException {
64+
v.writeTo(this);
65+
return this;
66+
}
67+
68+
69+
public void close() throws IOException {
70+
}
71+
72+
// public Packer write(Object o) throws IOException {
73+
// msgpack.lookup(o.getClass()).write(this, o);
74+
// return this;
75+
// }
76+
//
77+
// public Packer write(Value v) throws IOException {
78+
// v.writeTo(this);
79+
// return this;
80+
// }
81+
//
82+
// public Packer write(MessagePackable v) throws IOException {
83+
// v.writeTo(this);
84+
// return this;
85+
// }
86+
//
87+
// public Packer write(boolean v) throws IOException {
88+
// writeBoolean(v);
89+
// return this;
90+
// }
91+
//
92+
// public Packer write(byte v) throws IOException {
93+
// writeByte(v);
94+
// return this;
95+
// }
96+
//
97+
// public Packer write(short v) throws IOException {
98+
// writeShort(v);
99+
// return this;
100+
// }
101+
//
102+
// public Packer write(int v) throws IOException {
103+
// writeInt(v);
104+
// return this;
105+
// }
106+
//
107+
// public Packer write(long v) throws IOException {
108+
// writeLong(v);
109+
// return this;
110+
// }
111+
//
112+
// public Packer write(float v) throws IOException {
113+
// writeFloat(v);
114+
// return this;
115+
// }
116+
//
117+
// public Packer write(double v) throws IOException {
118+
// writeDouble(v);
119+
// return this;
120+
// }
121+
//
122+
// public Packer write(Boolean v) throws IOException {
123+
// if(v == null) {
124+
// writeNil();
125+
// } else {
126+
// writeBoolean(v);
127+
// }
128+
// return this;
129+
// }
130+
//
131+
// public Packer write(Byte v) throws IOException {
132+
// if(v == null) {
133+
// writeNil();
134+
// } else {
135+
// writeByte(v);
136+
// }
137+
// return this;
138+
// }
139+
//
140+
// public Packer write(Short v) throws IOException {
141+
// if(v == null) {
142+
// writeNil();
143+
// } else {
144+
// writeShort(v);
145+
// }
146+
// return this;
147+
// }
148+
//
149+
// public Packer write(Integer v) throws IOException {
150+
// if(v == null) {
151+
// writeNil();
152+
// } else {
153+
// writeInt(v);
154+
// }
155+
// return this;
156+
// }
157+
//
158+
// public Packer write(Long v) throws IOException {
159+
// if(v == null) {
160+
// writeNil();
161+
// } else {
162+
// writeLong(v);
163+
// }
164+
// return this;
165+
// }
166+
//
167+
// public Packer write(Float v) throws IOException {
168+
// if(v == null) {
169+
// writeNil();
170+
// } else {
171+
// writeFloat(v);
172+
// }
173+
// return this;
174+
// }
175+
//
176+
// public Packer write(Double v) throws IOException {
177+
// if(v == null) {
178+
// writeNil();
179+
// } else {
180+
// writeDouble(v);
181+
// }
182+
// return this;
183+
// }
184+
//
185+
// public Packer write(String s) throws IOException {
186+
// writeString(s);
187+
// return this;
188+
// }
189+
//
190+
// public Packer write(byte[] b) throws IOException {
191+
// writeByteArray(b);
192+
// return this;
193+
// }
194+
//
195+
// public Packer write(byte[] b, int off, int len) throws IOException {
196+
// writeByteArray(b, off, len);
197+
// return this;
198+
// }
199+
//
200+
// //public Packer write(ByteBuffer b) throws IOException {
201+
// // writeByteBuffer(b);
202+
// // return this;
203+
// //}
204+
}
205+

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

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,8 @@
1717
//
1818
package org.msgpack.packer;
1919

20-
import org.msgpack.MessagePack;
21-
import org.msgpack.io.LinkedBufferOutput;
2220

23-
24-
public class BufferPacker extends AbstractMessagePackPacker {
25-
private static final int DEFAULT_BUFFER_SIZE = 512; // TODO default buffer size
26-
27-
public BufferPacker() {
28-
this(DEFAULT_BUFFER_SIZE);
29-
}
30-
31-
public BufferPacker(int bufferSize) {
32-
this(new MessagePack(), bufferSize);
33-
}
34-
35-
public BufferPacker(MessagePack msgpack) {
36-
this(msgpack, DEFAULT_BUFFER_SIZE);
37-
}
38-
39-
public BufferPacker(MessagePack msgpack, int bufferSize) {
40-
super(msgpack, new LinkedBufferOutput(bufferSize));
41-
}
42-
43-
public byte[] toByteArray() {
44-
LinkedBufferOutput bo = (LinkedBufferOutput) out;
45-
return ((LinkedBufferOutput) bo).toByteArray();
46-
}
21+
public interface BufferPacker extends Packer {
22+
public byte[] toByteArray();
4723
}
4824

0 commit comments

Comments
 (0)