Skip to content

Commit d640794

Browse files
committed
added templates
1 parent 78e16a9 commit d640794

25 files changed

Lines changed: 907 additions & 22 deletions

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.OutputStream;
2222
import java.io.IOException;
2323
import java.nio.ByteBuffer;
24+
import java.math.BigInteger;
2425
import org.msgpack.template.Template;
2526
import org.msgpack.packer.Packer;
2627
import org.msgpack.packer.StreamPacker;
@@ -170,11 +171,29 @@ public static <T> T unpack(ByteBuffer b, Class<T> c) {
170171
*/
171172

172173
public static void loadDefaultTemplates(TemplateRegistry reg) {
173-
reg.register(Integer.class, IntTemplate.getInstance());
174-
reg.register(int.class, IntTemplate.getInstance());
175-
reg.register(Short.class, ShortTemplate.getInstance());
174+
reg.register(boolean.class, BooleanTemplate.getInstance());
175+
reg.register(Boolean.class, BooleanTemplate.getInstance());
176+
reg.register(byte.class, ByteTemplate.getInstance());
177+
reg.register(Byte.class, ByteTemplate.getInstance());
176178
reg.register(short.class, ShortTemplate.getInstance());
179+
reg.register(Short.class, ShortTemplate.getInstance());
180+
reg.register(int.class, IntTemplate.getInstance());
181+
reg.register(Integer.class, IntTemplate.getInstance());
182+
reg.register(long.class, LongTemplate.getInstance());
183+
reg.register(Long.class, LongTemplate.getInstance());
184+
reg.register(float.class, FloatTemplate.getInstance());
185+
reg.register(Float.class, FloatTemplate.getInstance());
186+
reg.register(double.class, DoubleTemplate.getInstance());
187+
reg.register(Double.class, DoubleTemplate.getInstance());
188+
reg.register(BigInteger.class, BigIntegerTemplate.getInstance());
189+
reg.register(boolean[].class, ByteArrayTemplate.getInstance());
190+
reg.register(short[].class, ShortArrayTemplate.getInstance());
177191
reg.register(int[].class, IntArrayTemplate.getInstance());
192+
reg.register(long[].class, LongArrayTemplate.getInstance());
193+
reg.register(float[].class, FloatArrayTemplate.getInstance());
194+
reg.register(double[].class, DoubleArrayTemplate.getInstance());
195+
reg.register(String.class, StringTemplate.getInstance());
196+
reg.register(byte[].class, ByteArrayTemplate.getInstance());
178197
reg.register(Value.class, ValueTemplate.getInstance());
179198
}
180199
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public void writeBoolean(boolean d) throws IOException {
189189
}
190190

191191
@Override
192-
public void writeBytes(byte[] b, int off, int len) throws IOException {
192+
public void writeByteArray(byte[] b, int off, int len) throws IOException {
193193
if(len < 32) {
194194
out.writeByte((byte)(0xa0 | len));
195195
} else if(len < 65536) {
@@ -210,7 +210,7 @@ public void writeString(String s) throws IOException {
210210
} catch (UnsupportedEncodingException ex) {
211211
throw new MessageTypeException();
212212
}
213-
writeBytes(b, 0, b.length);
213+
writeByteArray(b, 0, b.length);
214214
stack.reduceCount();
215215
}
216216

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public abstract class Packer {
4242

4343
public abstract void writeDouble(double v) throws IOException;
4444

45-
public void writeBytes(byte[] b) throws IOException {
46-
writeBytes(b, 0, b.length);
45+
public void writeByteArray(byte[] b) throws IOException {
46+
writeByteArray(b, 0, b.length);
4747
}
4848

49-
public abstract void writeBytes(byte[] b, int off, int len) throws IOException;
49+
public abstract void writeByteArray(byte[] b, int off, int len) throws IOException;
5050

51-
//public abstract void writeBytes(ByteBuffer b) throws IOException;
51+
//public abstract void writeByteArray(ByteBuffer b) throws IOException;
5252

5353
public abstract void writeString(String s) throws IOException;
5454

@@ -167,13 +167,13 @@ public void write(Double v) throws IOException {
167167
public abstract void write(String s) throws IOException;
168168
169169
public void write(byte[] b) throws IOException {
170-
writeBytesBegin(b.length);
171-
writeBytesBody(b);
170+
writeByteArray(b.length);
171+
writeByteArray(b);
172172
}
173173
174174
public void write(byte[] b, int off, int len) throws IOException {
175-
writeBytesBegin(len);
176-
writeBytes(b, off, len);
175+
writeByteArray(len);
176+
writeByteArray(b, off, len);
177177
}
178178
179179
public void write(ByteBuffer b) throws IOException {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void writeDouble(double v) {
8787
}
8888

8989
@Override
90-
public void writeBytes(byte[] b, int off, int len) {
90+
public void writeByteArray(byte[] b, int off, int len) {
9191
put(ValueFactory.rawValue(b, off, len));
9292
}
9393

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Copyright (C) 2009-2010 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.template;
19+
20+
import java.io.IOException;
21+
import java.math.BigInteger;
22+
import org.msgpack.packer.Packer;
23+
import org.msgpack.unpacker.Unpacker;
24+
import org.msgpack.MessageTypeException;
25+
26+
public class BigIntegerTemplate implements Template {
27+
private BigIntegerTemplate() { }
28+
29+
public void write(Packer pk, Object target) throws IOException {
30+
if(target == null) {
31+
throw new MessageTypeException("Attempted to write null");
32+
}
33+
pk.writeBigInteger((BigInteger)target);
34+
}
35+
36+
public Object read(Unpacker u, Object to) throws IOException {
37+
return u.readBigInteger();
38+
}
39+
40+
static public BigIntegerTemplate getInstance() {
41+
return instance;
42+
}
43+
44+
static final BigIntegerTemplate instance = new BigIntegerTemplate();
45+
}
46+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Copyright (C) 2009-2010 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.template;
19+
20+
import java.io.IOException;
21+
import org.msgpack.packer.Packer;
22+
import org.msgpack.unpacker.Unpacker;
23+
import org.msgpack.MessageTypeException;
24+
25+
public class BooleanArrayTemplate implements Template {
26+
private BooleanArrayTemplate() { }
27+
28+
public void write(Packer pk, Object target) throws IOException {
29+
if(target == null) {
30+
throw new MessageTypeException("Attempted to write null");
31+
}
32+
boolean[] array = (boolean[]) target;
33+
pk.writeArrayBegin(array.length);
34+
for(boolean a : array) {
35+
pk.writeBoolean(a);
36+
}
37+
pk.writeArrayEnd();
38+
}
39+
40+
public Object read(Unpacker u, Object to) throws IOException {
41+
int n = u.readArrayBegin();
42+
boolean[] array;
43+
if(to != null && ((boolean[]) to).length == n) {
44+
array = (boolean[]) to;
45+
} else {
46+
array = new boolean[n];
47+
}
48+
for(int i=0; i < n; i++) {
49+
array[i] = u.readBoolean();
50+
}
51+
u.readArrayEnd();
52+
return array;
53+
}
54+
55+
static public BooleanArrayTemplate getInstance() {
56+
return instance;
57+
}
58+
59+
static final BooleanArrayTemplate instance = new BooleanArrayTemplate();
60+
}
61+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Copyright (C) 2009-2010 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.template;
19+
20+
import java.io.IOException;
21+
import org.msgpack.packer.Packer;
22+
import org.msgpack.unpacker.Unpacker;
23+
import org.msgpack.MessageTypeException;
24+
25+
public class BooleanTemplate implements Template {
26+
private BooleanTemplate() { }
27+
28+
public void write(Packer pk, Object target) throws IOException {
29+
if(target == null) {
30+
throw new MessageTypeException("Attempted to write null");
31+
}
32+
pk.writeBoolean((Boolean)target);
33+
}
34+
35+
public Object read(Unpacker u, Object to) throws IOException {
36+
return u.readBoolean();
37+
}
38+
39+
static public BooleanTemplate getInstance() {
40+
return instance;
41+
}
42+
43+
static final BooleanTemplate instance = new BooleanTemplate();
44+
}
45+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Copyright (C) 2009-2010 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.template;
19+
20+
import java.io.IOException;
21+
import org.msgpack.packer.Packer;
22+
import org.msgpack.unpacker.Unpacker;
23+
import org.msgpack.MessageTypeException;
24+
25+
public class ByteArrayTemplate implements Template {
26+
private ByteArrayTemplate() { }
27+
28+
public void write(Packer pk, Object target) throws IOException {
29+
if(target == null) {
30+
throw new MessageTypeException("Attempted to write null");
31+
}
32+
byte[] array = (byte[]) target;
33+
pk.writeByteArray(array);
34+
}
35+
36+
public Object read(Unpacker u, Object to) throws IOException {
37+
return u.readByteArray(); // TODO read to 'to' obj
38+
}
39+
40+
static public ByteArrayTemplate getInstance() {
41+
return instance;
42+
}
43+
44+
static final ByteArrayTemplate instance = new ByteArrayTemplate();
45+
}
46+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Copyright (C) 2009-2010 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.template;
19+
20+
import java.io.IOException;
21+
import org.msgpack.packer.Packer;
22+
import org.msgpack.unpacker.Unpacker;
23+
import org.msgpack.MessageTypeException;
24+
25+
public class ByteTemplate implements Template {
26+
private ByteTemplate() { }
27+
28+
public void write(Packer pk, Object target) throws IOException {
29+
if(target == null) {
30+
throw new MessageTypeException("Attempted to write null");
31+
}
32+
pk.writeByte((Byte)target);
33+
}
34+
35+
public Object read(Unpacker u, Object to) throws IOException {
36+
return u.readByte();
37+
}
38+
39+
static public ByteTemplate getInstance() {
40+
return instance;
41+
}
42+
43+
static final ByteTemplate instance = new ByteTemplate();
44+
}
45+

0 commit comments

Comments
 (0)