// readStrict async read is too slow(many Task garbage) so I don't provide async option. public static async System.Threading.Tasks.Task <T> DeserializeAsync <T>(Stream stream, IFormatterResolver resolver) { var rentBuffer = BufferPool.Default.Rent(); var buf = rentBuffer; try { int length = 0; int read; while ((read = await stream.ReadAsync(buf, length, buf.Length - length).ConfigureAwait(false)) > 0) { length += read; if (length == buf.Length) { MessagePackBinary.FastResize(ref buf, length * 2); } } return(Deserialize <T>(buf, resolver)); } finally { BufferPool.Default.Return(rentBuffer); } }
/// <summary> /// From Json String to MessagePack binary /// </summary> public static byte[] FromJson(TextReader reader) { var offset = 0; byte[] binary = null; using (var jr = new TinyJsonReader(reader, false)) { FromJsonCore(jr, ref binary, ref offset); } MessagePackBinary.FastResize(ref binary, offset); return(binary); }
public void GetEncodedStringBytes(char c, int count) { var s = new string(c, count); var bin1 = MessagePackBinary.GetEncodedStringBytes(s); var bin2 = MessagePackSerializer.Serialize(s); byte[] bin3 = null; var size = MessagePackBinary.WriteRaw(ref bin3, 0, bin1); MessagePackBinary.FastResize(ref bin3, size); MessagePack.Internal.ByteArrayComparer.Equals(bin1, 0, bin1.Length, bin2).IsTrue(); MessagePack.Internal.ByteArrayComparer.Equals(bin1, 0, bin1.Length, bin3).IsTrue(); }
static int FillFromStream(Stream input, ref byte[] buffer) { int length = 0; int read; while ((read = input.Read(buffer, length, buffer.Length - length)) > 0) { length += read; if (length == buffer.Length) { MessagePackBinary.FastResize(ref buffer, length * 2); } } return(length); }
public void WriteRaw() { // x86 for (int i = 1; i <= MessagePackRange.MaxFixStringLength; i++) { var src = Enumerable.Range(0, i).Select(x => (byte)x).ToArray(); byte[] dst = null; var len = ((typeof(UnsafeMemory32).GetMethod("WriteRaw" + i)).CreateDelegate(typeof(WriteDelegate)) as WriteDelegate).Invoke(ref dst, 0, src); len.Is(i); MessagePackBinary.FastResize(ref dst, len); MessagePack.Internal.ByteArrayComparer.Equals(src, 0, src.Length, dst).IsTrue(); } // x64 for (int i = 1; i <= MessagePackRange.MaxFixStringLength; i++) { var src = Enumerable.Range(0, i).Select(x => (byte)x).ToArray(); byte[] dst = null; var len = ((typeof(UnsafeMemory64).GetMethod("WriteRaw" + i)).CreateDelegate(typeof(WriteDelegate)) as WriteDelegate).Invoke(ref dst, 0, src); len.Is(i); MessagePackBinary.FastResize(ref dst, len); MessagePack.Internal.ByteArrayComparer.Equals(src, 0, src.Length, dst).IsTrue(); } // x86, offset for (int i = 1; i <= MessagePackRange.MaxFixStringLength; i++) { var src = Enumerable.Range(0, i).Select(x => (byte)x).ToArray(); byte[] dst = new byte[3]; var len = ((typeof(UnsafeMemory32).GetMethod("WriteRaw" + i)).CreateDelegate(typeof(WriteDelegate)) as WriteDelegate).Invoke(ref dst, 3, src); len.Is(i); dst = dst.Skip(3).Take(len).ToArray(); MessagePack.Internal.ByteArrayComparer.Equals(src, 0, src.Length, dst).IsTrue(); } // x64, offset for (int i = 1; i <= MessagePackRange.MaxFixStringLength; i++) { var src = Enumerable.Range(0, i).Select(x => (byte)x).ToArray(); byte[] dst = new byte[3]; var len = ((typeof(UnsafeMemory64).GetMethod("WriteRaw" + i)).CreateDelegate(typeof(WriteDelegate)) as WriteDelegate).Invoke(ref dst, 3, src); len.Is(i); dst = dst.Skip(3).Take(len).ToArray(); MessagePack.Internal.ByteArrayComparer.Equals(src, 0, src.Length, dst).IsTrue(); } }
public byte[] Save(IFormatterResolver resolver) { lock (memories) { var bytes = new byte[255]; // initial var offset = 0; offset += MessagePackBinary.WriteArrayHeader(ref bytes, 0, MemoryCount); foreach (var item in memories) { offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 2); offset += MessagePackBinary.WriteString(ref bytes, offset, item.Key); offset += item.Value.Serialize(ref bytes, offset, resolver); } MessagePackBinary.FastResize(ref bytes, offset); return(bytes); } }
public void Read() { void Check1 <T, T2>(T data, T2 result, Func <Stream, T2> streamRead) { const int CheckOffset = 10; byte[] bytes = null; var len = MessagePack.Resolvers.StandardResolver.Instance.GetFormatter <T>().Serialize(ref bytes, CheckOffset, data, MessagePack.Resolvers.StandardResolver.Instance); MessagePackBinary.FastResize(ref bytes, CheckOffset + len); var ms = new MemoryStream(bytes); ms.Position = CheckOffset; streamRead(ms).Is(result); } void Check2 <T>(T data, Func <Stream, T> streamRead) { const int CheckOffset = 10; byte[] bytes = null; var len = MessagePack.Resolvers.StandardResolver.Instance.GetFormatter <T>().Serialize(ref bytes, CheckOffset, data, MessagePack.Resolvers.StandardResolver.Instance); MessagePackBinary.FastResize(ref bytes, CheckOffset + len); var ms = new MemoryStream(bytes); ms.Position = CheckOffset; streamRead(ms).Is(data); } Check1(new[] { 1, 10, 100, 1000, 10000, short.MaxValue, int.MaxValue }, 7, x => MessagePackBinary.ReadArrayHeader(x)); Check1(new[] { 1, 10, 100, 1000, 10000, short.MaxValue, int.MaxValue }, (uint)7, x => MessagePackBinary.ReadArrayHeaderRaw(x)); Check1(Nil.Default, true, x => MessagePackBinary.IsNil((x))); Check2(true, x => MessagePackBinary.ReadBoolean(x)); Check2((byte)100, x => MessagePackBinary.ReadByte(x)); Check2(new byte[] { 1, 10, 100, 245 }, x => MessagePackBinary.ReadBytes(x)); Check2('あ', x => MessagePackBinary.ReadChar(x)); Check2(DateTime.UtcNow, x => MessagePackBinary.ReadDateTime(x)); Check2(132, x => MessagePackBinary.ReadInt16(x)); Check2(423, x => MessagePackBinary.ReadInt32(x)); Check2(64332, x => MessagePackBinary.ReadInt64(x)); Check2(Nil.Default, x => MessagePackBinary.ReadNil(x)); Check2(11, x => MessagePackBinary.ReadSByte(x)); Check2(10.31231f, x => MessagePackBinary.ReadSingle(x)); Check2("foobar", x => MessagePackBinary.ReadString(x)); Check2(124, x => MessagePackBinary.ReadUInt16(x)); Check2((uint)432, x => MessagePackBinary.ReadUInt32(x)); Check2((ulong)432, x => MessagePackBinary.ReadUInt64(x)); Check1(new Dictionary <int, int>() { { 1, 2 } }, 1, x => MessagePackBinary.ReadMapHeader(x)); Check1(new Dictionary <int, int>() { { 1, 2 } }, (uint)1, x => MessagePackBinary.ReadMapHeaderRaw(x)); { var block = new object[] { 1, new[] { 1, 10, 100 }, 100 }; var bytes = MessagePackSerializer.Serialize(block); var stream = new MemoryStream(bytes); MessagePackBinary.ReadNext(stream); // array(first) MessagePackBinary.ReadNext(stream); // int MessagePackBinary.ReadNextBlock(stream); // skip array MessagePackBinary.ReadInt32(stream).Is(100); } { var block = new object[] { 1, new Dictionary <int, int> { { 1, 10 }, { 111, 200 } }, 100 }; var bytes = MessagePackSerializer.Serialize(block); var stream = new MemoryStream(bytes); MessagePackBinary.ReadNext(stream); MessagePackBinary.ReadNext(stream); MessagePackBinary.ReadNextBlock(stream); MessagePackBinary.ReadInt32(stream).Is(100); } }
public void Write() { void Check(Action <Stream> streamAction, RefAction bytesAction) { const int CheckOffset = 10; var ms = new MemoryStream(); byte[] bytes = null; ms.Position = CheckOffset; streamAction(ms); var len = bytesAction(ref bytes, CheckOffset); MessagePackBinary.FastResize(ref bytes, CheckOffset + len); ms.ToArray().Is(bytes); } Check( (x) => MessagePackBinary.WriteArrayHeader(x, 999), (ref byte[] x, int y) => MessagePackBinary.WriteArrayHeader(ref x, y, 999)); Check( (x) => MessagePackBinary.WriteArrayHeaderForceArray32Block(x, 999), (ref byte[] x, int y) => MessagePackBinary.WriteArrayHeaderForceArray32Block(ref x, y, 999)); Check( (x) => MessagePackBinary.WriteBoolean(x, true), (ref byte[] x, int y) => MessagePackBinary.WriteBoolean(ref x, y, true)); Check( (x) => MessagePackBinary.WriteByte(x, (byte)100), (ref byte[] x, int y) => MessagePackBinary.WriteByte(ref x, y, (byte)100)); Check( (x) => MessagePackBinary.WriteByteForceByteBlock(x, (byte)11), (ref byte[] x, int y) => MessagePackBinary.WriteByteForceByteBlock(ref x, y, (byte)11)); Check( (x) => MessagePackBinary.WriteBytes(x, new byte[] { 1, 10, 100 }), (ref byte[] x, int y) => MessagePackBinary.WriteBytes(ref x, y, new byte[] { 1, 10, 100 })); Check( (x) => MessagePackBinary.WriteChar(x, 'z'), (ref byte[] x, int y) => MessagePackBinary.WriteChar(ref x, y, 'z')); var now = DateTime.UtcNow; Check( (x) => MessagePackBinary.WriteDateTime(x, now), (ref byte[] x, int y) => MessagePackBinary.WriteDateTime(ref x, y, now)); Check( (x) => MessagePackBinary.WriteDouble(x, 10.31231f), (ref byte[] x, int y) => MessagePackBinary.WriteDouble(ref x, y, 10.31231f)); Check( (x) => MessagePackBinary.WriteExtensionFormat(x, 10, new byte[] { 1, 10, 100 }), (ref byte[] x, int y) => MessagePackBinary.WriteExtensionFormat(ref x, y, 10, new byte[] { 1, 10, 100 })); Check( (x) => MessagePackBinary.WriteFixedArrayHeaderUnsafe(x, 'z'), (ref byte[] x, int y) => MessagePackBinary.WriteFixedArrayHeaderUnsafe(ref x, y, 'z')); Check( (x) => MessagePackBinary.WriteFixedMapHeaderUnsafe(x, 'z'), (ref byte[] x, int y) => MessagePackBinary.WriteFixedMapHeaderUnsafe(ref x, y, 'z')); Check( (x) => MessagePackBinary.WriteFixedStringUnsafe(x, "aaa", Encoding.UTF8.GetByteCount("aaa")), (ref byte[] x, int y) => MessagePackBinary.WriteFixedStringUnsafe(ref x, y, "aaa", Encoding.UTF8.GetByteCount("aaa"))); Check( (x) => MessagePackBinary.WriteInt16(x, 321), (ref byte[] x, int y) => MessagePackBinary.WriteInt16(ref x, y, 321)); Check( (x) => MessagePackBinary.WriteInt16ForceInt16Block(x, 321), (ref byte[] x, int y) => MessagePackBinary.WriteInt16ForceInt16Block(ref x, y, 321)); Check( (x) => MessagePackBinary.WriteInt32(x, 321), (ref byte[] x, int y) => MessagePackBinary.WriteInt32(ref x, y, 321)); Check( (x) => MessagePackBinary.WriteInt32ForceInt32Block(x, 321), (ref byte[] x, int y) => MessagePackBinary.WriteInt32ForceInt32Block(ref x, y, 321)); Check( (x) => MessagePackBinary.WriteInt64(x, 321), (ref byte[] x, int y) => MessagePackBinary.WriteInt64(ref x, y, 321)); Check( (x) => MessagePackBinary.WriteInt64ForceInt64Block(x, 321), (ref byte[] x, int y) => MessagePackBinary.WriteInt64ForceInt64Block(ref x, y, 321)); Check( (x) => MessagePackBinary.WriteMapHeader(x, 321), (ref byte[] x, int y) => MessagePackBinary.WriteMapHeader(ref x, y, 321)); Check( (x) => MessagePackBinary.WriteMapHeaderForceMap32Block(x, 321), (ref byte[] x, int y) => MessagePackBinary.WriteMapHeaderForceMap32Block(ref x, y, 321)); Check( (x) => MessagePackBinary.WriteNil(x), (ref byte[] x, int y) => MessagePackBinary.WriteNil(ref x, y)); Check( (x) => MessagePackBinary.WritePositiveFixedIntUnsafe(x, 12), (ref byte[] x, int y) => MessagePackBinary.WritePositiveFixedIntUnsafe(ref x, y, 12)); Check( (x) => MessagePackBinary.WriteSByte(x, 12), (ref byte[] x, int y) => MessagePackBinary.WriteSByte(ref x, y, 12)); Check( (x) => MessagePackBinary.WriteSByteForceSByteBlock(x, 12), (ref byte[] x, int y) => MessagePackBinary.WriteSByteForceSByteBlock(ref x, y, 12)); Check( (x) => MessagePackBinary.WriteSingle(x, 123), (ref byte[] x, int y) => MessagePackBinary.WriteSingle(ref x, y, 123)); Check( (x) => MessagePackBinary.WriteString(x, "aaa"), (ref byte[] x, int y) => MessagePackBinary.WriteString(ref x, y, "aaa")); Check( (x) => MessagePackBinary.WriteStringBytes(x, new byte[] { 1, 10 }), (ref byte[] x, int y) => MessagePackBinary.WriteStringBytes(ref x, y, new byte[] { 1, 10 })); Check( (x) => MessagePackBinary.WriteStringForceStr32Block(x, "zzz"), (ref byte[] x, int y) => MessagePackBinary.WriteStringForceStr32Block(ref x, y, "zzz")); Check( (x) => MessagePackBinary.WriteStringUnsafe(x, "zzz", Encoding.UTF8.GetByteCount("zzz")), (ref byte[] x, int y) => MessagePackBinary.WriteStringUnsafe(ref x, y, "zzz", Encoding.UTF8.GetByteCount("zzz"))); Check( (x) => MessagePackBinary.WriteUInt16(x, 31), (ref byte[] x, int y) => MessagePackBinary.WriteUInt16(ref x, y, 31)); Check( (x) => MessagePackBinary.WriteUInt16ForceUInt16Block(x, 32), (ref byte[] x, int y) => MessagePackBinary.WriteUInt16ForceUInt16Block(ref x, y, 32)); Check( (x) => MessagePackBinary.WriteUInt32(x, 11), (ref byte[] x, int y) => MessagePackBinary.WriteUInt32(ref x, y, 11)); Check( (x) => MessagePackBinary.WriteUInt32ForceUInt32Block(x, 11), (ref byte[] x, int y) => MessagePackBinary.WriteUInt32ForceUInt32Block(ref x, y, 11)); Check( (x) => MessagePackBinary.WriteUInt64(x, 11), (ref byte[] x, int y) => MessagePackBinary.WriteUInt64(ref x, y, 11)); Check( (x) => MessagePackBinary.WriteUInt64ForceUInt64Block(x, 11), (ref byte[] x, int y) => MessagePackBinary.WriteUInt64ForceUInt64Block(ref x, y, 11)); }