From e3bf8a404b993da62676d1d435e49a37c404fe3b Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 24 Oct 2010 19:32:45 +0900 Subject: java: adds MessagePack class --- java/src/main/java/org/msgpack/MessagePack.java | 123 +++++++++++++++++++++ java/src/main/java/org/msgpack/Unpacker.java | 5 +- .../org/msgpack/TestMessagePackStaticMethods.java | 121 ++++++++++++++++++++ 3 files changed, 247 insertions(+), 2 deletions(-) create mode 100644 java/src/main/java/org/msgpack/MessagePack.java create mode 100644 java/src/test/java/org/msgpack/TestMessagePackStaticMethods.java (limited to 'java') diff --git a/java/src/main/java/org/msgpack/MessagePack.java b/java/src/main/java/org/msgpack/MessagePack.java new file mode 100644 index 0000000..f9dcce8 --- /dev/null +++ b/java/src/main/java/org/msgpack/MessagePack.java @@ -0,0 +1,123 @@ +// +// 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; + +import java.io.OutputStream; +import java.io.InputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +public class MessagePack { + public static byte[] pack(Object obj) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try { + new Packer(out).pack(obj); + } catch (IOException e) { + throw new RuntimeException(e); + } + return out.toByteArray(); + } + + public static void pack(OutputStream out, Object obj) throws IOException { + new Packer(out).pack(obj); + } + + public static byte[] pack(Object obj, Template tmpl) throws MessageTypeException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try { + new Packer(out).pack(obj, tmpl); + } catch (IOException e) { + throw new RuntimeException(e); + } + return out.toByteArray(); + } + + public static void pack(OutputStream out, Object obj, Template tmpl) throws IOException { + new Packer(out).pack(obj, tmpl); + } + + + public static MessagePackObject unpack(byte[] buffer) throws IOException { + Unpacker pac = new Unpacker(); + pac.wrap(buffer); + try { + return pac.unpackObject(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static Object unpack(byte[] buffer, Template tmpl) throws MessageTypeException { + Unpacker pac = new Unpacker(); + pac.wrap(buffer); + try { + return pac.unpack(tmpl); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static T unpack(byte[] buffer, Class klass) throws MessageTypeException { + Unpacker pac = new Unpacker(); + pac.wrap(buffer); + try { + return pac.unpack(klass); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static MessagePackObject unpack(InputStream in) { + Unpacker pac = new Unpacker(in); + try { + return pac.unpackObject(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static Object unpack(InputStream in, Template tmpl) throws IOException, MessageTypeException { + Unpacker pac = new Unpacker(in); + try { + return pac.unpack(tmpl); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static T unpack(InputStream in, Class klass) throws IOException, MessageTypeException { + Unpacker pac = new Unpacker(in); + try { + return pac.unpack(klass); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + //public static void register(Class target); // TODO: auto-detect + + //public static void register(Class target, Template tmpl); // TODO + + //public static void registerPacker(Class target, MessagePacker packer); // TODO + + //public static void registerConverter(Class target, MessageConverter converter); // TODO + + //public static void registerUnpacker(Class target, MessageUnpacker unpacker); // TODO +} + diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index e458b62..d07de1e 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -580,8 +580,9 @@ public class Unpacker implements Iterable { return tmpl.unpack(this); } - final public Object unpack(Class klass) throws IOException, MessageTypeException, InstantiationException, IllegalAccessException { - return unpack(Templates.tClass(klass)); + final public T unpack(Class klass) throws IOException, MessageTypeException { + // FIXME optional? + return (T)unpack(Templates.tOptional(Templates.tClass(klass))); } } diff --git a/java/src/test/java/org/msgpack/TestMessagePackStaticMethods.java b/java/src/test/java/org/msgpack/TestMessagePackStaticMethods.java new file mode 100644 index 0000000..dea2a37 --- /dev/null +++ b/java/src/test/java/org/msgpack/TestMessagePackStaticMethods.java @@ -0,0 +1,121 @@ +package org.msgpack; + +import org.msgpack.*; +import org.msgpack.object.*; +import static org.msgpack.Templates.*; + +import java.io.*; +import java.util.*; +import java.math.BigInteger; + +import org.junit.Test; +import junit.framework.TestCase; + +public class TestMessagePackStaticMethods extends TestCase { + @Test + public void testPackToByteArray() throws Exception { + byte[] a = MessagePack.pack("msgpack"); + byte[] b = MessagePack.pack((Object)1); + byte[] c = MessagePack.pack((Object)null); + + { + MessagePackObject aobj = MessagePack.unpack(a); + MessagePackObject bobj = MessagePack.unpack(b); + MessagePackObject cobj = MessagePack.unpack(c); + + assertEquals(aobj, RawType.create("msgpack")); + assertEquals(bobj, IntegerType.create(1)); + assertEquals(cobj, NilType.create()); + } + } + + @Test + public void testPackToStream() throws Exception { + ByteArrayOutputStream aout = new ByteArrayOutputStream(); + MessagePack.pack(aout, "msgpack"); + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + MessagePack.pack(bout, (Object)1); + ByteArrayOutputStream cout = new ByteArrayOutputStream(); + MessagePack.pack(cout, (Object)null); + + { + InputStream ain = new ByteArrayInputStream(aout.toByteArray()); + MessagePackObject aobj = MessagePack.unpack(ain); + InputStream bin = new ByteArrayInputStream(bout.toByteArray()); + MessagePackObject bobj = MessagePack.unpack(bin); + InputStream cin = new ByteArrayInputStream(cout.toByteArray()); + MessagePackObject cobj = MessagePack.unpack(cin); + + assertEquals(aobj, RawType.create("msgpack")); + assertEquals(bobj, IntegerType.create(1)); + assertEquals(cobj, NilType.create()); + } + } + + @Test + public void testCheckedPackToByteArray() throws Exception { + byte[] a = MessagePack.pack("msgpack", TString); + byte[] b = MessagePack.pack((Object)1, TInteger); + byte[] c = MessagePack.pack((Object)null, TAny); + + { + Object aobj = MessagePack.unpack(a, TString); + Object bobj = MessagePack.unpack(b, TInteger); + Object cobj_any = MessagePack.unpack(c, TAny); + Object cobj_obj = MessagePack.unpack(c, tOptional(TAny)); + assertEquals(aobj, "msgpack"); + assertEquals(bobj, 1); + assertEquals(cobj_any, NilType.create()); + assertEquals(cobj_obj, null); + } + + { + String aobj = MessagePack.unpack(a, String.class); + Integer bobj = MessagePack.unpack(b, Integer.class); + Object cobj = MessagePack.unpack(c, Object.class); + assertEquals(aobj, "msgpack"); + assertEquals(bobj, (Integer)1); + assertEquals(cobj, null); + } + } + + @Test + public void testCheckedPackToStream() throws Exception { + ByteArrayOutputStream aout = new ByteArrayOutputStream(); + MessagePack.pack(aout, "msgpack"); + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + MessagePack.pack(bout, (Object)1); + ByteArrayOutputStream cout = new ByteArrayOutputStream(); + MessagePack.pack(cout, (Object)null); + + { + InputStream ain = new ByteArrayInputStream(aout.toByteArray()); + Object aobj = MessagePack.unpack(ain, TString); + InputStream bin = new ByteArrayInputStream(bout.toByteArray()); + Object bobj = MessagePack.unpack(bin, TInteger); + InputStream cin_any = new ByteArrayInputStream(cout.toByteArray()); + Object cobj_any = MessagePack.unpack(cin_any, TAny); + InputStream cin_obj = new ByteArrayInputStream(cout.toByteArray()); + Object cobj_obj = MessagePack.unpack(cin_obj, tOptional(TAny)); + + assertEquals(aobj, "msgpack"); + assertEquals(bobj, 1); + assertEquals(cobj_any, NilType.create()); + assertEquals(cobj_obj, null); + } + + { + InputStream ain = new ByteArrayInputStream(aout.toByteArray()); + String aobj = MessagePack.unpack(ain, String.class); + InputStream bin = new ByteArrayInputStream(bout.toByteArray()); + Integer bobj = MessagePack.unpack(bin, Integer.class); + InputStream cin = new ByteArrayInputStream(cout.toByteArray()); + Object cobj = MessagePack.unpack(cin, Object.class); + + assertEquals(aobj, "msgpack"); + assertEquals(bobj, (Integer)1); + assertEquals(cobj, null); + } + } +} + -- cgit v1.2.1