summaryrefslogtreecommitdiff
path: root/java/src
diff options
context:
space:
mode:
authorMuga Nishizawa <muga@f11vm.(none)>2010-10-02 17:44:00 +0900
committerMuga Nishizawa <muga@f11vm.(none)>2010-10-02 17:44:00 +0900
commit804a1cc22dbe2a8278932bd9c601b8b3f474a9cb (patch)
tree76f6c2a87e73d5f7bff28f992db371cdd7664e1a /java/src
parentcdd60e5f9cff77af78eb2b19f4a701d522e3c467 (diff)
parent7c76f073841f309056e596811ec9851beb78ba7d (diff)
downloadmsgpack-python-804a1cc22dbe2a8278932bd9c601b8b3f474a9cb.tar.gz
Merge branch 'master' of git@github.com:msgpack/msgpack
Diffstat (limited to 'java/src')
-rw-r--r--java/src/main/java/org/msgpack/Packer.java79
-rw-r--r--java/src/main/java/org/msgpack/Unpacker.java3
-rw-r--r--java/src/main/java/org/msgpack/packer/BigIntegerPacker.java41
-rw-r--r--java/src/main/java/org/msgpack/packer/BooleanPacker.java40
-rw-r--r--java/src/main/java/org/msgpack/packer/ByteArrayPacker.java40
-rw-r--r--java/src/main/java/org/msgpack/packer/BytePacker.java40
-rw-r--r--java/src/main/java/org/msgpack/packer/DoublePacker.java40
-rw-r--r--java/src/main/java/org/msgpack/packer/FloatPacker.java40
-rw-r--r--java/src/main/java/org/msgpack/packer/IntegerPacker.java40
-rw-r--r--java/src/main/java/org/msgpack/packer/LongPacker.java40
-rw-r--r--java/src/main/java/org/msgpack/packer/ShortPacker.java40
-rw-r--r--java/src/main/java/org/msgpack/packer/StringPacker.java40
-rw-r--r--java/src/main/java/org/msgpack/template/ClassTemplate.java4
13 files changed, 458 insertions, 29 deletions
diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java
index 52bd29d..704481d 100644
--- a/java/src/main/java/org/msgpack/Packer.java
+++ b/java/src/main/java/org/msgpack/Packer.java
@@ -21,12 +21,14 @@ import java.io.OutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
+import java.util.Set;
import java.util.Map;
import java.math.BigInteger;
import org.msgpack.annotation.MessagePackDelegate;
import org.msgpack.annotation.MessagePackMessage;
import org.msgpack.annotation.MessagePackOrdinalEnum;
+import org.msgpack.packer.*;
/**
* Packer enables you to serialize objects into OutputStream.
@@ -45,6 +47,22 @@ import org.msgpack.annotation.MessagePackOrdinalEnum;
* You can serialize objects that implements {@link MessagePackable} interface.
*/
public class Packer {
+ static {
+ // final classes
+ BooleanPacker.getInstance();
+ ByteArrayPacker.getInstance();
+ BytePacker.getInstance();
+ DoublePacker.getInstance();
+ FloatPacker.getInstance();
+ IntegerPacker.getInstance();
+ LongPacker.getInstance();
+ ShortPacker.getInstance();
+ StringPacker.getInstance();
+ //BigIntegerPacker.getInstance(); // BigInteger is not final
+ }
+
+ public static void load() { }
+
protected byte[] castBytes = new byte[9];
protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes);
protected OutputStream out;
@@ -433,22 +451,27 @@ public class Packer {
public Packer pack(Object o) throws IOException {
if(o == null) {
return packNil();
- } else if(o instanceof String) {
- byte[] b = ((String)o).getBytes("UTF-8");
- packRaw(b.length);
- return packRawBody(b);
+ //} else if(o instanceof String) {
+ // byte[] b = ((String)o).getBytes("UTF-8");
+ // packRaw(b.length);
+ // return packRawBody(b);
} else if(o instanceof MessagePackable) {
((MessagePackable)o).messagePack(this);
return this;
- } else if(o instanceof byte[]) {
- byte[] b = (byte[])o;
- packRaw(b.length);
- return packRawBody(b);
+ //} else if(o instanceof byte[]) {
+ // byte[] b = (byte[])o;
+ // packRaw(b.length);
+ // return packRawBody(b);
} else if(o instanceof List) {
List<Object> l = (List<Object>)o;
packArray(l.size());
for(Object i : l) { pack(i); }
return this;
+ } else if(o instanceof Set) {
+ Set<Object> l = (Set<Object>)o;
+ packArray(l.size());
+ for(Object i : l) { pack(i); }
+ return this;
} else if(o instanceof Map) {
Map<Object,Object> m = (Map<Object,Object>)o;
packMap(m.size());
@@ -457,27 +480,27 @@ public class Packer {
pack(e.getValue());
}
return this;
- } else if(o instanceof Boolean) {
- if((Boolean)o) {
- return packTrue();
- } else {
- return packFalse();
- }
- } else if(o instanceof Integer) {
- return packInt((Integer)o);
- } else if(o instanceof Long) {
- return packLong((Long)o);
- } else if(o instanceof Short) {
- return packShort((Short)o);
- } else if(o instanceof Byte) {
- return packByte((Byte)o);
- } else if(o instanceof Float) {
- return packFloat((Float)o);
- } else if(o instanceof Double) {
- return packDouble((Double)o);
+ //} else if(o instanceof Boolean) {
+ // if((Boolean)o) {
+ // return packTrue();
+ // } else {
+ // return packFalse();
+ // }
+ //} else if(o instanceof Integer) {
+ // return packInt((Integer)o);
+ //} else if(o instanceof Long) {
+ // return packLong((Long)o);
+ //} else if(o instanceof Short) {
+ // return packShort((Short)o);
+ //} else if(o instanceof Byte) {
+ // return packByte((Byte)o);
+ //} else if(o instanceof Float) {
+ // return packFloat((Float)o);
+ //} else if(o instanceof Double) {
+ // return packDouble((Double)o);
} else if(o instanceof BigInteger) {
return packBigInteger((BigInteger)o);
- }
+ }
Class<?> klass = o.getClass();
MessagePacker packer = CustomPacker.get(klass);
@@ -500,5 +523,5 @@ public class Packer {
}
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
}
-
}
+
diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java
index d91fc0e..8bbafb3 100644
--- a/java/src/main/java/org/msgpack/Unpacker.java
+++ b/java/src/main/java/org/msgpack/Unpacker.java
@@ -588,10 +588,11 @@ public class Unpacker implements Iterable<MessagePackObject> {
}
MessageUnpacker unpacker = CustomUnpacker.get(klass);
+
if(unpacker != null) {
return unpacker.unpack(this);
}
-
+
Template tmpl = null;
if (CustomMessage.isAnnotated(klass, MessagePackMessage.class)) {
tmpl = ReflectionTemplate.create(klass);
diff --git a/java/src/main/java/org/msgpack/packer/BigIntegerPacker.java b/java/src/main/java/org/msgpack/packer/BigIntegerPacker.java
new file mode 100644
index 0000000..60bbab9
--- /dev/null
+++ b/java/src/main/java/org/msgpack/packer/BigIntegerPacker.java
@@ -0,0 +1,41 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009-2010 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.packer;
+
+import java.io.IOException;
+import java.math.BigInteger;
+import org.msgpack.*;
+
+public class BigIntegerPacker implements MessagePacker {
+ private BigIntegerPacker() { }
+
+ public void pack(Packer pk, Object target) throws IOException {
+ pk.pack((BigInteger)target);
+ }
+
+ static public BigIntegerPacker getInstance() {
+ return instance;
+ }
+
+ static final BigIntegerPacker instance = new BigIntegerPacker();
+
+ static {
+ CustomMessage.registerPacker(BigInteger.class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/packer/BooleanPacker.java b/java/src/main/java/org/msgpack/packer/BooleanPacker.java
new file mode 100644
index 0000000..6f300c9
--- /dev/null
+++ b/java/src/main/java/org/msgpack/packer/BooleanPacker.java
@@ -0,0 +1,40 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009-2010 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.packer;
+
+import java.io.IOException;
+import org.msgpack.*;
+
+public class BooleanPacker implements MessagePacker {
+ private BooleanPacker() { }
+
+ public void pack(Packer pk, Object target) throws IOException {
+ pk.pack((Boolean)target);
+ }
+
+ static public BooleanPacker getInstance() {
+ return instance;
+ }
+
+ static final BooleanPacker instance = new BooleanPacker();
+
+ static {
+ CustomMessage.registerPacker(Boolean.class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/packer/ByteArrayPacker.java b/java/src/main/java/org/msgpack/packer/ByteArrayPacker.java
new file mode 100644
index 0000000..f5f38f8
--- /dev/null
+++ b/java/src/main/java/org/msgpack/packer/ByteArrayPacker.java
@@ -0,0 +1,40 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009-2010 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.packer;
+
+import java.io.IOException;
+import org.msgpack.*;
+
+public class ByteArrayPacker implements MessagePacker {
+ private ByteArrayPacker() { }
+
+ public void pack(Packer pk, Object target) throws IOException {
+ pk.pack((byte[])target);
+ }
+
+ static public ByteArrayPacker getInstance() {
+ return instance;
+ }
+
+ static final ByteArrayPacker instance = new ByteArrayPacker();
+
+ static {
+ CustomMessage.registerPacker(byte[].class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/packer/BytePacker.java b/java/src/main/java/org/msgpack/packer/BytePacker.java
new file mode 100644
index 0000000..a005f76
--- /dev/null
+++ b/java/src/main/java/org/msgpack/packer/BytePacker.java
@@ -0,0 +1,40 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009-2010 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.packer;
+
+import java.io.IOException;
+import org.msgpack.*;
+
+public class BytePacker implements MessagePacker {
+ private BytePacker() { }
+
+ public void pack(Packer pk, Object target) throws IOException {
+ pk.pack(((Byte)target).byteValue());
+ }
+
+ static public BytePacker getInstance() {
+ return instance;
+ }
+
+ static final BytePacker instance = new BytePacker();
+
+ static {
+ CustomMessage.registerPacker(Byte.class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/packer/DoublePacker.java b/java/src/main/java/org/msgpack/packer/DoublePacker.java
new file mode 100644
index 0000000..df53e33
--- /dev/null
+++ b/java/src/main/java/org/msgpack/packer/DoublePacker.java
@@ -0,0 +1,40 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009-2010 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.packer;
+
+import java.io.IOException;
+import org.msgpack.*;
+
+public class DoublePacker implements MessagePacker {
+ private DoublePacker() { }
+
+ public void pack(Packer pk, Object target) throws IOException {
+ pk.pack(((Double)target).doubleValue());
+ }
+
+ static public DoublePacker getInstance() {
+ return instance;
+ }
+
+ static final DoublePacker instance = new DoublePacker();
+
+ static {
+ CustomMessage.registerPacker(Double.class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/packer/FloatPacker.java b/java/src/main/java/org/msgpack/packer/FloatPacker.java
new file mode 100644
index 0000000..3aa47e1
--- /dev/null
+++ b/java/src/main/java/org/msgpack/packer/FloatPacker.java
@@ -0,0 +1,40 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009-2010 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.packer;
+
+import java.io.IOException;
+import org.msgpack.*;
+
+public class FloatPacker implements MessagePacker {
+ private FloatPacker() { }
+
+ public void pack(Packer pk, Object target) throws IOException {
+ pk.pack(((Float)target).floatValue());
+ }
+
+ static public FloatPacker getInstance() {
+ return instance;
+ }
+
+ static final FloatPacker instance = new FloatPacker();
+
+ static {
+ CustomMessage.registerPacker(Float.class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/packer/IntegerPacker.java b/java/src/main/java/org/msgpack/packer/IntegerPacker.java
new file mode 100644
index 0000000..1e64429
--- /dev/null
+++ b/java/src/main/java/org/msgpack/packer/IntegerPacker.java
@@ -0,0 +1,40 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009-2010 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.packer;
+
+import java.io.IOException;
+import org.msgpack.*;
+
+public class IntegerPacker implements MessagePacker {
+ private IntegerPacker() { }
+
+ public void pack(Packer pk, Object target) throws IOException {
+ pk.pack(((Integer)target).intValue());
+ }
+
+ static public IntegerPacker getInstance() {
+ return instance;
+ }
+
+ static final IntegerPacker instance = new IntegerPacker();
+
+ static {
+ CustomMessage.registerPacker(Integer.class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/packer/LongPacker.java b/java/src/main/java/org/msgpack/packer/LongPacker.java
new file mode 100644
index 0000000..146e42d
--- /dev/null
+++ b/java/src/main/java/org/msgpack/packer/LongPacker.java
@@ -0,0 +1,40 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009-2010 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.packer;
+
+import java.io.IOException;
+import org.msgpack.*;
+
+public class LongPacker implements MessagePacker {
+ private LongPacker() { }
+
+ public void pack(Packer pk, Object target) throws IOException {
+ pk.pack(((Long)target).longValue());
+ }
+
+ static public LongPacker getInstance() {
+ return instance;
+ }
+
+ static final LongPacker instance = new LongPacker();
+
+ static {
+ CustomMessage.registerPacker(Long.class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/packer/ShortPacker.java b/java/src/main/java/org/msgpack/packer/ShortPacker.java
new file mode 100644
index 0000000..e4be9ec
--- /dev/null
+++ b/java/src/main/java/org/msgpack/packer/ShortPacker.java
@@ -0,0 +1,40 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009-2010 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.packer;
+
+import java.io.IOException;
+import org.msgpack.*;
+
+public class ShortPacker implements MessagePacker {
+ private ShortPacker() { }
+
+ public void pack(Packer pk, Object target) throws IOException {
+ pk.pack(((Short)target).shortValue());
+ }
+
+ static public ShortPacker getInstance() {
+ return instance;
+ }
+
+ static final ShortPacker instance = new ShortPacker();
+
+ static {
+ CustomMessage.registerPacker(Short.class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/packer/StringPacker.java b/java/src/main/java/org/msgpack/packer/StringPacker.java
new file mode 100644
index 0000000..17a0500
--- /dev/null
+++ b/java/src/main/java/org/msgpack/packer/StringPacker.java
@@ -0,0 +1,40 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009-2010 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.packer;
+
+import java.io.IOException;
+import org.msgpack.*;
+
+public class StringPacker implements MessagePacker {
+ private StringPacker() { }
+
+ public void pack(Packer pk, Object target) throws IOException {
+ pk.pack((String)target);
+ }
+
+ static public StringPacker getInstance() {
+ return instance;
+ }
+
+ static final StringPacker instance = new StringPacker();
+
+ static {
+ CustomMessage.registerPacker(String.class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/template/ClassTemplate.java b/java/src/main/java/org/msgpack/template/ClassTemplate.java
index c529edd..9bb957c 100644
--- a/java/src/main/java/org/msgpack/template/ClassTemplate.java
+++ b/java/src/main/java/org/msgpack/template/ClassTemplate.java
@@ -21,6 +21,10 @@ import java.io.IOException;
import org.msgpack.*;
public class ClassTemplate implements Template {
+ static {
+ Templates.load();
+ }
+
private Class klass;
public ClassTemplate(Class klass) {