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
202 lines (168 loc) · 5.97 KB
/
Copy pathMessagePack.java
File metadata and controls
202 lines (168 loc) · 5.97 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
//
// 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 java.math.BigInteger;
import org.msgpack.template.Template;
import org.msgpack.packer.Packer;
import org.msgpack.packer.StreamPacker;
import org.msgpack.packer.BufferPacker;
import org.msgpack.packer.Unconverter;
import org.msgpack.unpacker.Unpacker;
import org.msgpack.unpacker.StreamUnpacker;
import org.msgpack.unpacker.BufferUnpacker;
import org.msgpack.unpacker.Converter;
import org.msgpack.value.Value;
import org.msgpack.template.*;
public class MessagePack {
private TemplateRegistry registry;
public MessagePack() {
registry = new TemplateRegistry();
}
public MessagePack(MessagePack parent) {
registry = new TemplateRegistry(parent.registry);
}
public byte[] pack(Object v) throws IOException {
return pack(v, getTemplate(v.getClass()));
}
public byte[] pack(Object v, Template tmpl) throws IOException { // TODO IOException
BufferPacker pk = new BufferPacker();
tmpl.write(pk, v);
return pk.toByteArray();
}
public void pack(OutputStream out, Object v) throws IOException {
pack(out, v, getTemplate(v.getClass()));
}
public void pack(OutputStream out, Object v, Template tmpl) throws IOException {
StreamPacker pk = new StreamPacker(out);
tmpl.write(pk, v);
}
public byte[] pack(Value v) throws IOException { // TODO IOException
// FIXME ValueTemplate should do this
BufferPacker pk = new BufferPacker();
pk.write(v);
return pk.toByteArray();
}
public <T> T unpack(InputStream in, T v) throws IOException {
// TODO
Template tmpl = getTemplate(v.getClass());
return (T)tmpl.read(new StreamUnpacker(in), v);
}
public <T> T unpack(InputStream in, Class<T> c) throws IOException {
// TODO
Template tmpl = getTemplate(c);
return (T)tmpl.read(new StreamUnpacker(in), null);
}
public Value unpack(byte[] b) throws IOException { // TODO IOException
return unpack(b, 0, b.length);
}
public Value unpack(byte[] b, int off, int len) throws IOException { // TODO IOException
return new BufferUnpacker().wrap(b, off, len).readValue();
}
public Value unpack(ByteBuffer buf) throws IOException { // TODO IOException
return new BufferUnpacker().wrap(buf).readValue();
}
public <T> T unpack(byte[] b, T v) throws IOException { // TODO IOException
// TODO
Template tmpl = getTemplate(v.getClass());
BufferUnpacker u = new BufferUnpacker();
u.wrap(b);
return (T)tmpl.read(u, v);
}
public <T> T unpack(byte[] b, Class<T> c) throws IOException { // TODO IOException
// TODO
Template tmpl = getTemplate(c);
BufferUnpacker u = new BufferUnpacker();
u.wrap(b);
return (T)tmpl.read(u, null);
}
public <T> T unpack(ByteBuffer b, T v) throws IOException { // TODO IOException
// TODO
Template tmpl = getTemplate(v.getClass());
BufferUnpacker u = new BufferUnpacker();
u.wrap(b);
return (T)tmpl.read(u, v);
}
public <T> T unpack(ByteBuffer b, Class<T> c) { // TODO IOException
// TODO
Template tmpl = getTemplate(c);
BufferUnpacker u = new BufferUnpacker();
u.wrap(b);
return null;
}
public <T> T convert(Value v, T to) throws IOException { // TODO IOException
// TODO
Template tmpl = getTemplate(to.getClass());
return (T)tmpl.read(new Converter(v), to);
}
public <T> T convert(Value v, Class<T> c) throws IOException { // TODO IOException
// TODO
Template tmpl = getTemplate(c);
return (T)tmpl.read(new Converter(v), null);
}
public Value unconvert(Object v) throws IOException { // TODO IOException
Template tmpl = getTemplate(v.getClass());
Unconverter pk = new Unconverter();
tmpl.write(pk, v);
return pk.getResult();
}
protected Template getTemplate(Class<?> c) {
Template tmpl = registry.lookup(c);
if(tmpl == null) {
throw new MessageTypeException("Can't find template for "+c+" class. Try to add @Message annotation to the class or call MessagePack.register(Type).");
}
return tmpl;
}
public void register(Class<?> type) {
registry.register(type);
}
public void registerTemplate(Class<?> type, Template tmpl) {
registry.register(type, tmpl);
}
/*
// TODO
private static final MessagePack globalMessagePack;
@Deprecated
public static <T> T unpack(InputStream in, T v) {
return globalMessagePack.unpack(in, v);
}
@Deprecated
public static <T> T unpack(InputStream in, Class<T> c) {
return globalMessagePack.unpack(in, c);
}
@Deprecated
public static <T> T unpack(byte[] b, T v) {
return globalMessagePack.unpack(b, v);
}
@Deprecated
public static <T> T unpack(byte[] b, Class<T> c) {
return globalMessagePack.unpack(b, c);
}
@Deprecated
public static <T> T unpack(ByteBuffer b, T v) {
return globalMessagePack.unpack(b, v);
}
@Deprecated
public static <T> T unpack(ByteBuffer b, Class<T> c) {
return globalMessagePack.unpack(b, c);
}
*/
}