diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index f443f530..704481da 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -28,6 +28,7 @@ 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. @@ -46,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; @@ -434,17 +451,17 @@ 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 l = (List)o; packArray(l.size()); @@ -463,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); @@ -506,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 d91fc0ef..8bbafb33 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 { } 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 00000000..60bbab91 --- /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 00000000..6f300c95 --- /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 00000000..f5f38f88 --- /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 00000000..a005f76e --- /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 00000000..df53e332 --- /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 00000000..3aa47e19 --- /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 00000000..1e64429f --- /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 00000000..146e42dd --- /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 00000000..e4be9ec8 --- /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 00000000..17a05008 --- /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); + } +} +