From 94e5b0d78fab7bdb9dadd4b5a31a81252ae57cf2 Mon Sep 17 00:00:00 2001 From: Kazuki Oikawa Date: Sun, 17 Apr 2011 00:33:37 +0900 Subject: [PATCH] csharp: fix char pack/unpack problem on Mono --- csharp/msgpack/MsgPackWriter.cs | 5 +++++ csharp/msgpack/ObjectPacker.cs | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/csharp/msgpack/MsgPackWriter.cs b/csharp/msgpack/MsgPackWriter.cs index 78e347b9..2bd3929c 100644 --- a/csharp/msgpack/MsgPackWriter.cs +++ b/csharp/msgpack/MsgPackWriter.cs @@ -42,6 +42,11 @@ namespace msgpack } } + public void Write (char x) + { + Write ((ushort)x); + } + public void Write (uint x) { if (x < 0x10000) { diff --git a/csharp/msgpack/ObjectPacker.cs b/csharp/msgpack/ObjectPacker.cs index 0dabd21c..fb0a7bf7 100644 --- a/csharp/msgpack/ObjectPacker.cs +++ b/csharp/msgpack/ObjectPacker.cs @@ -61,7 +61,8 @@ namespace msgpack else if (t.Equals (typeof (byte))) writer.Write ((byte)o); else if (t.Equals (typeof (sbyte))) writer.Write ((sbyte)o); else if (t.Equals (typeof (short))) writer.Write ((short)o); - else if (t.Equals (typeof (ushort)) || t.Equals (typeof (char))) writer.Write ((ushort)o); + else if (t.Equals (typeof (ushort))) writer.Write ((ushort)o); + else if (t.Equals (typeof (char))) writer.Write ((ushort)(char)o); else throw new NotSupportedException (); return; } @@ -113,6 +114,26 @@ namespace msgpack return (T)Unpack (reader, typeof (T)); } + public object Unpack (Type type, byte[] buf) + { + return Unpack (type, buf, 0, buf.Length); + } + + public object Unpack (Type type, byte[] buf, int offset, int size) + { + using (MemoryStream ms = new MemoryStream (buf, offset, size)) { + return Unpack (type, ms); + } + } + + public object Unpack (Type type, Stream strm) + { + if (type.IsPrimitive) + throw new NotSupportedException (); + MsgPackReader reader = new MsgPackReader (strm); + return Unpack (reader, type); + } + object Unpack (MsgPackReader reader, Type t) { if (t.IsPrimitive) {