forked from msgpack/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessagePack.java
More file actions
241 lines (198 loc) · 7.39 KB
/
Copy pathMessagePack.java
File metadata and controls
241 lines (198 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// MessagePack for Java
//
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.msgpack.template.Template;
import org.msgpack.packer.StreamPacker;
import org.msgpack.packer.BufferPacker;
import org.msgpack.packer.Unconverter;
import org.msgpack.unpacker.StreamUnpacker;
import org.msgpack.unpacker.BufferUnpacker;
import org.msgpack.unpacker.Converter;
import org.msgpack.value.Value;
public class MessagePack {
private TemplateRegistry registry;
public MessagePack() {
registry = new TemplateRegistry();
}
public MessagePack(MessagePack msgpack) {
registry = new TemplateRegistry(msgpack.registry);
}
public byte[] write(Object v) throws IOException {
return write(v, registry.lookup(v.getClass()));
}
public byte[] write(Object v, Template tmpl) throws IOException { // TODO IOException
BufferPacker pk = new BufferPacker();
tmpl.write(pk, v);
return pk.toByteArray();
}
public void write(OutputStream out, Object v) throws IOException {
write(out, v, registry.lookup(v.getClass()));
}
public void write(OutputStream out, Object v, Template tmpl) throws IOException {
StreamPacker pk = new StreamPacker(out);
tmpl.write(pk, v);
}
public byte[] write(Value v) throws IOException { // TODO IOException
// FIXME ValueTemplate should do this
BufferPacker pk = new BufferPacker();
pk.write(v);
return pk.toByteArray();
}
public Value read(byte[] b) throws IOException { // TODO IOException
return read(b, 0, b.length);
}
public Value read(byte[] b, int off, int len) throws IOException { // TODO IOException
return new BufferUnpacker().wrap(b, off, len).readValue();
}
public Value read(ByteBuffer buf) throws IOException { // TODO IOException
return new BufferUnpacker().wrap(buf).readValue();
}
public Value read(InputStream in) throws IOException {
return new StreamUnpacker(in).readValue();
}
public <T> T read(byte[] b, T v) throws IOException { // TODO IOException
// TODO
Template tmpl = registry.lookup(v.getClass());
BufferUnpacker u = new BufferUnpacker();
u.wrap(b);
return (T)tmpl.read(u, v);
}
public <T> T read(byte[] b, Class<T> c) throws IOException { // TODO IOException
// TODO
Template tmpl = registry.lookup(c);
BufferUnpacker u = new BufferUnpacker();
u.wrap(b);
return (T)tmpl.read(u, null);
}
public <T> T read(ByteBuffer b, T v) throws IOException { // TODO IOException
// TODO
Template tmpl = registry.lookup(v.getClass());
BufferUnpacker u = new BufferUnpacker();
u.wrap(b);
return (T)tmpl.read(u, v);
}
public <T> T read(ByteBuffer b, Class<T> c) { // TODO IOException
// TODO
Template tmpl = registry.lookup(c);
BufferUnpacker u = new BufferUnpacker();
u.wrap(b);
return null;
}
public <T> T read(InputStream in, T v) throws IOException {
// TODO
Template tmpl = registry.lookup(v.getClass());
return (T)tmpl.read(new StreamUnpacker(in), v);
}
public <T> T read(InputStream in, Class<T> c) throws IOException {
// TODO
Template tmpl = registry.lookup(c);
return (T)tmpl.read(new StreamUnpacker(in), null);
}
public <T> T convert(Value v, T to) throws IOException { // TODO IOException
// TODO
Template tmpl = registry.lookup(to.getClass());
return (T)tmpl.read(new Converter(v), to);
}
public <T> T convert(Value v, Class<T> c) throws IOException {
Template tmpl = registry.lookup(c);
return (T) tmpl.read(new Converter(v), null);
}
public Value unconvert(Object v) throws IOException {
Template tmpl = registry.lookup(v.getClass());
Unconverter pk = new Unconverter();
tmpl.write(pk, v);
return pk.getResult();
}
public void register(Class<?> type) {
registry.register(type);
}
// TODO #MN
// public void forceRegister(Class<?> type);
public void register(Class<?> type, Template tmpl) {
registry.register(type, tmpl);
}
public Template getTemplate(Class<?> type) {
return registry.lookup(type);
}
private static final MessagePack globalMessagePack = new MessagePack();
@Deprecated
public static byte[] pack(Object obj) throws IOException { // TODO IOException
return globalMessagePack.write(obj);
}
@Deprecated
public static void pack(OutputStream out, Object obj) throws IOException {
globalMessagePack.write(out, obj);
}
@Deprecated
public static byte[] pack(Object obj, Template tmpl) throws IOException { // TODO IOException
BufferPacker pk = new BufferPacker();
tmpl.write(pk, obj);
return pk.toByteArray();
}
@Deprecated
public static void pack(OutputStream out, Object obj, Template tmpl) throws IOException {
StreamPacker pk = new StreamPacker(out);
tmpl.write(pk, obj);
}
@Deprecated
public static Value unpack(byte[] buffer) throws IOException {
return globalMessagePack.read(buffer);
}
@Deprecated
public static <T> T unpack(byte[] buffer, Template tmpl) throws IOException {
BufferUnpacker u = new BufferUnpacker().wrap(buffer);
return (T)tmpl.read(u, null);
}
@Deprecated
public static <T> T unpack(byte[] buffer, Template tmpl, T to) throws IOException {
BufferUnpacker u = new BufferUnpacker().wrap(buffer);
return (T)tmpl.read(u, to);
}
@Deprecated
public static <T> T unpack(byte[] buffer, Class<T> klass) throws IOException {
return globalMessagePack.read(buffer, klass);
}
@Deprecated
public static <T> T unpack(byte[] buffer, T to) throws IOException {
return globalMessagePack.read(buffer, to);
}
@Deprecated
public static Value unpack(InputStream in) throws IOException {
return globalMessagePack.read(in);
}
@Deprecated
public static Object unpack(InputStream in, Template tmpl) throws IOException, MessageTypeException {
return tmpl.read(new StreamUnpacker(in), null);
}
@Deprecated
public static <T> T unpack(InputStream in, Template tmpl, T to) throws IOException, MessageTypeException {
return (T)tmpl.read(new StreamUnpacker(in), to);
}
@Deprecated
public static <T> T unpack(InputStream in, Class<T> klass) throws IOException {
return globalMessagePack.read(in, klass);
}
@Deprecated
public static <T> T unpack(InputStream in, T to) throws IOException {
return globalMessagePack.read(in, to);
}
}