Skip to content

Commit 8e1c40f

Browse files
committed
added Packer.write and Unpacker.read methods
1 parent 7fed352 commit 8e1c40f

4 files changed

Lines changed: 153 additions & 120 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public Value unconvert(Object v) throws IOException { // TODO IOException
150150
return pk.getResult();
151151
}
152152

153-
protected Template getTemplate(Class<?> c) {
153+
public Template getTemplate(Class<?> c) {
154154
Template tmpl = registry.lookup(c);
155155
if(tmpl == null) {
156156
throw new MessageTypeException("Can't find template for "+c+" class. Try to add @Message annotation to the class or call MessagePack.register(Type).");

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

Lines changed: 141 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
import java.io.IOException;
2222
import java.nio.ByteBuffer;
2323
import org.msgpack.value.Value;
24+
import org.msgpack.MessagePack;
2425
import org.msgpack.MessagePackable;
2526

2627
public abstract class Packer {
28+
protected MessagePack msgpack; // TODO initialize
29+
2730
public abstract void writeNil() throws IOException;
2831

2932
public abstract void writeBoolean(boolean v) throws IOException;
@@ -68,120 +71,148 @@ public void writeMapEnd() throws IOException {
6871
writeMapEnd(true);
6972
}
7073

71-
public void write(Value v) throws IOException {
72-
v.writeTo(this);
73-
}
74-
75-
public void write(MessagePackable v) throws IOException {
76-
v.writeTo(this);
77-
}
78-
79-
/* TODO
80-
public void write(boolean v) throws IOException {
81-
writeBoolean(v);
82-
}
83-
84-
public void write(byte v) throws IOException {
85-
writeByte(v);
86-
}
87-
88-
public void write(short v) throws IOException {
89-
writeShort(v);
90-
}
91-
92-
public void write(int v) throws IOException {
93-
writeInt(v);
94-
}
95-
96-
public void write(long v) throws IOException {
97-
writeLong(v);
98-
}
99-
100-
public void write(float v) throws IOException {
101-
writeFloat(v);
102-
}
103-
104-
public void write(double v) throws IOException {
105-
writeDouble(v);
106-
}
107-
108-
public void write(Boolean v) throws IOException {
109-
if(v == null) {
110-
writeNil();
111-
} else {
112-
writeBoolean(v);
113-
}
114-
}
115-
116-
public void write(Byte v) throws IOException {
117-
if(v == null) {
118-
writeNil();
119-
} else {
120-
writeByte(v);
121-
}
122-
}
123-
124-
public void write(Short v) throws IOException {
125-
if(v == null) {
126-
writeNil();
127-
} else {
128-
writeShort(v);
129-
}
130-
}
131-
132-
public void write(Integer v) throws IOException {
133-
if(v == null) {
134-
writeNil();
135-
} else {
136-
writeInt(v);
137-
}
138-
}
139-
140-
public void write(Long v) throws IOException {
141-
if(v == null) {
142-
writeNil();
143-
} else {
144-
writeLong(v);
145-
}
146-
}
147-
148-
public void write(Float v) throws IOException {
149-
if(v == null) {
150-
writeNil();
151-
} else {
152-
writeFloat(v);
153-
}
154-
}
155-
156-
public void write(Double v) throws IOException {
157-
if(v == null) {
158-
writeNil();
159-
} else {
160-
writeDouble(v);
161-
}
162-
}
163-
164-
public abstract void write(String s) throws IOException;
165-
166-
public void write(byte[] b) throws IOException {
167-
writeByteArray(b.length);
168-
writeByteArray(b);
169-
}
17074

171-
public void write(byte[] b, int off, int len) throws IOException {
172-
writeByteArray(len);
173-
writeByteArray(b, off, len);
75+
public Packer write(Object o) throws IOException {
76+
msgpack.getTemplate(o.getClass()).write(this, o);
77+
return this;
17478
}
17579

176-
public void write(ByteBuffer b) throws IOException {
177-
// TODO
178-
}
179-
180-
public void write(MessagePackable o) throws IOException {
181-
o.writeTo(this);
80+
public Packer write(Value v) throws IOException {
81+
v.writeTo(this);
82+
return this;
18283
}
18384

184-
public abstract void write(Object o) throws IOException;
185-
*/
85+
// public Packer write(Object o) throws IOException {
86+
// msgpack.getTemplate(o.getClass()).write(this, o);
87+
// return this;
88+
// }
89+
//
90+
// public Packer write(Value v) throws IOException {
91+
// v.writeTo(this);
92+
// return this;
93+
// }
94+
//
95+
// public Packer write(MessagePackable v) throws IOException {
96+
// v.writeTo(this);
97+
// return this;
98+
// }
99+
//
100+
// public Packer write(boolean v) throws IOException {
101+
// writeBoolean(v);
102+
// return this;
103+
// }
104+
//
105+
// public Packer write(byte v) throws IOException {
106+
// writeByte(v);
107+
// return this;
108+
// }
109+
//
110+
// public Packer write(short v) throws IOException {
111+
// writeShort(v);
112+
// return this;
113+
// }
114+
//
115+
// public Packer write(int v) throws IOException {
116+
// writeInt(v);
117+
// return this;
118+
// }
119+
//
120+
// public Packer write(long v) throws IOException {
121+
// writeLong(v);
122+
// return this;
123+
// }
124+
//
125+
// public Packer write(float v) throws IOException {
126+
// writeFloat(v);
127+
// return this;
128+
// }
129+
//
130+
// public Packer write(double v) throws IOException {
131+
// writeDouble(v);
132+
// return this;
133+
// }
134+
//
135+
// public Packer write(Boolean v) throws IOException {
136+
// if(v == null) {
137+
// writeNil();
138+
// } else {
139+
// writeBoolean(v);
140+
// }
141+
// return this;
142+
// }
143+
//
144+
// public Packer write(Byte v) throws IOException {
145+
// if(v == null) {
146+
// writeNil();
147+
// } else {
148+
// writeByte(v);
149+
// }
150+
// return this;
151+
// }
152+
//
153+
// public Packer write(Short v) throws IOException {
154+
// if(v == null) {
155+
// writeNil();
156+
// } else {
157+
// writeShort(v);
158+
// }
159+
// return this;
160+
// }
161+
//
162+
// public Packer write(Integer v) throws IOException {
163+
// if(v == null) {
164+
// writeNil();
165+
// } else {
166+
// writeInt(v);
167+
// }
168+
// return this;
169+
// }
170+
//
171+
// public Packer write(Long v) throws IOException {
172+
// if(v == null) {
173+
// writeNil();
174+
// } else {
175+
// writeLong(v);
176+
// }
177+
// return this;
178+
// }
179+
//
180+
// public Packer write(Float v) throws IOException {
181+
// if(v == null) {
182+
// writeNil();
183+
// } else {
184+
// writeFloat(v);
185+
// }
186+
// return this;
187+
// }
188+
//
189+
// public Packer write(Double v) throws IOException {
190+
// if(v == null) {
191+
// writeNil();
192+
// } else {
193+
// writeDouble(v);
194+
// }
195+
// return this;
196+
// }
197+
//
198+
// public Packer write(String s) throws IOException {
199+
// writeString(s);
200+
// return this;
201+
// }
202+
//
203+
// public Packer write(byte[] b) throws IOException {
204+
// writeByteArray(b);
205+
// return this;
206+
// }
207+
//
208+
// public Packer write(byte[] b, int off, int len) throws IOException {
209+
// writeByteArray(b, off, len);
210+
// return this;
211+
// }
212+
//
213+
// //public Packer write(ByteBuffer b) throws IOException {
214+
// // writeByteBuffer(b);
215+
// // return this;
216+
// //}
186217
}
187218

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ public void writeMapEnd(boolean check) {
170170
}
171171

172172
@Override
173-
public void write(Value v) {
173+
public Packer write(Value v) {
174174
put(v);
175+
return this;
175176
}
176177

177178
private void put(Value v) {

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
import java.util.NoSuchElementException;
2626
import java.lang.Iterable;
2727
import org.msgpack.value.Value;
28+
import org.msgpack.MessagePack;
2829
import org.msgpack.packer.Unconverter;
2930

3031
public abstract class Unpacker implements Iterable<Value> {
32+
protected MessagePack msgpack; // TODO initialize
33+
3134
public abstract void readNil() throws IOException;
3235

3336
public abstract boolean tryReadNil() throws IOException;
@@ -92,14 +95,12 @@ public Value readValue() throws IOException {
9295
}
9396

9497

95-
//public <T> T read(T to) throws IOException {
96-
// // TODO template
97-
// return null;
98-
//}
98+
public <T> T read(T to) throws IOException {
99+
return (T)msgpack.getTemplate(to.getClass()).read(this, to);
100+
}
99101

100-
//public <T> T read(Class<T> klass) throws IOException {
101-
// // TODO template
102-
// return null;
103-
//}
102+
public <T> T read(Class<T> klass) throws IOException {
103+
return (T)msgpack.getTemplate(klass).read(this, null);
104+
}
104105
}
105106

0 commit comments

Comments
 (0)