Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/aspnet/MessagePack-CSharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/MessagePack/MessagePackBinary.cs')
-rw-r--r--src/MessagePack/MessagePackBinary.cs3956
1 files changed, 1308 insertions, 2648 deletions
diff --git a/src/MessagePack/MessagePackBinary.cs b/src/MessagePack/MessagePackBinary.cs
index 2352f450..24f34d88 100644
--- a/src/MessagePack/MessagePackBinary.cs
+++ b/src/MessagePack/MessagePackBinary.cs
@@ -1,7 +1,9 @@
-using MessagePack.Decoders;
-using MessagePack.Internal;
-using System;
+using System;
+using System.Buffers;
using System.IO;
+using System.Runtime.InteropServices;
+using MessagePack.Decoders;
+using MessagePack.Internal;
namespace MessagePack
{
@@ -11,30 +13,30 @@ namespace MessagePack
/// </summary>
public static partial class MessagePackBinary
{
- const int MaxSize = 256; // [0] ~ [255]
- const int ArrayMaxSize = 0x7FFFFFC7; // https://msdn.microsoft.com/en-us/library/system.array
-
- static readonly IMapHeaderDecoder[] mapHeaderDecoders = new IMapHeaderDecoder[MaxSize];
- static readonly IArrayHeaderDecoder[] arrayHeaderDecoders = new IArrayHeaderDecoder[MaxSize];
- static readonly IBooleanDecoder[] booleanDecoders = new IBooleanDecoder[MaxSize];
- static readonly IByteDecoder[] byteDecoders = new IByteDecoder[MaxSize];
- static readonly IBytesDecoder[] bytesDecoders = new IBytesDecoder[MaxSize];
- static readonly IBytesSegmentDecoder[] bytesSegmentDecoders = new IBytesSegmentDecoder[MaxSize];
- static readonly ISByteDecoder[] sbyteDecoders = new ISByteDecoder[MaxSize];
- static readonly ISingleDecoder[] singleDecoders = new ISingleDecoder[MaxSize];
- static readonly IDoubleDecoder[] doubleDecoders = new IDoubleDecoder[MaxSize];
- static readonly IInt16Decoder[] int16Decoders = new IInt16Decoder[MaxSize];
- static readonly IInt32Decoder[] int32Decoders = new IInt32Decoder[MaxSize];
- static readonly IInt64Decoder[] int64Decoders = new IInt64Decoder[MaxSize];
- static readonly IUInt16Decoder[] uint16Decoders = new IUInt16Decoder[MaxSize];
- static readonly IUInt32Decoder[] uint32Decoders = new IUInt32Decoder[MaxSize];
- static readonly IUInt64Decoder[] uint64Decoders = new IUInt64Decoder[MaxSize];
- static readonly IStringDecoder[] stringDecoders = new IStringDecoder[MaxSize];
- static readonly IStringSegmentDecoder[] stringSegmentDecoders = new IStringSegmentDecoder[MaxSize];
- static readonly IExtDecoder[] extDecoders = new IExtDecoder[MaxSize];
- static readonly IExtHeaderDecoder[] extHeaderDecoders = new IExtHeaderDecoder[MaxSize];
- static readonly IDateTimeDecoder[] dateTimeDecoders = new IDateTimeDecoder[MaxSize];
- static readonly IReadNextDecoder[] readNextDecoders = new IReadNextDecoder[MaxSize];
+ private const int MaxSize = 256; // [0] ~ [255]
+ private const int ArrayMaxSize = 0x7FFFFFC7; // https://msdn.microsoft.com/en-us/library/system.array
+
+ private static readonly IMapHeaderDecoder[] mapHeaderDecoders = new IMapHeaderDecoder[MaxSize];
+ private static readonly IArrayHeaderDecoder[] arrayHeaderDecoders = new IArrayHeaderDecoder[MaxSize];
+ private static readonly IBooleanDecoder[] booleanDecoders = new IBooleanDecoder[MaxSize];
+ private static readonly IByteDecoder[] byteDecoders = new IByteDecoder[MaxSize];
+ private static readonly IBytesDecoder[] bytesDecoders = new IBytesDecoder[MaxSize];
+ private static readonly IBytesSegmentDecoder[] bytesSegmentDecoders = new IBytesSegmentDecoder[MaxSize];
+ private static readonly ISByteDecoder[] sbyteDecoders = new ISByteDecoder[MaxSize];
+ private static readonly ISingleDecoder[] singleDecoders = new ISingleDecoder[MaxSize];
+ private static readonly IDoubleDecoder[] doubleDecoders = new IDoubleDecoder[MaxSize];
+ private static readonly IInt16Decoder[] int16Decoders = new IInt16Decoder[MaxSize];
+ private static readonly IInt32Decoder[] int32Decoders = new IInt32Decoder[MaxSize];
+ private static readonly IInt64Decoder[] int64Decoders = new IInt64Decoder[MaxSize];
+ private static readonly IUInt16Decoder[] uint16Decoders = new IUInt16Decoder[MaxSize];
+ private static readonly IUInt32Decoder[] uint32Decoders = new IUInt32Decoder[MaxSize];
+ private static readonly IUInt64Decoder[] uint64Decoders = new IUInt64Decoder[MaxSize];
+ private static readonly IStringDecoder[] stringDecoders = new IStringDecoder[MaxSize];
+ private static readonly IStringSegmentDecoder[] stringSegmentDecoders = new IStringSegmentDecoder[MaxSize];
+ private static readonly IExtDecoder[] extDecoders = new IExtDecoder[MaxSize];
+ private static readonly IExtHeaderDecoder[] extHeaderDecoders = new IExtHeaderDecoder[MaxSize];
+ private static readonly IDateTimeDecoder[] dateTimeDecoders = new IDateTimeDecoder[MaxSize];
+ private static readonly IReadNextDecoder[] readNextDecoders = new IReadNextDecoder[MaxSize];
static MessagePackBinary()
{
@@ -251,115 +253,25 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static void EnsureCapacity(ref byte[] bytes, int offset, int appendLength)
+ public static MessagePackType GetMessagePackType(ReadOnlySequence<byte> byteSequence)
{
- var newLength = offset + appendLength;
-
- // If null(most case fisrt time) fill byte.
- if (bytes == null)
- {
- bytes = new byte[newLength];
- return;
- }
-
- // like MemoryStream.EnsureCapacity
- var current = bytes.Length;
- if (newLength > current)
- {
- int num = newLength;
- if (num < 256)
- {
- num = 256;
- FastResize(ref bytes, num);
- return;
- }
-
- if (current == ArrayMaxSize)
- {
- throw new InvalidOperationException("byte[] size reached maximum size of array(0x7FFFFFC7), can not write to single byte[]. Details: https://msdn.microsoft.com/en-us/library/system.array");
- }
-
- var newSize = unchecked((current * 2));
- if (newSize < 0) // overflow
- {
- num = ArrayMaxSize;
- }
- else
- {
- if (num < newSize)
- {
- num = newSize;
- }
- }
-
- FastResize(ref bytes, num);
- }
+ return MessagePackCode.ToMessagePackType(byteSequence.First.Span[0]);
}
- // Buffer.BlockCopy version of Array.Resize
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static void FastResize(ref byte[] array, int newSize)
+ public static void ReadNext(ref ReadOnlySequence<byte> byteSequence)
{
- if (newSize < 0) throw new ArgumentOutOfRangeException("newSize");
-
- byte[] array2 = array;
- if (array2 == null)
- {
- array = new byte[newSize];
- return;
- }
-
- if (array2.Length != newSize)
- {
- byte[] array3 = new byte[newSize];
- Buffer.BlockCopy(array2, 0, array3, 0, (array2.Length > newSize) ? newSize : array2.Length);
- array = array3;
- }
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static byte[] FastCloneWithResize(byte[] array, int newSize)
- {
- if (newSize < 0) throw new ArgumentOutOfRangeException("newSize");
-
- byte[] array2 = array;
- if (array2 == null)
- {
- array = new byte[newSize];
- return array;
- }
-
- byte[] array3 = new byte[newSize];
- Buffer.BlockCopy(array2, 0, array3, 0, (array2.Length > newSize) ? newSize : array2.Length);
- return array3;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static MessagePackType GetMessagePackType(byte[] bytes, int offset)
- {
- return MessagePackCode.ToMessagePackType(bytes[offset]);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int ReadNext(byte[] bytes, int offset)
- {
- return readNextDecoders[bytes[offset]].Read(bytes, offset);
+ readNextDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int ReadNextBlock(byte[] bytes, int offset)
+ public static void ReadNextBlock(ref ReadOnlySequence<byte> byteSequence)
{
- switch (GetMessagePackType(bytes, offset))
+ switch (GetMessagePackType(byteSequence))
{
case MessagePackType.Unknown:
case MessagePackType.Integer:
@@ -370,31 +282,28 @@ namespace MessagePack
case MessagePackType.Binary:
case MessagePackType.Extension:
default:
- return ReadNext(bytes, offset);
+ ReadNext(ref byteSequence);
+ break;
case MessagePackType.Array:
{
- var startOffset = offset;
- int readSize;
- var header = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
- offset += readSize;
+ var header = MessagePackBinary.ReadArrayHeader(ref byteSequence);
for (int i = 0; i < header; i++)
{
- offset += ReadNextBlock(bytes, offset);
+ ReadNextBlock(ref byteSequence);
}
- return offset - startOffset;
+
+ break;
}
case MessagePackType.Map:
{
- var startOffset = offset;
- int readSize;
- var header = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
- offset += readSize;
+ var header = MessagePackBinary.ReadMapHeader(ref byteSequence);
for (int i = 0; i < header; i++)
{
- offset += ReadNextBlock(bytes, offset); // read key block
- offset += ReadNextBlock(bytes, offset); // read value block
+ ReadNextBlock(ref byteSequence); // read key block
+ ReadNextBlock(ref byteSequence); // read value block
}
- return offset - startOffset;
+
+ break;
}
}
}
@@ -402,251 +311,42 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteNil(ref byte[] bytes, int offset)
+ public static void WriteNil(IBufferWriter<byte> writer)
{
- EnsureCapacity(ref bytes, offset, 1);
-
- bytes[offset] = MessagePackCode.Nil;
- return 1;
+ var span = writer.GetSpan(1);
+ span[0] = MessagePackCode.Nil;
+ writer.Advance(1);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static Nil ReadNil(byte[] bytes, int offset, out int readSize)
+ public static Nil ReadNil(ref ReadOnlySequence<byte> byteSequence)
{
- if (bytes[offset] == MessagePackCode.Nil)
+ if (byteSequence.First.Span[0] == MessagePackCode.Nil)
{
- readSize = 1;
+ byteSequence = byteSequence.Slice(1);
return Nil.Default;
}
else
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static bool IsNil(byte[] bytes, int offset)
+ public static bool IsNil(ReadOnlySequence<byte> byteSequence)
{
- return bytes[offset] == MessagePackCode.Nil;
+ return byteSequence.First.Span[0] == MessagePackCode.Nil;
}
- public static int WriteRaw(ref byte[] bytes, int offset, byte[] rawMessagePackBlock)
+ public static void WriteRaw(IBufferWriter<byte> writer, ReadOnlySpan<byte> rawMessagePackBlock)
{
- EnsureCapacity(ref bytes, offset, rawMessagePackBlock.Length);
-
-#if NETSTANDARD || NETFRAMEWORK
- if (UnsafeMemory.Is32Bit)
- {
- switch (rawMessagePackBlock.Length)
- {
- case 1:
- UnsafeMemory32.WriteRaw1(ref bytes, offset, rawMessagePackBlock);
- break;
- case 2:
- UnsafeMemory32.WriteRaw2(ref bytes, offset, rawMessagePackBlock);
- break;
- case 3:
- UnsafeMemory32.WriteRaw3(ref bytes, offset, rawMessagePackBlock);
- break;
- case 4:
- UnsafeMemory32.WriteRaw4(ref bytes, offset, rawMessagePackBlock);
- break;
- case 5:
- UnsafeMemory32.WriteRaw5(ref bytes, offset, rawMessagePackBlock);
- break;
- case 6:
- UnsafeMemory32.WriteRaw6(ref bytes, offset, rawMessagePackBlock);
- break;
- case 7:
- UnsafeMemory32.WriteRaw7(ref bytes, offset, rawMessagePackBlock);
- break;
- case 8:
- UnsafeMemory32.WriteRaw8(ref bytes, offset, rawMessagePackBlock);
- break;
- case 9:
- UnsafeMemory32.WriteRaw9(ref bytes, offset, rawMessagePackBlock);
- break;
- case 10:
- UnsafeMemory32.WriteRaw10(ref bytes, offset, rawMessagePackBlock);
- break;
- case 11:
- UnsafeMemory32.WriteRaw11(ref bytes, offset, rawMessagePackBlock);
- break;
- case 12:
- UnsafeMemory32.WriteRaw12(ref bytes, offset, rawMessagePackBlock);
- break;
- case 13:
- UnsafeMemory32.WriteRaw13(ref bytes, offset, rawMessagePackBlock);
- break;
- case 14:
- UnsafeMemory32.WriteRaw14(ref bytes, offset, rawMessagePackBlock);
- break;
- case 15:
- UnsafeMemory32.WriteRaw15(ref bytes, offset, rawMessagePackBlock);
- break;
- case 16:
- UnsafeMemory32.WriteRaw16(ref bytes, offset, rawMessagePackBlock);
- break;
- case 17:
- UnsafeMemory32.WriteRaw17(ref bytes, offset, rawMessagePackBlock);
- break;
- case 18:
- UnsafeMemory32.WriteRaw18(ref bytes, offset, rawMessagePackBlock);
- break;
- case 19:
- UnsafeMemory32.WriteRaw19(ref bytes, offset, rawMessagePackBlock);
- break;
- case 20:
- UnsafeMemory32.WriteRaw20(ref bytes, offset, rawMessagePackBlock);
- break;
- case 21:
- UnsafeMemory32.WriteRaw21(ref bytes, offset, rawMessagePackBlock);
- break;
- case 22:
- UnsafeMemory32.WriteRaw22(ref bytes, offset, rawMessagePackBlock);
- break;
- case 23:
- UnsafeMemory32.WriteRaw23(ref bytes, offset, rawMessagePackBlock);
- break;
- case 24:
- UnsafeMemory32.WriteRaw24(ref bytes, offset, rawMessagePackBlock);
- break;
- case 25:
- UnsafeMemory32.WriteRaw25(ref bytes, offset, rawMessagePackBlock);
- break;
- case 26:
- UnsafeMemory32.WriteRaw26(ref bytes, offset, rawMessagePackBlock);
- break;
- case 27:
- UnsafeMemory32.WriteRaw27(ref bytes, offset, rawMessagePackBlock);
- break;
- case 28:
- UnsafeMemory32.WriteRaw28(ref bytes, offset, rawMessagePackBlock);
- break;
- case 29:
- UnsafeMemory32.WriteRaw29(ref bytes, offset, rawMessagePackBlock);
- break;
- case 30:
- UnsafeMemory32.WriteRaw30(ref bytes, offset, rawMessagePackBlock);
- break;
- case 31:
- UnsafeMemory32.WriteRaw31(ref bytes, offset, rawMessagePackBlock);
- break;
- default:
- Buffer.BlockCopy(rawMessagePackBlock, 0, bytes, offset, rawMessagePackBlock.Length);
- break;
- }
- }
- else
- {
- switch (rawMessagePackBlock.Length)
- {
- case 1:
- UnsafeMemory64.WriteRaw1(ref bytes, offset, rawMessagePackBlock);
- break;
- case 2:
- UnsafeMemory64.WriteRaw2(ref bytes, offset, rawMessagePackBlock);
- break;
- case 3:
- UnsafeMemory64.WriteRaw3(ref bytes, offset, rawMessagePackBlock);
- break;
- case 4:
- UnsafeMemory64.WriteRaw4(ref bytes, offset, rawMessagePackBlock);
- break;
- case 5:
- UnsafeMemory64.WriteRaw5(ref bytes, offset, rawMessagePackBlock);
- break;
- case 6:
- UnsafeMemory64.WriteRaw6(ref bytes, offset, rawMessagePackBlock);
- break;
- case 7:
- UnsafeMemory64.WriteRaw7(ref bytes, offset, rawMessagePackBlock);
- break;
- case 8:
- UnsafeMemory64.WriteRaw8(ref bytes, offset, rawMessagePackBlock);
- break;
- case 9:
- UnsafeMemory64.WriteRaw9(ref bytes, offset, rawMessagePackBlock);
- break;
- case 10:
- UnsafeMemory64.WriteRaw10(ref bytes, offset, rawMessagePackBlock);
- break;
- case 11:
- UnsafeMemory64.WriteRaw11(ref bytes, offset, rawMessagePackBlock);
- break;
- case 12:
- UnsafeMemory64.WriteRaw12(ref bytes, offset, rawMessagePackBlock);
- break;
- case 13:
- UnsafeMemory64.WriteRaw13(ref bytes, offset, rawMessagePackBlock);
- break;
- case 14:
- UnsafeMemory64.WriteRaw14(ref bytes, offset, rawMessagePackBlock);
- break;
- case 15:
- UnsafeMemory64.WriteRaw15(ref bytes, offset, rawMessagePackBlock);
- break;
- case 16:
- UnsafeMemory64.WriteRaw16(ref bytes, offset, rawMessagePackBlock);
- break;
- case 17:
- UnsafeMemory64.WriteRaw17(ref bytes, offset, rawMessagePackBlock);
- break;
- case 18:
- UnsafeMemory64.WriteRaw18(ref bytes, offset, rawMessagePackBlock);
- break;
- case 19:
- UnsafeMemory64.WriteRaw19(ref bytes, offset, rawMessagePackBlock);
- break;
- case 20:
- UnsafeMemory64.WriteRaw20(ref bytes, offset, rawMessagePackBlock);
- break;
- case 21:
- UnsafeMemory64.WriteRaw21(ref bytes, offset, rawMessagePackBlock);
- break;
- case 22:
- UnsafeMemory64.WriteRaw22(ref bytes, offset, rawMessagePackBlock);
- break;
- case 23:
- UnsafeMemory64.WriteRaw23(ref bytes, offset, rawMessagePackBlock);
- break;
- case 24:
- UnsafeMemory64.WriteRaw24(ref bytes, offset, rawMessagePackBlock);
- break;
- case 25:
- UnsafeMemory64.WriteRaw25(ref bytes, offset, rawMessagePackBlock);
- break;
- case 26:
- UnsafeMemory64.WriteRaw26(ref bytes, offset, rawMessagePackBlock);
- break;
- case 27:
- UnsafeMemory64.WriteRaw27(ref bytes, offset, rawMessagePackBlock);
- break;
- case 28:
- UnsafeMemory64.WriteRaw28(ref bytes, offset, rawMessagePackBlock);
- break;
- case 29:
- UnsafeMemory64.WriteRaw29(ref bytes, offset, rawMessagePackBlock);
- break;
- case 30:
- UnsafeMemory64.WriteRaw30(ref bytes, offset, rawMessagePackBlock);
- break;
- case 31:
- UnsafeMemory64.WriteRaw31(ref bytes, offset, rawMessagePackBlock);
- break;
- default:
- Buffer.BlockCopy(rawMessagePackBlock, 0, bytes, offset, rawMessagePackBlock.Length);
- break;
- }
- }
-#else
- Buffer.BlockCopy(rawMessagePackBlock, 0, bytes, offset, rawMessagePackBlock.Length);
-#endif
- return rawMessagePackBlock.Length;
+ var span = writer.GetSpan(rawMessagePackBlock.Length);
+ rawMessagePackBlock.CopyTo(span);
+ writer.Advance(span.Length);
}
/// <summary>
@@ -656,10 +356,10 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteFixedMapHeaderUnsafe(ref byte[] bytes, int offset, int count)
+ public static int WriteFixedMapHeaderUnsafe(IBufferWriter<byte> writer, int count)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = (byte)(MessagePackCode.MinFixMap | count);
+ var span = writer.GetSpan(1);
+ span[0] = (byte)(MessagePackCode.MinFixMap | count);
return 1;
}
@@ -669,11 +369,11 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteMapHeader(ref byte[] bytes, int offset, int count)
+ public static void WriteMapHeader(IBufferWriter<byte> writer, int count)
{
checked
{
- return WriteMapHeader(ref bytes, offset, (uint)count);
+ WriteMapHeader(writer, (uint)count);
}
}
@@ -683,37 +383,37 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteMapHeader(ref byte[] bytes, int offset, uint count)
+ public static void WriteMapHeader(IBufferWriter<byte> writer, uint count)
{
if (count <= MessagePackRange.MaxFixMapCount)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = (byte)(MessagePackCode.MinFixMap | count);
- return 1;
+ var span = writer.GetSpan(1);
+ span[0] = (byte)(MessagePackCode.MinFixMap | count);
+ writer.Advance(1);
}
else if (count <= ushort.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 3);
+ var span = writer.GetSpan(3);
unchecked
{
- bytes[offset] = MessagePackCode.Map16;
- bytes[offset + 1] = (byte)(count >> 8);
- bytes[offset + 2] = (byte)(count);
+ span[0] = MessagePackCode.Map16;
+ span[1] = (byte)(count >> 8);
+ span[2] = (byte)(count);
}
- return 3;
+ writer.Advance(3);
}
else
{
- EnsureCapacity(ref bytes, offset, 5);
+ var span = writer.GetSpan(5);
unchecked
{
- bytes[offset] = MessagePackCode.Map32;
- bytes[offset + 1] = (byte)(count >> 24);
- bytes[offset + 2] = (byte)(count >> 16);
- bytes[offset + 3] = (byte)(count >> 8);
- bytes[offset + 4] = (byte)(count);
+ span[0] = MessagePackCode.Map32;
+ span[1] = (byte)(count >> 24);
+ span[2] = (byte)(count >> 16);
+ span[3] = (byte)(count >> 8);
+ span[4] = (byte)(count);
}
- return 5;
+ writer.Advance(5);
}
}
@@ -723,18 +423,18 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteMapHeaderForceMap32Block(ref byte[] bytes, int offset, uint count)
+ public static void WriteMapHeaderForceMap32Block(IBufferWriter<byte> writer, uint count)
{
- EnsureCapacity(ref bytes, offset, 5);
+ var span = writer.GetSpan(5);
unchecked
{
- bytes[offset] = MessagePackCode.Map32;
- bytes[offset + 1] = (byte)(count >> 24);
- bytes[offset + 2] = (byte)(count >> 16);
- bytes[offset + 3] = (byte)(count >> 8);
- bytes[offset + 4] = (byte)(count);
+ span[0] = MessagePackCode.Map32;
+ span[1] = (byte)(count >> 24);
+ span[2] = (byte)(count >> 16);
+ span[3] = (byte)(count >> 8);
+ span[4] = (byte)(count);
}
- return 5;
+ writer.Advance(5);
}
/// <summary>
@@ -743,11 +443,11 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int ReadMapHeader(byte[] bytes, int offset, out int readSize)
+ public static int ReadMapHeader(ref ReadOnlySequence<byte> byteSequence)
{
checked
{
- return (int)mapHeaderDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return (int)mapHeaderDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
}
@@ -757,9 +457,9 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static uint ReadMapHeaderRaw(byte[] bytes, int offset, out int readSize)
+ public static uint ReadMapHeaderRaw(ref ReadOnlySequence<byte> byteSequence)
{
- return mapHeaderDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return mapHeaderDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
@@ -788,11 +488,11 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteFixedArrayHeaderUnsafe(ref byte[] bytes, int offset, int count)
+ public static void WriteFixedArrayHeaderUnsafe(IBufferWriter<byte> writer, int count)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = (byte)(MessagePackCode.MinFixArray | count);
- return 1;
+ var span = writer.GetSpan(1);
+ span[0] = (byte)(MessagePackCode.MinFixArray | count);
+ writer.Advance(1);
}
/// <summary>
@@ -801,11 +501,11 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteArrayHeader(ref byte[] bytes, int offset, int count)
+ public static void WriteArrayHeader(IBufferWriter<byte> writer, int count)
{
checked
{
- return WriteArrayHeader(ref bytes, offset, (uint)count);
+ WriteArrayHeader(writer, (uint)count);
}
}
@@ -815,37 +515,37 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteArrayHeader(ref byte[] bytes, int offset, uint count)
+ public static void WriteArrayHeader(IBufferWriter<byte> writer, uint count)
{
if (count <= MessagePackRange.MaxFixArrayCount)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = (byte)(MessagePackCode.MinFixArray | count);
- return 1;
+ var span = writer.GetSpan(1);
+ span[0] = (byte)(MessagePackCode.MinFixArray | count);
+ writer.Advance(1);
}
else if (count <= ushort.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 3);
+ var span = writer.GetSpan(3);
unchecked
{
- bytes[offset] = MessagePackCode.Array16;
- bytes[offset + 1] = (byte)(count >> 8);
- bytes[offset + 2] = (byte)(count);
+ span[0] = MessagePackCode.Array16;
+ span[1] = (byte)(count >> 8);
+ span[2] = (byte)(count);
}
- return 3;
+ writer.Advance(3);
}
else
{
- EnsureCapacity(ref bytes, offset, 5);
+ var span = writer.GetSpan(5);
unchecked
{
- bytes[offset] = MessagePackCode.Array32;
- bytes[offset + 1] = (byte)(count >> 24);
- bytes[offset + 2] = (byte)(count >> 16);
- bytes[offset + 3] = (byte)(count >> 8);
- bytes[offset + 4] = (byte)(count);
+ span[0] = MessagePackCode.Array32;
+ span[1] = (byte)(count >> 24);
+ span[2] = (byte)(count >> 16);
+ span[3] = (byte)(count >> 8);
+ span[4] = (byte)(count);
}
- return 5;
+ writer.Advance(5);
}
}
@@ -855,16 +555,16 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteArrayHeaderForceArray32Block(ref byte[] bytes, int offset, uint count)
+ public static int WriteArrayHeaderForceArray32Block(IBufferWriter<byte> writer, uint count)
{
- EnsureCapacity(ref bytes, offset, 5);
+ var span = writer.GetSpan(5);
unchecked
{
- bytes[offset] = MessagePackCode.Array32;
- bytes[offset + 1] = (byte)(count >> 24);
- bytes[offset + 2] = (byte)(count >> 16);
- bytes[offset + 3] = (byte)(count >> 8);
- bytes[offset + 4] = (byte)(count);
+ span[0] = MessagePackCode.Array32;
+ span[1] = (byte)(count >> 24);
+ span[2] = (byte)(count >> 16);
+ span[3] = (byte)(count >> 8);
+ span[4] = (byte)(count);
}
return 5;
}
@@ -875,11 +575,11 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int ReadArrayHeader(byte[] bytes, int offset, out int readSize)
+ public static int ReadArrayHeader(ref ReadOnlySequence<byte> byteSequence)
{
checked
{
- return (int)arrayHeaderDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return (int)arrayHeaderDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
}
@@ -889,47 +589,48 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static uint ReadArrayHeaderRaw(byte[] bytes, int offset, out int readSize)
+ public static uint ReadArrayHeaderRaw(ref ReadOnlySequence<byte> byteSequence)
{
- return arrayHeaderDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return arrayHeaderDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteBoolean(ref byte[] bytes, int offset, bool value)
+ public static int WriteBoolean(IBufferWriter<byte> writer, bool value)
{
- EnsureCapacity(ref bytes, offset, 1);
+ var span = writer.GetSpan(1);
- bytes[offset] = (value ? MessagePackCode.True : MessagePackCode.False);
+ span[0] = (value ? MessagePackCode.True : MessagePackCode.False);
return 1;
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static bool ReadBoolean(byte[] bytes, int offset, out int readSize)
+ public static bool ReadBoolean(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return booleanDecoders[bytes[offset]].Read();
+ bool result = booleanDecoders[byteSequence.First.Span[0]].Read();
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteByte(ref byte[] bytes, int offset, byte value)
+ public static int WriteByte(IBufferWriter<byte> writer, byte value)
{
if (value <= MessagePackCode.MaxFixInt)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = value;
+ var span = writer.GetSpan(1);
+ span[0] = value;
return 1;
}
else
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.UInt8;
- bytes[offset + 1] = value;
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.UInt8;
+ span[1] = value;
return 2;
}
}
@@ -937,124 +638,109 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteByteForceByteBlock(ref byte[] bytes, int offset, byte value)
+ public static int WriteByteForceByteBlock(IBufferWriter<byte> writer, byte value)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.UInt8;
- bytes[offset + 1] = value;
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.UInt8;
+ span[1] = value;
return 2;
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static byte ReadByte(byte[] bytes, int offset, out int readSize)
- {
- return byteDecoders[bytes[offset]].Read(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteBytes(ref byte[] bytes, int offset, byte[] value)
+ public static byte ReadByte(ref ReadOnlySequence<byte> byteSequence)
{
- if (value == null)
- {
- return WriteNil(ref bytes, offset);
- }
- else
- {
- return WriteBytes(ref bytes, offset, value, 0, value.Length);
- }
+ return byteDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteBytes(ref byte[] dest, int dstOffset, byte[] src, int srcOffset, int count)
+ public static void WriteBytes(IBufferWriter<byte> writer, ReadOnlySpan<byte> src)
{
if (src == null)
{
- return WriteNil(ref dest, dstOffset);
+ WriteNil(writer);
}
- if (count <= byte.MaxValue)
+ if (src.Length <= byte.MaxValue)
{
- var size = count + 2;
- EnsureCapacity(ref dest, dstOffset, size);
+ var size = src.Length + 2;
+ var span = writer.GetSpan(size);
- dest[dstOffset] = MessagePackCode.Bin8;
- dest[dstOffset + 1] = (byte)count;
+ span[0] = MessagePackCode.Bin8;
+ span[1] = (byte)src.Length;
- Buffer.BlockCopy(src, srcOffset, dest, dstOffset + 2, count);
- return size;
+ src.CopyTo(span.Slice(2));
+ writer.Advance(size);
}
- else if (count <= UInt16.MaxValue)
+ else if (src.Length <= UInt16.MaxValue)
{
- var size = count + 3;
- EnsureCapacity(ref dest, dstOffset, size);
+ var size = src.Length + 3;
+ var span = writer.GetSpan(size);
unchecked
{
- dest[dstOffset] = MessagePackCode.Bin16;
- dest[dstOffset + 1] = (byte)(count >> 8);
- dest[dstOffset + 2] = (byte)(count);
+ span[0] = MessagePackCode.Bin16;
+ span[1] = (byte)(src.Length >> 8);
+ span[2] = (byte)(src.Length);
}
- Buffer.BlockCopy(src, srcOffset, dest, dstOffset + 3, count);
- return size;
+ src.CopyTo(span.Slice(3));
+ writer.Advance(size);
}
else
{
- var size = count + 5;
- EnsureCapacity(ref dest, dstOffset, size);
+ var size = src.Length + 5;
+ var span = writer.GetSpan(size);
unchecked
{
- dest[dstOffset] = MessagePackCode.Bin32;
- dest[dstOffset + 1] = (byte)(count >> 24);
- dest[dstOffset + 2] = (byte)(count >> 16);
- dest[dstOffset + 3] = (byte)(count >> 8);
- dest[dstOffset + 4] = (byte)(count);
+ span[0] = MessagePackCode.Bin32;
+ span[1] = (byte)(src.Length >> 24);
+ span[2] = (byte)(src.Length >> 16);
+ span[3] = (byte)(src.Length >> 8);
+ span[4] = (byte)(src.Length);
}
- Buffer.BlockCopy(src, srcOffset, dest, dstOffset + 5, count);
- return size;
+ src.CopyTo(span.Slice(5));
+ writer.Advance(size);
}
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static byte[] ReadBytes(byte[] bytes, int offset, out int readSize)
+ public static byte[] ReadBytes(ref ReadOnlySequence<byte> byteSequence)
{
- return bytesDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return bytesDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static ArraySegment<byte> ReadBytesSegment(byte[] bytes, int offset, out int readSize)
+ public static ArraySegment<byte> ReadBytesSegment(ref ReadOnlySequence<byte> byteSequence)
{
- return bytesSegmentDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return bytesSegmentDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteSByte(ref byte[] bytes, int offset, sbyte value)
+ public static int WriteSByte(IBufferWriter<byte> writer, sbyte value)
{
if (value < MessagePackRange.MinFixNegativeInt)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.Int8;
- bytes[offset + 1] = unchecked((byte)(value));
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.Int8;
+ span[1] = unchecked((byte)(value));
return 2;
}
else
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = unchecked((byte)value);
+ var span = writer.GetSpan(1);
+ span[0] = unchecked((byte)value);
return 1;
}
}
@@ -1062,45 +748,45 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteSByteForceSByteBlock(ref byte[] bytes, int offset, sbyte value)
+ public static int WriteSByteForceSByteBlock(IBufferWriter<byte> writer, sbyte value)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.Int8;
- bytes[offset + 1] = unchecked((byte)(value));
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.Int8;
+ span[1] = unchecked((byte)(value));
return 2;
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static sbyte ReadSByte(byte[] bytes, int offset, out int readSize)
+ public static sbyte ReadSByte(ref ReadOnlySequence<byte> byteSequence)
{
- return sbyteDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return sbyteDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteSingle(ref byte[] bytes, int offset, float value)
+ public static int WriteSingle(IBufferWriter<byte> writer, float value)
{
- EnsureCapacity(ref bytes, offset, 5);
+ var span = writer.GetSpan(5);
- bytes[offset] = MessagePackCode.Float32;
+ span[0] = MessagePackCode.Float32;
var num = new Float32Bits(value);
if (BitConverter.IsLittleEndian)
{
- bytes[offset + 1] = num.Byte3;
- bytes[offset + 2] = num.Byte2;
- bytes[offset + 3] = num.Byte1;
- bytes[offset + 4] = num.Byte0;
+ span[1] = num.Byte3;
+ span[2] = num.Byte2;
+ span[3] = num.Byte1;
+ span[4] = num.Byte0;
}
else
{
- bytes[offset + 1] = num.Byte0;
- bytes[offset + 2] = num.Byte1;
- bytes[offset + 3] = num.Byte2;
- bytes[offset + 4] = num.Byte3;
+ span[1] = num.Byte0;
+ span[2] = num.Byte1;
+ span[3] = num.Byte2;
+ span[4] = num.Byte3;
}
return 5;
@@ -1109,42 +795,42 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static float ReadSingle(byte[] bytes, int offset, out int readSize)
+ public static float ReadSingle(ref ReadOnlySequence<byte> byteSequence)
{
- return singleDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return singleDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteDouble(ref byte[] bytes, int offset, double value)
+ public static int WriteDouble(IBufferWriter<byte> writer, double value)
{
- EnsureCapacity(ref bytes, offset, 9);
+ var span = writer.GetSpan(9);
- bytes[offset] = MessagePackCode.Float64;
+ span[0] = MessagePackCode.Float64;
var num = new Float64Bits(value);
if (BitConverter.IsLittleEndian)
{
- bytes[offset + 1] = num.Byte7;
- bytes[offset + 2] = num.Byte6;
- bytes[offset + 3] = num.Byte5;
- bytes[offset + 4] = num.Byte4;
- bytes[offset + 5] = num.Byte3;
- bytes[offset + 6] = num.Byte2;
- bytes[offset + 7] = num.Byte1;
- bytes[offset + 8] = num.Byte0;
+ span[1] = num.Byte7;
+ span[2] = num.Byte6;
+ span[3] = num.Byte5;
+ span[4] = num.Byte4;
+ span[5] = num.Byte3;
+ span[6] = num.Byte2;
+ span[7] = num.Byte1;
+ span[8] = num.Byte0;
}
else
{
- bytes[offset + 1] = num.Byte0;
- bytes[offset + 2] = num.Byte1;
- bytes[offset + 3] = num.Byte2;
- bytes[offset + 4] = num.Byte3;
- bytes[offset + 5] = num.Byte4;
- bytes[offset + 6] = num.Byte5;
- bytes[offset + 7] = num.Byte6;
- bytes[offset + 8] = num.Byte7;
+ span[1] = num.Byte0;
+ span[2] = num.Byte1;
+ span[3] = num.Byte2;
+ span[4] = num.Byte3;
+ span[5] = num.Byte4;
+ span[6] = num.Byte5;
+ span[7] = num.Byte6;
+ span[8] = num.Byte7;
}
return 9;
@@ -1153,38 +839,38 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static double ReadDouble(byte[] bytes, int offset, out int readSize)
+ public static double ReadDouble(ref ReadOnlySequence<byte> byteSequence)
{
- return doubleDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return doubleDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteInt16(ref byte[] bytes, int offset, short value)
+ public static int WriteInt16(IBufferWriter<byte> writer, short value)
{
if (value >= 0)
{
// positive int(use uint)
if (value <= MessagePackRange.MaxFixPositiveInt)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = unchecked((byte)value);
+ var span = writer.GetSpan(1);
+ span[0] = unchecked((byte)value);
return 1;
}
else if (value <= byte.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.UInt8;
- bytes[offset + 1] = unchecked((byte)value);
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.UInt8;
+ span[1] = unchecked((byte)value);
return 2;
}
else
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.UInt16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.UInt16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
return 3;
}
}
@@ -1193,23 +879,23 @@ namespace MessagePack
// negative int(use int)
if (MessagePackRange.MinFixNegativeInt <= value)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = unchecked((byte)value);
+ var span = writer.GetSpan(1);
+ span[0] = unchecked((byte)value);
return 1;
}
else if (sbyte.MinValue <= value)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.Int8;
- bytes[offset + 1] = unchecked((byte)value);
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.Int8;
+ span[1] = unchecked((byte)value);
return 2;
}
else
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.Int16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.Int16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
return 3;
}
}
@@ -1218,21 +904,21 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteInt16ForceInt16Block(ref byte[] bytes, int offset, short value)
+ public static int WriteInt16ForceInt16Block(IBufferWriter<byte> writer, short value)
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.Int16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.Int16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
return 3;
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static short ReadInt16(byte[] bytes, int offset, out int readSize)
+ public static short ReadInt16(ref ReadOnlySequence<byte> byteSequence)
{
- return int16Decoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return int16Decoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
/// <summary>
@@ -1241,51 +927,51 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WritePositiveFixedIntUnsafe(ref byte[] bytes, int offset, int value)
+ public static int WritePositiveFixedIntUnsafe(IBufferWriter<byte> writer, int value)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = (byte)value;
+ var span = writer.GetSpan(1);
+ span[0] = (byte)value;
return 1;
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteInt32(ref byte[] bytes, int offset, int value)
+ public static void WriteInt32(IBufferWriter<byte> writer, int value)
{
if (value >= 0)
{
// positive int(use uint)
if (value <= MessagePackRange.MaxFixPositiveInt)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = unchecked((byte)value);
- return 1;
+ var span = writer.GetSpan(1);
+ span[0] = unchecked((byte)value);
+ writer.Advance(1);
}
else if (value <= byte.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.UInt8;
- bytes[offset + 1] = unchecked((byte)value);
- return 2;
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.UInt8;
+ span[1] = unchecked((byte)value);
+ writer.Advance(2);
}
else if (value <= ushort.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.UInt16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
- return 3;
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.UInt16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
+ writer.Advance(3);
}
else
{
- EnsureCapacity(ref bytes, offset, 5);
- bytes[offset] = MessagePackCode.UInt32;
- bytes[offset + 1] = unchecked((byte)(value >> 24));
- bytes[offset + 2] = unchecked((byte)(value >> 16));
- bytes[offset + 3] = unchecked((byte)(value >> 8));
- bytes[offset + 4] = unchecked((byte)value);
- return 5;
+ var span = writer.GetSpan(5);
+ span[0] = MessagePackCode.UInt32;
+ span[1] = unchecked((byte)(value >> 24));
+ span[2] = unchecked((byte)(value >> 16));
+ span[3] = unchecked((byte)(value >> 8));
+ span[4] = unchecked((byte)value);
+ writer.Advance(5);
}
}
else
@@ -1293,34 +979,34 @@ namespace MessagePack
// negative int(use int)
if (MessagePackRange.MinFixNegativeInt <= value)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = unchecked((byte)value);
- return 1;
+ var span = writer.GetSpan(1);
+ span[0] = unchecked((byte)value);
+ writer.Advance(1);
}
else if (sbyte.MinValue <= value)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.Int8;
- bytes[offset + 1] = unchecked((byte)value);
- return 2;
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.Int8;
+ span[1] = unchecked((byte)value);
+ writer.Advance(2);
}
else if (short.MinValue <= value)
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.Int16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
- return 3;
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.Int16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
+ writer.Advance(3);
}
else
{
- EnsureCapacity(ref bytes, offset, 5);
- bytes[offset] = MessagePackCode.Int32;
- bytes[offset + 1] = unchecked((byte)(value >> 24));
- bytes[offset + 2] = unchecked((byte)(value >> 16));
- bytes[offset + 3] = unchecked((byte)(value >> 8));
- bytes[offset + 4] = unchecked((byte)value);
- return 5;
+ var span = writer.GetSpan(5);
+ span[0] = MessagePackCode.Int32;
+ span[1] = unchecked((byte)(value >> 24));
+ span[2] = unchecked((byte)(value >> 16));
+ span[3] = unchecked((byte)(value >> 8));
+ span[4] = unchecked((byte)value);
+ writer.Advance(5);
}
}
}
@@ -1331,77 +1017,77 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteInt32ForceInt32Block(ref byte[] bytes, int offset, int value)
- {
- EnsureCapacity(ref bytes, offset, 5);
- bytes[offset] = MessagePackCode.Int32;
- bytes[offset + 1] = unchecked((byte)(value >> 24));
- bytes[offset + 2] = unchecked((byte)(value >> 16));
- bytes[offset + 3] = unchecked((byte)(value >> 8));
- bytes[offset + 4] = unchecked((byte)value);
+ public static int WriteInt32ForceInt32Block(IBufferWriter<byte> writer, int value)
+ {
+ var span = writer.GetSpan(5);
+ span[0] = MessagePackCode.Int32;
+ span[1] = unchecked((byte)(value >> 24));
+ span[2] = unchecked((byte)(value >> 16));
+ span[3] = unchecked((byte)(value >> 8));
+ span[4] = unchecked((byte)value);
return 5;
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int ReadInt32(byte[] bytes, int offset, out int readSize)
+ public static int ReadInt32(ref ReadOnlySequence<byte> byteSequence)
{
- return int32Decoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return int32Decoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteInt64(ref byte[] bytes, int offset, long value)
+ public static void WriteInt64(IBufferWriter<byte> writer, long value)
{
if (value >= 0)
{
// positive int(use uint)
if (value <= MessagePackRange.MaxFixPositiveInt)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = unchecked((byte)value);
- return 1;
+ var span = writer.GetSpan(1);
+ span[0] = unchecked((byte)value);
+ writer.Advance(1);
}
else if (value <= byte.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.UInt8;
- bytes[offset + 1] = unchecked((byte)value);
- return 2;
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.UInt8;
+ span[1] = unchecked((byte)value);
+ writer.Advance(2);
}
else if (value <= ushort.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.UInt16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
- return 3;
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.UInt16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
+ writer.Advance(3);
}
else if (value <= uint.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 5);
- bytes[offset] = MessagePackCode.UInt32;
- bytes[offset + 1] = unchecked((byte)(value >> 24));
- bytes[offset + 2] = unchecked((byte)(value >> 16));
- bytes[offset + 3] = unchecked((byte)(value >> 8));
- bytes[offset + 4] = unchecked((byte)value);
- return 5;
+ var span = writer.GetSpan(5);
+ span[0] = MessagePackCode.UInt32;
+ span[1] = unchecked((byte)(value >> 24));
+ span[2] = unchecked((byte)(value >> 16));
+ span[3] = unchecked((byte)(value >> 8));
+ span[4] = unchecked((byte)value);
+ writer.Advance(5);
}
else
{
- EnsureCapacity(ref bytes, offset, 9);
- bytes[offset] = MessagePackCode.UInt64;
- bytes[offset + 1] = unchecked((byte)(value >> 56));
- bytes[offset + 2] = unchecked((byte)(value >> 48));
- bytes[offset + 3] = unchecked((byte)(value >> 40));
- bytes[offset + 4] = unchecked((byte)(value >> 32));
- bytes[offset + 5] = unchecked((byte)(value >> 24));
- bytes[offset + 6] = unchecked((byte)(value >> 16));
- bytes[offset + 7] = unchecked((byte)(value >> 8));
- bytes[offset + 8] = unchecked((byte)value);
- return 9;
+ var span = writer.GetSpan(9);
+ span[0] = MessagePackCode.UInt64;
+ span[1] = unchecked((byte)(value >> 56));
+ span[2] = unchecked((byte)(value >> 48));
+ span[3] = unchecked((byte)(value >> 40));
+ span[4] = unchecked((byte)(value >> 32));
+ span[5] = unchecked((byte)(value >> 24));
+ span[6] = unchecked((byte)(value >> 16));
+ span[7] = unchecked((byte)(value >> 8));
+ span[8] = unchecked((byte)value);
+ writer.Advance(9);
}
}
else
@@ -1409,48 +1095,48 @@ namespace MessagePack
// negative int(use int)
if (MessagePackRange.MinFixNegativeInt <= value)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = unchecked((byte)value);
- return 1;
+ var span = writer.GetSpan(1);
+ span[0] = unchecked((byte)value);
+ writer.Advance(1);
}
else if (sbyte.MinValue <= value)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.Int8;
- bytes[offset + 1] = unchecked((byte)value);
- return 2;
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.Int8;
+ span[1] = unchecked((byte)value);
+ writer.Advance(2);
}
else if (short.MinValue <= value)
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.Int16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
- return 3;
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.Int16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
+ writer.Advance(3);
}
else if (int.MinValue <= value)
{
- EnsureCapacity(ref bytes, offset, 5);
- bytes[offset] = MessagePackCode.Int32;
- bytes[offset + 1] = unchecked((byte)(value >> 24));
- bytes[offset + 2] = unchecked((byte)(value >> 16));
- bytes[offset + 3] = unchecked((byte)(value >> 8));
- bytes[offset + 4] = unchecked((byte)value);
- return 5;
+ var span = writer.GetSpan(5);
+ span[0] = MessagePackCode.Int32;
+ span[1] = unchecked((byte)(value >> 24));
+ span[2] = unchecked((byte)(value >> 16));
+ span[3] = unchecked((byte)(value >> 8));
+ span[4] = unchecked((byte)value);
+ writer.Advance(5);
}
else
{
- EnsureCapacity(ref bytes, offset, 9);
- bytes[offset] = MessagePackCode.Int64;
- bytes[offset + 1] = unchecked((byte)(value >> 56));
- bytes[offset + 2] = unchecked((byte)(value >> 48));
- bytes[offset + 3] = unchecked((byte)(value >> 40));
- bytes[offset + 4] = unchecked((byte)(value >> 32));
- bytes[offset + 5] = unchecked((byte)(value >> 24));
- bytes[offset + 6] = unchecked((byte)(value >> 16));
- bytes[offset + 7] = unchecked((byte)(value >> 8));
- bytes[offset + 8] = unchecked((byte)value);
- return 9;
+ var span = writer.GetSpan(9);
+ span[0] = MessagePackCode.Int64;
+ span[1] = unchecked((byte)(value >> 56));
+ span[2] = unchecked((byte)(value >> 48));
+ span[3] = unchecked((byte)(value >> 40));
+ span[4] = unchecked((byte)(value >> 32));
+ span[5] = unchecked((byte)(value >> 24));
+ span[6] = unchecked((byte)(value >> 16));
+ span[7] = unchecked((byte)(value >> 8));
+ span[8] = unchecked((byte)value);
+ writer.Advance(9);
}
}
}
@@ -1458,111 +1144,111 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteInt64ForceInt64Block(ref byte[] bytes, int offset, long value)
- {
- EnsureCapacity(ref bytes, offset, 9);
- bytes[offset] = MessagePackCode.Int64;
- bytes[offset + 1] = unchecked((byte)(value >> 56));
- bytes[offset + 2] = unchecked((byte)(value >> 48));
- bytes[offset + 3] = unchecked((byte)(value >> 40));
- bytes[offset + 4] = unchecked((byte)(value >> 32));
- bytes[offset + 5] = unchecked((byte)(value >> 24));
- bytes[offset + 6] = unchecked((byte)(value >> 16));
- bytes[offset + 7] = unchecked((byte)(value >> 8));
- bytes[offset + 8] = unchecked((byte)value);
- return 9;
+ public static void WriteInt64ForceInt64Block(IBufferWriter<byte> writer, long value)
+ {
+ var span = writer.GetSpan(9);
+ span[0] = MessagePackCode.Int64;
+ span[1] = unchecked((byte)(value >> 56));
+ span[2] = unchecked((byte)(value >> 48));
+ span[3] = unchecked((byte)(value >> 40));
+ span[4] = unchecked((byte)(value >> 32));
+ span[5] = unchecked((byte)(value >> 24));
+ span[6] = unchecked((byte)(value >> 16));
+ span[7] = unchecked((byte)(value >> 8));
+ span[8] = unchecked((byte)value);
+ writer.Advance(9);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static long ReadInt64(byte[] bytes, int offset, out int readSize)
+ public static long ReadInt64(ref ReadOnlySequence<byte> byteSequence)
{
- return int64Decoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return int64Decoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteUInt16(ref byte[] bytes, int offset, ushort value)
+ public static void WriteUInt16(IBufferWriter<byte> writer, ushort value)
{
if (value <= MessagePackRange.MaxFixPositiveInt)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = unchecked((byte)value);
- return 1;
+ var span = writer.GetSpan(1);
+ span[0] = unchecked((byte)value);
+ writer.Advance(1);
}
else if (value <= byte.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.UInt8;
- bytes[offset + 1] = unchecked((byte)value);
- return 2;
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.UInt8;
+ span[1] = unchecked((byte)value);
+ writer.Advance(2);
}
else
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.UInt16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
- return 3;
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.UInt16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
+ writer.Advance(3);
}
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteUInt16ForceUInt16Block(ref byte[] bytes, int offset, ushort value)
+ public static void WriteUInt16ForceUInt16Block(IBufferWriter<byte> writer, ushort value)
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.UInt16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
- return 3;
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.UInt16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
+ writer.Advance(3);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static ushort ReadUInt16(byte[] bytes, int offset, out int readSize)
+ public static ushort ReadUInt16(ref ReadOnlySequence<byte> byteSequence)
{
- return uint16Decoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return uint16Decoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteUInt32(ref byte[] bytes, int offset, uint value)
+ public static int WriteUInt32(IBufferWriter<byte> writer, uint value)
{
if (value <= MessagePackRange.MaxFixPositiveInt)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = unchecked((byte)value);
+ var span = writer.GetSpan(1);
+ span[0] = unchecked((byte)value);
return 1;
}
else if (value <= byte.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.UInt8;
- bytes[offset + 1] = unchecked((byte)value);
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.UInt8;
+ span[1] = unchecked((byte)value);
return 2;
}
else if (value <= ushort.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.UInt16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.UInt16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
return 3;
}
else
{
- EnsureCapacity(ref bytes, offset, 5);
- bytes[offset] = MessagePackCode.UInt32;
- bytes[offset + 1] = unchecked((byte)(value >> 24));
- bytes[offset + 2] = unchecked((byte)(value >> 16));
- bytes[offset + 3] = unchecked((byte)(value >> 8));
- bytes[offset + 4] = unchecked((byte)value);
+ var span = writer.GetSpan(5);
+ span[0] = MessagePackCode.UInt32;
+ span[1] = unchecked((byte)(value >> 24));
+ span[2] = unchecked((byte)(value >> 16));
+ span[3] = unchecked((byte)(value >> 8));
+ span[4] = unchecked((byte)value);
return 5;
}
}
@@ -1570,132 +1256,130 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteUInt32ForceUInt32Block(ref byte[] bytes, int offset, uint value)
- {
- EnsureCapacity(ref bytes, offset, 5);
- bytes[offset] = MessagePackCode.UInt32;
- bytes[offset + 1] = unchecked((byte)(value >> 24));
- bytes[offset + 2] = unchecked((byte)(value >> 16));
- bytes[offset + 3] = unchecked((byte)(value >> 8));
- bytes[offset + 4] = unchecked((byte)value);
+ public static int WriteUInt32ForceUInt32Block(IBufferWriter<byte> writer, uint value)
+ {
+ var span = writer.GetSpan(5);
+ span[0] = MessagePackCode.UInt32;
+ span[1] = unchecked((byte)(value >> 24));
+ span[2] = unchecked((byte)(value >> 16));
+ span[3] = unchecked((byte)(value >> 8));
+ span[4] = unchecked((byte)value);
return 5;
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static uint ReadUInt32(byte[] bytes, int offset, out int readSize)
+ public static uint ReadUInt32(ref ReadOnlySequence<byte> byteSequence)
{
- return uint32Decoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return uint32Decoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteUInt64(ref byte[] bytes, int offset, ulong value)
+ public static void WriteUInt64(IBufferWriter<byte> writer, ulong value)
{
if (value <= MessagePackRange.MaxFixPositiveInt)
{
- EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = unchecked((byte)value);
- return 1;
+ var span = writer.GetSpan(1);
+ span[0] = unchecked((byte)value);
+ writer.Advance(1);
}
else if (value <= byte.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 2);
- bytes[offset] = MessagePackCode.UInt8;
- bytes[offset + 1] = unchecked((byte)value);
- return 2;
+ var span = writer.GetSpan(2);
+ span[0] = MessagePackCode.UInt8;
+ span[1] = unchecked((byte)value);
+ writer.Advance(2);
}
else if (value <= ushort.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.UInt16;
- bytes[offset + 1] = unchecked((byte)(value >> 8));
- bytes[offset + 2] = unchecked((byte)value);
- return 3;
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.UInt16;
+ span[1] = unchecked((byte)(value >> 8));
+ span[2] = unchecked((byte)value);
+ writer.Advance(3);
}
else if (value <= uint.MaxValue)
{
- EnsureCapacity(ref bytes, offset, 5);
- bytes[offset] = MessagePackCode.UInt32;
- bytes[offset + 1] = unchecked((byte)(value >> 24));
- bytes[offset + 2] = unchecked((byte)(value >> 16));
- bytes[offset + 3] = unchecked((byte)(value >> 8));
- bytes[offset + 4] = unchecked((byte)value);
- return 5;
+ var span = writer.GetSpan(5);
+ span[0] = MessagePackCode.UInt32;
+ span[1] = unchecked((byte)(value >> 24));
+ span[2] = unchecked((byte)(value >> 16));
+ span[3] = unchecked((byte)(value >> 8));
+ span[4] = unchecked((byte)value);
+ writer.Advance(5);
}
else
{
- EnsureCapacity(ref bytes, offset, 9);
- bytes[offset] = MessagePackCode.UInt64;
- bytes[offset + 1] = unchecked((byte)(value >> 56));
- bytes[offset + 2] = unchecked((byte)(value >> 48));
- bytes[offset + 3] = unchecked((byte)(value >> 40));
- bytes[offset + 4] = unchecked((byte)(value >> 32));
- bytes[offset + 5] = unchecked((byte)(value >> 24));
- bytes[offset + 6] = unchecked((byte)(value >> 16));
- bytes[offset + 7] = unchecked((byte)(value >> 8));
- bytes[offset + 8] = unchecked((byte)value);
- return 9;
+ var span = writer.GetSpan(9);
+ span[0] = MessagePackCode.UInt64;
+ span[1] = unchecked((byte)(value >> 56));
+ span[2] = unchecked((byte)(value >> 48));
+ span[3] = unchecked((byte)(value >> 40));
+ span[4] = unchecked((byte)(value >> 32));
+ span[5] = unchecked((byte)(value >> 24));
+ span[6] = unchecked((byte)(value >> 16));
+ span[7] = unchecked((byte)(value >> 8));
+ span[8] = unchecked((byte)value);
+ writer.Advance(9);
}
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteUInt64ForceUInt64Block(ref byte[] bytes, int offset, ulong value)
- {
- EnsureCapacity(ref bytes, offset, 9);
- bytes[offset] = MessagePackCode.UInt64;
- bytes[offset + 1] = unchecked((byte)(value >> 56));
- bytes[offset + 2] = unchecked((byte)(value >> 48));
- bytes[offset + 3] = unchecked((byte)(value >> 40));
- bytes[offset + 4] = unchecked((byte)(value >> 32));
- bytes[offset + 5] = unchecked((byte)(value >> 24));
- bytes[offset + 6] = unchecked((byte)(value >> 16));
- bytes[offset + 7] = unchecked((byte)(value >> 8));
- bytes[offset + 8] = unchecked((byte)value);
- return 9;
+ public static void WriteUInt64ForceUInt64Block(IBufferWriter<byte> writer, ulong value)
+ {
+ var span = writer.GetSpan(9);
+ span[0] = MessagePackCode.UInt64;
+ span[1] = unchecked((byte)(value >> 56));
+ span[2] = unchecked((byte)(value >> 48));
+ span[3] = unchecked((byte)(value >> 40));
+ span[4] = unchecked((byte)(value >> 32));
+ span[5] = unchecked((byte)(value >> 24));
+ span[6] = unchecked((byte)(value >> 16));
+ span[7] = unchecked((byte)(value >> 8));
+ span[8] = unchecked((byte)value);
+ writer.Advance(9);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static ulong ReadUInt64(byte[] bytes, int offset, out int readSize)
+ public static ulong ReadUInt64(ref ReadOnlySequence<byte> byteSequence)
{
- return uint64Decoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return uint64Decoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteChar(ref byte[] bytes, int offset, char value)
+ public static void WriteChar(IBufferWriter<byte> writer, char value)
{
- return WriteUInt16(ref bytes, offset, (ushort)value);
+ WriteUInt16(writer, value);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static char ReadChar(byte[] bytes, int offset, out int readSize)
+ public static char ReadChar(ref ReadOnlySequence<byte> byteSequence)
{
- return (char)ReadUInt16(bytes, offset, out readSize);
+ return (char)ReadUInt16(ref byteSequence);
}
/// <summary>
- /// Unsafe. If value is guranteed length is 0 ~ 31, can use this method.
+ /// Unsafe. If value is guaranteed length is 0 ~ 31, can use this method.
/// </summary>
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteFixedStringUnsafe(ref byte[] bytes, int offset, string value, int byteCount)
+ public static unsafe void WriteFixedStringUnsafe(IBufferWriter<byte> writer, string value, int byteCount)
{
- EnsureCapacity(ref bytes, offset, byteCount + 1);
- bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount);
- StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 1);
-
- return byteCount + 1;
+ var span = writer.GetSpan(byteCount + 1);
+ span[0] = (byte)(MessagePackCode.MinFixStr | byteCount);
+ writer.Advance(StringEncoding.UTF8.GetBytes(value, span.Slice(1)) + 1);
}
/// <summary>
@@ -1704,41 +1388,41 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteStringUnsafe(ref byte[] bytes, int offset, string value, int byteCount)
+ public static int WriteStringUnsafe(IBufferWriter<byte> writer, string value, int byteCount)
{
if (byteCount <= MessagePackRange.MaxFixStringLength)
{
- EnsureCapacity(ref bytes, offset, byteCount + 1);
- bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount);
- StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 1);
+ var span = writer.GetSpan(byteCount + 1);
+ span[0] = (byte)(MessagePackCode.MinFixStr | byteCount);
+ StringEncoding.UTF8.GetBytes(value, span.Slice(1));
return byteCount + 1;
}
else if (byteCount <= byte.MaxValue)
{
- EnsureCapacity(ref bytes, offset, byteCount + 2);
- bytes[offset] = MessagePackCode.Str8;
- bytes[offset + 1] = unchecked((byte)byteCount);
- StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 2);
+ var span = writer.GetSpan(byteCount + 2);
+ span[0] = MessagePackCode.Str8;
+ span[1] = unchecked((byte)byteCount);
+ StringEncoding.UTF8.GetBytes(value, span.Slice(2));
return byteCount + 2;
}
else if (byteCount <= ushort.MaxValue)
{
- EnsureCapacity(ref bytes, offset, byteCount + 3);
- bytes[offset] = MessagePackCode.Str16;
- bytes[offset + 1] = unchecked((byte)(byteCount >> 8));
- bytes[offset + 2] = unchecked((byte)byteCount);
- StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 3);
+ var span = writer.GetSpan(byteCount + 3);
+ span[0] = MessagePackCode.Str16;
+ span[1] = unchecked((byte)(byteCount >> 8));
+ span[2] = unchecked((byte)byteCount);
+ StringEncoding.UTF8.GetBytes(value, span.Slice(3));
return byteCount + 3;
}
else
{
- EnsureCapacity(ref bytes, offset, byteCount + 5);
- bytes[offset] = MessagePackCode.Str32;
- bytes[offset + 1] = unchecked((byte)(byteCount >> 24));
- bytes[offset + 2] = unchecked((byte)(byteCount >> 16));
- bytes[offset + 3] = unchecked((byte)(byteCount >> 8));
- bytes[offset + 4] = unchecked((byte)byteCount);
- StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 5);
+ var span = writer.GetSpan(byteCount + 5);
+ span[0] = MessagePackCode.Str32;
+ span[1] = unchecked((byte)(byteCount >> 24));
+ span[2] = unchecked((byte)(byteCount >> 16));
+ span[3] = unchecked((byte)(byteCount >> 8));
+ span[4] = unchecked((byte)byteCount);
+ StringEncoding.UTF8.GetBytes(value, span.Slice(5));
return byteCount + 5;
}
}
@@ -1746,43 +1430,43 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteStringBytes(ref byte[] bytes, int offset, byte[] utf8stringBytes)
+ public static void WriteStringBytes(IBufferWriter<byte> writer, ReadOnlySpan<byte> utf8stringBytes)
{
var byteCount = utf8stringBytes.Length;
if (byteCount <= MessagePackRange.MaxFixStringLength)
{
- EnsureCapacity(ref bytes, offset, byteCount + 1);
- bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount);
- Buffer.BlockCopy(utf8stringBytes, 0, bytes, offset + 1, byteCount);
- return byteCount + 1;
+ var span = writer.GetSpan(byteCount + 1);
+ span[0] = (byte)(MessagePackCode.MinFixStr | byteCount);
+ utf8stringBytes.CopyTo(span.Slice(1));
+ writer.Advance(1);
}
else if (byteCount <= byte.MaxValue)
{
- EnsureCapacity(ref bytes, offset, byteCount + 2);
- bytes[offset] = MessagePackCode.Str8;
- bytes[offset + 1] = unchecked((byte)byteCount);
- Buffer.BlockCopy(utf8stringBytes, 0, bytes, offset + 2, byteCount);
- return byteCount + 2;
+ var span = writer.GetSpan(byteCount + 2);
+ span[0] = MessagePackCode.Str8;
+ span[1] = unchecked((byte)byteCount);
+ utf8stringBytes.CopyTo(span.Slice(2));
+ writer.Advance(2);
}
else if (byteCount <= ushort.MaxValue)
{
- EnsureCapacity(ref bytes, offset, byteCount + 3);
- bytes[offset] = MessagePackCode.Str16;
- bytes[offset + 1] = unchecked((byte)(byteCount >> 8));
- bytes[offset + 2] = unchecked((byte)byteCount);
- Buffer.BlockCopy(utf8stringBytes, 0, bytes, offset + 3, byteCount);
- return byteCount + 3;
+ var span = writer.GetSpan(byteCount + 3);
+ span[0] = MessagePackCode.Str16;
+ span[1] = unchecked((byte)(byteCount >> 8));
+ span[2] = unchecked((byte)byteCount);
+ utf8stringBytes.CopyTo(span.Slice(3));
+ writer.Advance(3);
}
else
{
- EnsureCapacity(ref bytes, offset, byteCount + 5);
- bytes[offset] = MessagePackCode.Str32;
- bytes[offset + 1] = unchecked((byte)(byteCount >> 24));
- bytes[offset + 2] = unchecked((byte)(byteCount >> 16));
- bytes[offset + 3] = unchecked((byte)(byteCount >> 8));
- bytes[offset + 4] = unchecked((byte)byteCount);
- Buffer.BlockCopy(utf8stringBytes, 0, bytes, offset + 5, byteCount);
- return byteCount + 5;
+ var span = writer.GetSpan(byteCount + 5);
+ span[0] = MessagePackCode.Str32;
+ span[1] = unchecked((byte)(byteCount >> 24));
+ span[2] = unchecked((byte)(byteCount >> 16));
+ span[3] = unchecked((byte)(byteCount >> 8));
+ span[4] = unchecked((byte)byteCount);
+ utf8stringBytes.CopyTo(span.Slice(5));
+ writer.Advance(5);
}
}
@@ -1826,15 +1510,19 @@ namespace MessagePack
}
}
- public static int WriteString(ref byte[] bytes, int offset, string value)
+ public static void WriteString(IBufferWriter<byte> writer, string value)
{
- if (value == null) return WriteNil(ref bytes, offset);
+ if (value == null)
+ {
+ WriteNil(writer);
+ return;
+ }
// MaxByteCount -> WritePrefix -> GetBytes has some overheads of `MaxByteCount`
// solves heuristic length check
// ensure buffer by MaxByteCount(faster than GetByteCount)
- MessagePackBinary.EnsureCapacity(ref bytes, offset, StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5);
+ var span = writer.GetSpan(StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5);
int useOffset;
if (value.Length <= MessagePackRange.MaxFixStringLength)
@@ -1855,151 +1543,154 @@ namespace MessagePack
}
// skip length area
- var writeBeginOffset = offset + useOffset;
- var byteCount = StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, writeBeginOffset);
+ var byteCount = StringEncoding.UTF8.GetBytes(value, span.Slice(useOffset));
// move body and write prefix
if (byteCount <= MessagePackRange.MaxFixStringLength)
{
if (useOffset != 1)
{
- Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 1, byteCount);
+ span.Slice(useOffset, byteCount).CopyTo(span.Slice(1));
}
- bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount);
- return byteCount + 1;
+ span[0] = (byte)(MessagePackCode.MinFixStr | byteCount);
+ writer.Advance(byteCount + 1);
}
else if (byteCount <= byte.MaxValue)
{
if (useOffset != 2)
{
- Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 2, byteCount);
+ span.Slice(useOffset, byteCount).CopyTo(span.Slice(2));
}
- bytes[offset] = MessagePackCode.Str8;
- bytes[offset + 1] = unchecked((byte)byteCount);
- return byteCount + 2;
+ span[0] = MessagePackCode.Str8;
+ span[1] = unchecked((byte)byteCount);
+ writer.Advance(byteCount + 2);
}
else if (byteCount <= ushort.MaxValue)
{
if (useOffset != 3)
{
- Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 3, byteCount);
+ span.Slice(useOffset, byteCount).CopyTo(span.Slice(3));
}
- bytes[offset] = MessagePackCode.Str16;
- bytes[offset + 1] = unchecked((byte)(byteCount >> 8));
- bytes[offset + 2] = unchecked((byte)byteCount);
- return byteCount + 3;
+ span[0] = MessagePackCode.Str16;
+ span[1] = unchecked((byte)(byteCount >> 8));
+ span[2] = unchecked((byte)byteCount);
+ writer.Advance(byteCount + 3);
}
else
{
if (useOffset != 5)
{
- Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 5, byteCount);
+ span.Slice(useOffset, byteCount).CopyTo(span.Slice(5));
}
- bytes[offset] = MessagePackCode.Str32;
- bytes[offset + 1] = unchecked((byte)(byteCount >> 24));
- bytes[offset + 2] = unchecked((byte)(byteCount >> 16));
- bytes[offset + 3] = unchecked((byte)(byteCount >> 8));
- bytes[offset + 4] = unchecked((byte)byteCount);
- return byteCount + 5;
+ span[0] = MessagePackCode.Str32;
+ span[1] = unchecked((byte)(byteCount >> 24));
+ span[2] = unchecked((byte)(byteCount >> 16));
+ span[3] = unchecked((byte)(byteCount >> 8));
+ span[4] = unchecked((byte)byteCount);
+ writer.Advance(byteCount + 5);
}
}
- public static int WriteStringForceStr32Block(ref byte[] bytes, int offset, string value)
+ public static void WriteStringForceStr32Block(IBufferWriter<byte> writer, string value)
{
- if (value == null) return WriteNil(ref bytes, offset);
+ if (value == null)
+ {
+ WriteNil(writer);
+ return;
+ }
- MessagePackBinary.EnsureCapacity(ref bytes, offset, StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5);
+ var span = writer.GetSpan(StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5);
- var byteCount = StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 5);
+ var byteCount = StringEncoding.UTF8.GetBytes(value, span.Slice(5));
- bytes[offset] = MessagePackCode.Str32;
- bytes[offset + 1] = unchecked((byte)(byteCount >> 24));
- bytes[offset + 2] = unchecked((byte)(byteCount >> 16));
- bytes[offset + 3] = unchecked((byte)(byteCount >> 8));
- bytes[offset + 4] = unchecked((byte)byteCount);
- return byteCount + 5;
+ span[0] = MessagePackCode.Str32;
+ span[1] = unchecked((byte)(byteCount >> 24));
+ span[2] = unchecked((byte)(byteCount >> 16));
+ span[3] = unchecked((byte)(byteCount >> 8));
+ span[4] = unchecked((byte)byteCount);
+ writer.Advance(byteCount + 5);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static string ReadString(byte[] bytes, int offset, out int readSize)
+ public static string ReadString(ref ReadOnlySequence<byte> byteSequence)
{
- return stringDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return stringDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static ArraySegment<byte> ReadStringSegment(byte[] bytes, int offset, out int readSize)
+ public static ArraySegment<byte> ReadStringSegment(ref ReadOnlySequence<byte> byteSequence)
{
- return stringSegmentDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return stringSegmentDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteExtensionFormatHeader(ref byte[] bytes, int offset, sbyte typeCode, int dataLength)
+ public static int WriteExtensionFormatHeader(IBufferWriter<byte> writer, sbyte typeCode, int dataLength)
{
switch (dataLength)
{
case 1:
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.FixExt1;
- bytes[offset + 1] = unchecked((byte)typeCode);
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.FixExt1;
+ span[1] = unchecked((byte)typeCode);
return 2;
case 2:
- EnsureCapacity(ref bytes, offset, 4);
- bytes[offset] = MessagePackCode.FixExt2;
- bytes[offset + 1] = unchecked((byte)typeCode);
+ span = writer.GetSpan(4);
+ span[0] = MessagePackCode.FixExt2;
+ span[1] = unchecked((byte)typeCode);
return 2;
case 4:
- EnsureCapacity(ref bytes, offset, 6);
- bytes[offset] = MessagePackCode.FixExt4;
- bytes[offset + 1] = unchecked((byte)typeCode);
+ span = writer.GetSpan(6);
+ span[0] = MessagePackCode.FixExt4;
+ span[1] = unchecked((byte)typeCode);
return 2;
case 8:
- EnsureCapacity(ref bytes, offset, 10);
- bytes[offset] = MessagePackCode.FixExt8;
- bytes[offset + 1] = unchecked((byte)typeCode);
+ span = writer.GetSpan(10);
+ span[0] = MessagePackCode.FixExt8;
+ span[1] = unchecked((byte)typeCode);
return 2;
case 16:
- EnsureCapacity(ref bytes, offset, 18);
- bytes[offset] = MessagePackCode.FixExt16;
- bytes[offset + 1] = unchecked((byte)typeCode);
+ span = writer.GetSpan(18);
+ span[0] = MessagePackCode.FixExt16;
+ span[1] = unchecked((byte)typeCode);
return 2;
default:
unchecked
{
if (dataLength <= byte.MaxValue)
{
- EnsureCapacity(ref bytes, offset, dataLength + 3);
- bytes[offset] = MessagePackCode.Ext8;
- bytes[offset + 1] = unchecked((byte)(dataLength));
- bytes[offset + 2] = unchecked((byte)typeCode);
+ span = writer.GetSpan(dataLength + 3);
+ span[0] = MessagePackCode.Ext8;
+ span[1] = unchecked((byte)(dataLength));
+ span[2] = unchecked((byte)typeCode);
return 3;
}
else if (dataLength <= UInt16.MaxValue)
{
- EnsureCapacity(ref bytes, offset, dataLength + 4);
- bytes[offset] = MessagePackCode.Ext16;
- bytes[offset + 1] = unchecked((byte)(dataLength >> 8));
- bytes[offset + 2] = unchecked((byte)(dataLength));
- bytes[offset + 3] = unchecked((byte)typeCode);
+ span = writer.GetSpan(dataLength + 4);
+ span[0] = MessagePackCode.Ext16;
+ span[1] = unchecked((byte)(dataLength >> 8));
+ span[2] = unchecked((byte)(dataLength));
+ span[3] = unchecked((byte)typeCode);
return 4;
}
else
{
- EnsureCapacity(ref bytes, offset, dataLength + 6);
- bytes[offset] = MessagePackCode.Ext32;
- bytes[offset + 1] = unchecked((byte)(dataLength >> 24));
- bytes[offset + 2] = unchecked((byte)(dataLength >> 16));
- bytes[offset + 3] = unchecked((byte)(dataLength >> 8));
- bytes[offset + 4] = unchecked((byte)dataLength);
- bytes[offset + 5] = unchecked((byte)typeCode);
+ span = writer.GetSpan(dataLength + 6);
+ span[0] = MessagePackCode.Ext32;
+ span[1] = unchecked((byte)(dataLength >> 24));
+ span[2] = unchecked((byte)(dataLength >> 16));
+ span[3] = unchecked((byte)(dataLength >> 8));
+ span[4] = unchecked((byte)dataLength);
+ span[5] = unchecked((byte)typeCode);
return 6;
}
}
@@ -2012,115 +1703,123 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteExtensionFormatHeaderForceExt32Block(ref byte[] bytes, int offset, sbyte typeCode, int dataLength)
- {
- EnsureCapacity(ref bytes, offset, dataLength + 6);
- bytes[offset] = MessagePackCode.Ext32;
- bytes[offset + 1] = unchecked((byte)(dataLength >> 24));
- bytes[offset + 2] = unchecked((byte)(dataLength >> 16));
- bytes[offset + 3] = unchecked((byte)(dataLength >> 8));
- bytes[offset + 4] = unchecked((byte)dataLength);
- bytes[offset + 5] = unchecked((byte)typeCode);
+ public static int WriteExtensionFormatHeaderForceExt32Block(IBufferWriter<byte> writer, sbyte typeCode, int dataLength)
+ {
+ var span = writer.GetSpan(dataLength + 6);
+ span[0] = MessagePackCode.Ext32;
+ span[1] = unchecked((byte)(dataLength >> 24));
+ span[2] = unchecked((byte)(dataLength >> 16));
+ span[3] = unchecked((byte)(dataLength >> 8));
+ span[4] = unchecked((byte)dataLength);
+ span[5] = unchecked((byte)typeCode);
return 6;
}
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteExtensionFormat(ref byte[] bytes, int offset, sbyte typeCode, byte[] data)
+ public static void WriteExtensionFormat(IBufferWriter<byte> writer, sbyte typeCode, ReadOnlySpan<byte> data)
{
var length = data.Length;
switch (length)
{
case 1:
- EnsureCapacity(ref bytes, offset, 3);
- bytes[offset] = MessagePackCode.FixExt1;
- bytes[offset + 1] = unchecked((byte)typeCode);
- bytes[offset + 2] = data[0];
- return 3;
+ var span = writer.GetSpan(3);
+ span[0] = MessagePackCode.FixExt1;
+ span[1] = unchecked((byte)typeCode);
+ span[2] = data[0];
+ writer.Advance(3);
+ return;
case 2:
- EnsureCapacity(ref bytes, offset, 4);
- bytes[offset] = MessagePackCode.FixExt2;
- bytes[offset + 1] = unchecked((byte)typeCode);
- bytes[offset + 2] = data[0];
- bytes[offset + 3] = data[1];
- return 4;
+ span = writer.GetSpan(4);
+ span[0] = MessagePackCode.FixExt2;
+ span[1] = unchecked((byte)typeCode);
+ span[2] = data[0];
+ span[3] = data[1];
+ writer.Advance(4);
+ return;
case 4:
- EnsureCapacity(ref bytes, offset, 6);
- bytes[offset] = MessagePackCode.FixExt4;
- bytes[offset + 1] = unchecked((byte)typeCode);
- bytes[offset + 2] = data[0];
- bytes[offset + 3] = data[1];
- bytes[offset + 4] = data[2];
- bytes[offset + 5] = data[3];
- return 6;
+ span = writer.GetSpan(6);
+ span[0] = MessagePackCode.FixExt4;
+ span[1] = unchecked((byte)typeCode);
+ span[2] = data[0];
+ span[3] = data[1];
+ span[4] = data[2];
+ span[5] = data[3];
+ writer.Advance(6);
+ return;
case 8:
- EnsureCapacity(ref bytes, offset, 10);
- bytes[offset] = MessagePackCode.FixExt8;
- bytes[offset + 1] = unchecked((byte)typeCode);
- bytes[offset + 2] = data[0];
- bytes[offset + 3] = data[1];
- bytes[offset + 4] = data[2];
- bytes[offset + 5] = data[3];
- bytes[offset + 6] = data[4];
- bytes[offset + 7] = data[5];
- bytes[offset + 8] = data[6];
- bytes[offset + 9] = data[7];
- return 10;
+ span = writer.GetSpan(10);
+ span[0] = MessagePackCode.FixExt8;
+ span[1] = unchecked((byte)typeCode);
+ span[2] = data[0];
+ span[3] = data[1];
+ span[4] = data[2];
+ span[5] = data[3];
+ span[6] = data[4];
+ span[7] = data[5];
+ span[8] = data[6];
+ span[9] = data[7];
+ writer.Advance(10);
+ return;
case 16:
- EnsureCapacity(ref bytes, offset, 18);
- bytes[offset] = MessagePackCode.FixExt16;
- bytes[offset + 1] = unchecked((byte)typeCode);
- bytes[offset + 2] = data[0];
- bytes[offset + 3] = data[1];
- bytes[offset + 4] = data[2];
- bytes[offset + 5] = data[3];
- bytes[offset + 6] = data[4];
- bytes[offset + 7] = data[5];
- bytes[offset + 8] = data[6];
- bytes[offset + 9] = data[7];
- bytes[offset + 10] = data[8];
- bytes[offset + 11] = data[9];
- bytes[offset + 12] = data[10];
- bytes[offset + 13] = data[11];
- bytes[offset + 14] = data[12];
- bytes[offset + 15] = data[13];
- bytes[offset + 16] = data[14];
- bytes[offset + 17] = data[15];
- return 18;
+ span = writer.GetSpan(18);
+ span[0] = MessagePackCode.FixExt16;
+ span[1] = unchecked((byte)typeCode);
+ span[2] = data[0];
+ span[3] = data[1];
+ span[4] = data[2];
+ span[5] = data[3];
+ span[6] = data[4];
+ span[7] = data[5];
+ span[8] = data[6];
+ span[9] = data[7];
+ span[10] = data[8];
+ span[11] = data[9];
+ span[12] = data[10];
+ span[13] = data[11];
+ span[14] = data[12];
+ span[15] = data[13];
+ span[16] = data[14];
+ span[17] = data[15];
+ writer.Advance(18);
+ return;
default:
unchecked
{
if (data.Length <= byte.MaxValue)
{
- EnsureCapacity(ref bytes, offset, length + 3);
- bytes[offset] = MessagePackCode.Ext8;
- bytes[offset + 1] = unchecked((byte)(length));
- bytes[offset + 2] = unchecked((byte)typeCode);
- Buffer.BlockCopy(data, 0, bytes, offset + 3, length);
- return length + 3;
+ span = writer.GetSpan(length + 3);
+ span[0] = MessagePackCode.Ext8;
+ span[1] = unchecked((byte)(length));
+ span[2] = unchecked((byte)typeCode);
+ data.Slice(0, length).CopyTo(span.Slice(3));
+ writer.Advance(length + 3);
+ return;
}
else if (data.Length <= UInt16.MaxValue)
{
- EnsureCapacity(ref bytes, offset, length + 4);
- bytes[offset] = MessagePackCode.Ext16;
- bytes[offset + 1] = unchecked((byte)(length >> 8));
- bytes[offset + 2] = unchecked((byte)(length));
- bytes[offset + 3] = unchecked((byte)typeCode);
- Buffer.BlockCopy(data, 0, bytes, offset + 4, length);
- return length + 4;
+ span = writer.GetSpan(length + 4);
+ span[0] = MessagePackCode.Ext16;
+ span[1] = unchecked((byte)(length >> 8));
+ span[2] = unchecked((byte)(length));
+ span[3] = unchecked((byte)typeCode);
+ data.Slice(0, length).CopyTo(span.Slice(4));
+ writer.Advance(length + 4);
+ return;
}
else
{
- EnsureCapacity(ref bytes, offset, length + 6);
- bytes[offset] = MessagePackCode.Ext32;
- bytes[offset + 1] = unchecked((byte)(length >> 24));
- bytes[offset + 2] = unchecked((byte)(length >> 16));
- bytes[offset + 3] = unchecked((byte)(length >> 8));
- bytes[offset + 4] = unchecked((byte)length);
- bytes[offset + 5] = unchecked((byte)typeCode);
- Buffer.BlockCopy(data, 0, bytes, offset + 6, length);
- return length + 6;
+ span = writer.GetSpan(length + 6);
+ span[0] = MessagePackCode.Ext32;
+ span[1] = unchecked((byte)(length >> 24));
+ span[2] = unchecked((byte)(length >> 16));
+ span[3] = unchecked((byte)(length >> 8));
+ span[4] = unchecked((byte)length);
+ span[5] = unchecked((byte)typeCode);
+ data.Slice(0, length).CopyTo(span.Slice(6));
+ writer.Advance(length + 6);
+ return;
}
}
}
@@ -2129,9 +1828,9 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static ExtensionResult ReadExtensionFormat(byte[] bytes, int offset, out int readSize)
+ public static ExtensionResult ReadExtensionFormat(ref ReadOnlySequence<byte> byteSequence)
{
- return extDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return extDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
/// <summary>
@@ -2140,9 +1839,9 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static ExtensionHeader ReadExtensionFormatHeader(byte[] bytes, int offset, out int readSize)
+ public static ExtensionHeader ReadExtensionFormatHeader(ref ReadOnlySequence<byte> byteSequence)
{
- return extHeaderDecoders[bytes[offset]].Read(bytes, offset, out readSize);
+ return extHeaderDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
#if NETSTANDARD || NETFRAMEWORK
@@ -2183,7 +1882,7 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static int WriteDateTime(ref byte[] bytes, int offset, DateTime dateTime)
+ public static int WriteDateTime(IBufferWriter<byte> writer, DateTime dateTime)
{
dateTime = dateTime.ToUniversalTime();
@@ -2226,51 +1925,51 @@ namespace MessagePack
{
// timestamp 32(seconds in 32-bit unsigned int)
var data32 = (UInt32)data64;
- EnsureCapacity(ref bytes, offset, 6);
- bytes[offset] = MessagePackCode.FixExt4;
- bytes[offset + 1] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime);
- bytes[offset + 2] = unchecked((byte)(data32 >> 24));
- bytes[offset + 3] = unchecked((byte)(data32 >> 16));
- bytes[offset + 4] = unchecked((byte)(data32 >> 8));
- bytes[offset + 5] = unchecked((byte)data32);
+ var span = writer.GetSpan(6);
+ span[0] = MessagePackCode.FixExt4;
+ span[1] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime);
+ span[2] = unchecked((byte)(data32 >> 24));
+ span[3] = unchecked((byte)(data32 >> 16));
+ span[4] = unchecked((byte)(data32 >> 8));
+ span[5] = unchecked((byte)data32);
return 6;
}
else
{
// timestamp 64(nanoseconds in 30-bit unsigned int | seconds in 34-bit unsigned int)
- EnsureCapacity(ref bytes, offset, 10);
- bytes[offset] = MessagePackCode.FixExt8;
- bytes[offset + 1] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime);
- bytes[offset + 2] = unchecked((byte)(data64 >> 56));
- bytes[offset + 3] = unchecked((byte)(data64 >> 48));
- bytes[offset + 4] = unchecked((byte)(data64 >> 40));
- bytes[offset + 5] = unchecked((byte)(data64 >> 32));
- bytes[offset + 6] = unchecked((byte)(data64 >> 24));
- bytes[offset + 7] = unchecked((byte)(data64 >> 16));
- bytes[offset + 8] = unchecked((byte)(data64 >> 8));
- bytes[offset + 9] = unchecked((byte)data64);
+ var span = writer.GetSpan(10);
+ span[0] = MessagePackCode.FixExt8;
+ span[1] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime);
+ span[2] = unchecked((byte)(data64 >> 56));
+ span[3] = unchecked((byte)(data64 >> 48));
+ span[4] = unchecked((byte)(data64 >> 40));
+ span[5] = unchecked((byte)(data64 >> 32));
+ span[6] = unchecked((byte)(data64 >> 24));
+ span[7] = unchecked((byte)(data64 >> 16));
+ span[8] = unchecked((byte)(data64 >> 8));
+ span[9] = unchecked((byte)data64);
return 10;
}
}
else
{
// timestamp 96( nanoseconds in 32-bit unsigned int | seconds in 64-bit signed int )
- EnsureCapacity(ref bytes, offset, 15);
- bytes[offset] = MessagePackCode.Ext8;
- bytes[offset + 1] = (byte)12;
- bytes[offset + 2] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime);
- bytes[offset + 3] = unchecked((byte)(nanoseconds >> 24));
- bytes[offset + 4] = unchecked((byte)(nanoseconds >> 16));
- bytes[offset + 5] = unchecked((byte)(nanoseconds >> 8));
- bytes[offset + 6] = unchecked((byte)nanoseconds);
- bytes[offset + 7] = unchecked((byte)(seconds >> 56));
- bytes[offset + 8] = unchecked((byte)(seconds >> 48));
- bytes[offset + 9] = unchecked((byte)(seconds >> 40));
- bytes[offset + 10] = unchecked((byte)(seconds >> 32));
- bytes[offset + 11] = unchecked((byte)(seconds >> 24));
- bytes[offset + 12] = unchecked((byte)(seconds >> 16));
- bytes[offset + 13] = unchecked((byte)(seconds >> 8));
- bytes[offset + 14] = unchecked((byte)seconds);
+ var span = writer.GetSpan(15);
+ span[0] = MessagePackCode.Ext8;
+ span[1] = 12;
+ span[2] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime);
+ span[3] = unchecked((byte)(nanoseconds >> 24));
+ span[4] = unchecked((byte)(nanoseconds >> 16));
+ span[5] = unchecked((byte)(nanoseconds >> 8));
+ span[6] = unchecked((byte)nanoseconds);
+ span[7] = unchecked((byte)(seconds >> 56));
+ span[8] = unchecked((byte)(seconds >> 48));
+ span[9] = unchecked((byte)(seconds >> 40));
+ span[10] = unchecked((byte)(seconds >> 32));
+ span[11] = unchecked((byte)(seconds >> 24));
+ span[12] = unchecked((byte)(seconds >> 16));
+ span[13] = unchecked((byte)(seconds >> 8));
+ span[14] = unchecked((byte)seconds);
return 15;
}
}
@@ -2278,1084 +1977,38 @@ namespace MessagePack
#if NETSTANDARD || NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
#endif
- public static DateTime ReadDateTime(byte[] bytes, int offset, out int readSize)
- {
- return dateTimeDecoders[bytes[offset]].Read(bytes, offset, out readSize);
- }
- }
-
- // Stream Overload
- public static partial class MessagePackBinary
- {
- static class StreamDecodeMemoryPool
+ public static DateTime ReadDateTime(ref ReadOnlySequence<byte> byteSequence)
{
- [ThreadStatic]
- static byte[] buffer = null;
-
- public static byte[] GetBuffer()
- {
- if (buffer == null)
- {
- buffer = new byte[65536];
- }
- return buffer;
- }
+ return dateTimeDecoders[byteSequence.First.Span[0]].Read(ref byteSequence);
}
- static byte[] ReadMessageBlockFromStreamUnsafe(Stream stream)
- {
- int _;
- return ReadMessageBlockFromStreamUnsafe(stream, false, out _);
- }
+ internal delegate T ContiguousMemoryReader<T>(ReadOnlySpan<byte> span);
- /// <summary>
- /// Read MessageBlock, returns byte[] block is in MemoryPool so careful to use.
- /// </summary>
- public static byte[] ReadMessageBlockFromStreamUnsafe(Stream stream, bool readOnlySingleMessage, out int readSize)
+ internal static T Read<T>(ref ReadOnlySequence<byte> byteSequence, int length, ContiguousMemoryReader<T> reader)
{
- var bytes = StreamDecodeMemoryPool.GetBuffer();
- readSize = ReadMessageBlockFromStreamCore(stream, ref bytes, 0, readOnlySingleMessage);
- return bytes;
- }
-
- static int ReadMessageBlockFromStreamCore(Stream stream, ref byte[] bytes, int offset, bool readOnlySingleMessage)
- {
- var byteCode = stream.ReadByte();
- if (byteCode < 0 || byte.MaxValue < byteCode)
- {
- throw new InvalidOperationException("Invalid MessagePack code was detected, code:" + byteCode);
- }
-
- var code = (byte)byteCode;
-
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 1);
- bytes[offset] = code;
-
- var type = MessagePackCode.ToMessagePackType(code);
- switch (type)
- {
- case MessagePackType.Integer:
- {
- var readCount = 0;
- if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) return 1;
- else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) return 1;
-
- switch (code)
- {
- case MessagePackCode.Int8: readCount = 1; break;
- case MessagePackCode.Int16: readCount = 2; break;
- case MessagePackCode.Int32: readCount = 4; break;
- case MessagePackCode.Int64: readCount = 8; break;
- case MessagePackCode.UInt8: readCount = 1; break;
- case MessagePackCode.UInt16: readCount = 2; break;
- case MessagePackCode.UInt32: readCount = 4; break;
- case MessagePackCode.UInt64: readCount = 8; break;
- default: throw new InvalidOperationException("Invalid Code");
- }
-
- MessagePackBinary.EnsureCapacity(ref bytes, offset, readCount + 1);
- ReadFully(stream, bytes, offset + 1, readCount);
- return readCount + 1;
- }
- case MessagePackType.Unknown:
- case MessagePackType.Nil:
- case MessagePackType.Boolean:
- return 1;
- case MessagePackType.Float:
- if (code == MessagePackCode.Float32)
- {
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 5);
- ReadFully(stream, bytes, offset + 1, 4);
- return 5;
- }
- else
- {
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 9);
- ReadFully(stream, bytes, offset + 1, 8);
- return 9;
- }
- case MessagePackType.String:
- {
- if (MessagePackCode.MinFixStr <= code && code <= MessagePackCode.MaxFixStr)
- {
- var length = bytes[offset] & 0x1F;
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 1 + length);
- ReadFully(stream, bytes, offset + 1, length);
- return length + 1;
- }
-
- switch (code)
- {
- case MessagePackCode.Str8:
- {
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 2);
- ReadFully(stream, bytes, offset + 1, 1);
- var length = bytes[offset + 1];
-
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 2 + length);
- ReadFully(stream, bytes, offset + 2, length);
-
- return length + 2;
- }
- case MessagePackCode.Str16:
- {
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 3);
- ReadFully(stream, bytes, offset + 1, 2);
- var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]);
-
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 3 + length);
- ReadFully(stream, bytes, offset + 3, length);
-
- return length + 3;
- }
- case MessagePackCode.Str32:
- {
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 5);
- ReadFully(stream, bytes, offset + 1, 4);
- var length = (bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | (bytes[offset + 4]);
-
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 5 + length);
- ReadFully(stream, bytes, offset + 5, length);
-
- return length + 5;
- }
- default: throw new InvalidOperationException("Invalid Code");
- }
- }
- case MessagePackType.Binary:
- {
- switch (code)
- {
- case MessagePackCode.Bin8:
- {
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 2);
- ReadFully(stream, bytes, offset + 1, 1);
- var length = bytes[offset + 1];
-
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 2 + length);
- ReadFully(stream, bytes, offset + 2, length);
-
- return length + 2;
- }
- case MessagePackCode.Bin16:
- {
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 3);
- ReadFully(stream, bytes, offset + 1, 2);
- var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]);
-
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 3 + length);
- ReadFully(stream, bytes, offset + 3, length);
-
- return length + 3;
- }
- case MessagePackCode.Bin32:
- {
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 5);
- ReadFully(stream, bytes, offset + 1, 4);
- var length = (bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | (bytes[offset + 4]);
-
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 5 + length);
- ReadFully(stream, bytes, offset + 5, length);
-
- return length + 5;
- }
- default: throw new InvalidOperationException("Invalid Code");
- }
- }
- case MessagePackType.Array:
- {
- var readHeaderSize = 0;
-
- if (MessagePackCode.MinFixArray <= code && code <= MessagePackCode.MaxFixArray) readHeaderSize = 0;
- else if (code == MessagePackCode.Array16) readHeaderSize = 2;
- else if (code == MessagePackCode.Array32) readHeaderSize = 4;
- if (readHeaderSize != 0)
- {
- MessagePackBinary.EnsureCapacity(ref bytes, offset, readHeaderSize + 1);
- ReadFully(stream, bytes, offset + 1, readHeaderSize);
- }
-
- var startOffset = offset;
- offset += (readHeaderSize + 1);
-
- int _;
- var length = ReadArrayHeaderRaw(bytes, startOffset, out _);
- if (!readOnlySingleMessage)
- {
- for (int i = 0; i < length; i++)
- {
- offset += ReadMessageBlockFromStreamCore(stream, ref bytes, offset, readOnlySingleMessage);
- }
- }
-
- return offset - startOffset;
- }
- case MessagePackType.Map:
- {
- var readHeaderSize = 0;
-
- if (MessagePackCode.MinFixMap <= code && code <= MessagePackCode.MaxFixMap) readHeaderSize = 0;
- else if (code == MessagePackCode.Map16) readHeaderSize = 2;
- else if (code == MessagePackCode.Map32) readHeaderSize = 4;
- if (readHeaderSize != 0)
- {
- MessagePackBinary.EnsureCapacity(ref bytes, offset, readHeaderSize + 1);
- ReadFully(stream, bytes, offset + 1, readHeaderSize);
- }
-
- var startOffset = offset;
- offset += (readHeaderSize + 1);
-
- int _;
- var length = ReadMapHeaderRaw(bytes, startOffset, out _);
- if (!readOnlySingleMessage)
- {
- for (int i = 0; i < length; i++)
- {
- offset += ReadMessageBlockFromStreamCore(stream, ref bytes, offset, readOnlySingleMessage); // key
- offset += ReadMessageBlockFromStreamCore(stream, ref bytes, offset, readOnlySingleMessage); // value
- }
- }
-
- return offset - startOffset;
- }
- case MessagePackType.Extension:
- {
- var readHeaderSize = 0;
-
- switch (code)
- {
- case MessagePackCode.FixExt1: readHeaderSize = 1; break;
- case MessagePackCode.FixExt2: readHeaderSize = 1; break;
- case MessagePackCode.FixExt4: readHeaderSize = 1; break;
- case MessagePackCode.FixExt8: readHeaderSize = 1; break;
- case MessagePackCode.FixExt16: readHeaderSize = 1; break;
- case MessagePackCode.Ext8: readHeaderSize = 2; break;
- case MessagePackCode.Ext16: readHeaderSize = 3; break;
- case MessagePackCode.Ext32: readHeaderSize = 5; break;
- default: throw new InvalidOperationException("Invalid Code");
- }
-
- MessagePackBinary.EnsureCapacity(ref bytes, offset, readHeaderSize + 1);
- ReadFully(stream, bytes, offset + 1, readHeaderSize);
-
- if (!readOnlySingleMessage)
- {
- int _;
- var header = ReadExtensionFormatHeader(bytes, offset, out _);
-
- MessagePackBinary.EnsureCapacity(ref bytes, offset, 1 + readHeaderSize + (int)header.Length);
- ReadFully(stream, bytes, offset + 1 + readHeaderSize, (int)header.Length);
-
- return 1 + readHeaderSize + (int)header.Length;
- }
- else
- {
- return readHeaderSize + 1;
- }
- }
- default: throw new InvalidOperationException("Invalid Code");
- }
- }
-
- static void ReadFully(Stream stream, byte[] bytes, int offset, int readSize)
- {
- var nextLen = readSize;
- while (nextLen != 0)
- {
- var len = stream.Read(bytes, offset, nextLen);
- if (len == -1) return;
- offset += len;
- nextLen = nextLen - len;
- }
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int ReadNext(Stream stream)
- {
- var bytes = StreamDecodeMemoryPool.GetBuffer();
- return ReadMessageBlockFromStreamCore(stream, ref bytes, 0, true);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int ReadNextBlock(Stream stream)
- {
- var bytes = StreamDecodeMemoryPool.GetBuffer();
- var offset = 0;
- return ReadMessageBlockFromStreamCore(stream, ref bytes, offset, false);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteNil(Stream stream)
- {
- stream.WriteByte(MessagePackCode.Nil);
- return 1;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static Nil ReadNil(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadNil(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static bool IsNil(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
-
- return bytes[offset] == MessagePackCode.Nil;
- }
-
- /// <summary>
- /// Unsafe. If value is guranteed 0 ~ MessagePackRange.MaxFixMapCount(15), can use this method.
- /// </summary>
- /// <returns></returns>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteFixedMapHeaderUnsafe(Stream stream, int count)
- {
- stream.WriteByte((byte)(MessagePackCode.MinFixMap | count));
- return 1;
- }
-
- /// <summary>
- /// Write map count.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteMapHeader(Stream stream, int count)
- {
- checked
+ const int StackAllocLimit = 64 * 1024;
+ T result;
+ if (byteSequence.First.Length >= length)
{
- return WriteMapHeader(stream, (uint)count);
+ result = reader(byteSequence.First.Span);
}
- }
-
- /// <summary>
- /// Write map count.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteMapHeader(Stream stream, uint count)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteMapHeader(ref buffer, 0, count);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
- /// <summary>
- /// Write map format header, always use map32 format(length is fixed, 5).
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteMapHeaderForceMap32Block(Stream stream, uint count)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteMapHeaderForceMap32Block(ref buffer, 0, count);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
- /// <summary>
- /// Return map count.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int ReadMapHeader(Stream stream)
- {
- checked
+ else if (length <= StackAllocLimit)
{
- var bytes = StreamDecodeMemoryPool.GetBuffer();
- ReadMessageBlockFromStreamCore(stream, ref bytes, 0, true);
- int readSize;
- return ReadMapHeader(bytes, 0, out readSize);
+ Span<byte> span = stackalloc byte[length];
+ byteSequence.Slice(0, length).CopyTo(span);
+ result = reader(span);
}
- }
-
- /// <summary>
- /// Return map count.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static uint ReadMapHeaderRaw(Stream stream)
- {
- var bytes = StreamDecodeMemoryPool.GetBuffer();
- ReadMessageBlockFromStreamCore(stream, ref bytes, 0, true);
- int readSize;
- return ReadMapHeaderRaw(bytes, 0, out readSize);
- }
-
- /// <summary>
- /// Unsafe. If value is guranteed 0 ~ MessagePackRange.MaxFixArrayCount(15), can use this method.
- /// </summary>
- /// <returns></returns>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteFixedArrayHeaderUnsafe(Stream stream, int count)
- {
- stream.WriteByte((byte)(MessagePackCode.MinFixArray | count));
- return 1;
- }
-
- /// <summary>
- /// Write array count.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteArrayHeader(Stream stream, int count)
- {
- checked
+ else
{
- return WriteArrayHeader(stream, (uint)count);
+ using (var rental = MemoryPool<byte>.Shared.Rent(length))
+ {
+ byteSequence.Slice(0, length).CopyTo(rental.Memory.Span);
+ result = reader(rental.Memory.Span.Slice(0, length));
+ }
}
- }
-
- /// <summary>
- /// Write array count.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteArrayHeader(Stream stream, uint count)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteArrayHeader(ref buffer, 0, count);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
- /// <summary>
- /// Write array format header, always use array32 format(length is fixed, 5).
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteArrayHeaderForceArray32Block(Stream stream, uint count)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteArrayHeaderForceArray32Block(ref buffer, 0, count);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
- /// <summary>
- /// Return array count.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int ReadArrayHeader(Stream stream)
- {
- var bytes = StreamDecodeMemoryPool.GetBuffer();
- ReadMessageBlockFromStreamCore(stream, ref bytes, 0, true);
- int readSize;
- return ReadArrayHeader(bytes, 0, out readSize);
- }
- /// <summary>
- /// Return array count.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static uint ReadArrayHeaderRaw(Stream stream)
- {
- var bytes = StreamDecodeMemoryPool.GetBuffer();
- ReadMessageBlockFromStreamCore(stream, ref bytes, 0, true);
- int readSize;
- return ReadArrayHeaderRaw(bytes, 0, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteBoolean(Stream stream, bool value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteBoolean(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static bool ReadBoolean(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadBoolean(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteByte(Stream stream, byte value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteByte(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteByteForceByteBlock(Stream stream, byte value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteByteForceByteBlock(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static byte ReadByte(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadByte(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteBytes(Stream stream, byte[] value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteBytes(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteBytes(Stream stream, byte[] src, int srcOffset, int count)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteBytes(ref buffer, 0, src, srcOffset, count);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static byte[] ReadBytes(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadBytes(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteSByte(Stream stream, sbyte value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteSByte(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteSByteForceSByteBlock(Stream stream, sbyte value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteSByteForceSByteBlock(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static sbyte ReadSByte(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadSByte(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteSingle(Stream stream, float value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteSingle(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static float ReadSingle(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadSingle(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteDouble(Stream stream, double value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteDouble(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static double ReadDouble(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadDouble(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteInt16(Stream stream, short value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteInt16(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteInt16ForceInt16Block(Stream stream, short value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteInt16ForceInt16Block(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static short ReadInt16(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadInt16(bytes, offset, out readSize);
- }
-
- /// <summary>
- /// Unsafe. If value is guranteed 0 ~ MessagePackCode.MaxFixInt(127), can use this method.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WritePositiveFixedIntUnsafe(Stream stream, int value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WritePositiveFixedIntUnsafe(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteInt32(Stream stream, int value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteInt32(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
- /// <summary>
- /// Acquire static message block(always 5 bytes).
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteInt32ForceInt32Block(Stream stream, int value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteInt32ForceInt32Block(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int ReadInt32(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadInt32(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteInt64(Stream stream, long value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteInt64(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteInt64ForceInt64Block(Stream stream, long value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteInt64ForceInt64Block(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static long ReadInt64(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadInt64(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteUInt16(Stream stream, ushort value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteUInt16(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteUInt16ForceUInt16Block(Stream stream, ushort value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteUInt16ForceUInt16Block(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static ushort ReadUInt16(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadUInt16(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteUInt32(Stream stream, uint value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteUInt32(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteUInt32ForceUInt32Block(Stream stream, uint value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteUInt32ForceUInt32Block(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static uint ReadUInt32(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadUInt32(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteUInt64(Stream stream, ulong value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteUInt64(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteUInt64ForceUInt64Block(Stream stream, ulong value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteUInt64ForceUInt64Block(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static ulong ReadUInt64(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadUInt64(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteChar(Stream stream, char value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteChar(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static char ReadChar(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadChar(bytes, offset, out readSize);
- }
-
- /// <summary>
- /// Unsafe. If value is guranteed length is 0 ~ 31, can use this method.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteFixedStringUnsafe(Stream stream, string value, int byteCount)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteFixedStringUnsafe(ref buffer, 0, value, byteCount);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
- /// <summary>
- /// Unsafe. If pre-calculated byteCount of target string, can use this method.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteStringUnsafe(Stream stream, string value, int byteCount)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteStringUnsafe(ref buffer, 0, value, byteCount);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteStringBytes(Stream stream, byte[] utf8stringBytes)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteStringBytes(ref buffer, 0, utf8stringBytes);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
- public static int WriteString(Stream stream, string value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteString(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
- public static int WriteStringForceStr32Block(Stream stream, string value)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteStringForceStr32Block(ref buffer, 0, value);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static string ReadString(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadString(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteExtensionFormatHeader(Stream stream, sbyte typeCode, int dataLength)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteExtensionFormatHeader(ref buffer, 0, typeCode, dataLength);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
- /// <summary>
- /// Write extension format header, always use ext32 format(length is fixed, 6).
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteExtensionFormatHeaderForceExt32Block(Stream stream, sbyte typeCode, int dataLength)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteExtensionFormatHeaderForceExt32Block(ref buffer, 0, typeCode, dataLength);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteExtensionFormat(Stream stream, sbyte typeCode, byte[] data)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteExtensionFormat(ref buffer, 0, typeCode, data);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static ExtensionResult ReadExtensionFormat(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadExtensionFormat(bytes, offset, out readSize);
- }
-
- /// <summary>
- /// return byte length of ExtensionFormat.
- /// </summary>
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static ExtensionHeader ReadExtensionFormatHeader(Stream stream)
- {
- int readSize;
- var bytes = ReadMessageBlockFromStreamUnsafe(stream, true, out readSize);
- var offset = 0;
-
- return ReadExtensionFormatHeader(bytes, offset, out readSize);
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static int WriteDateTime(Stream stream, DateTime dateTime)
- {
- var buffer = StreamDecodeMemoryPool.GetBuffer();
- var writeCount = WriteDateTime(ref buffer, 0, dateTime);
- stream.Write(buffer, 0, writeCount);
- return writeCount;
- }
-
-#if NETSTANDARD || NETFRAMEWORK
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-#endif
- public static DateTime ReadDateTime(Stream stream)
- {
- var bytes = ReadMessageBlockFromStreamUnsafe(stream);
- var offset = 0;
- int readSize;
-
- return ReadDateTime(bytes, offset, out readSize);
+ byteSequence = byteSequence.Slice(length);
+ return result;
}
}
@@ -3398,22 +2051,23 @@ namespace MessagePack.Decoders
{
internal interface IMapHeaderDecoder
{
- uint Read(byte[] bytes, int offset, out int readSize);
+ uint Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixMapHeader : IMapHeaderDecoder
{
internal static readonly IMapHeaderDecoder Instance = new FixMapHeader();
- FixMapHeader()
+ private FixMapHeader()
{
}
- public uint Read(byte[] bytes, int offset, out int readSize)
+ public uint Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return (uint)(bytes[offset] & 0xF);
+ uint result = (uint)(byteSequence.First.Span[0] & 0xF);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -3421,18 +2075,14 @@ namespace MessagePack.Decoders
{
internal static readonly IMapHeaderDecoder Instance = new Map16Header();
- Map16Header()
+ private Map16Header()
{
}
- public uint Read(byte[] bytes, int offset, out int readSize)
+ public uint Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 3;
- unchecked
- {
- return (uint)((bytes[offset + 1] << 8) | (bytes[offset + 2]));
- }
+ return MessagePackBinary.Read(ref byteSequence, 3, span => unchecked((uint)((span[1] << 8) | (span[2]))));
}
}
@@ -3440,18 +2090,14 @@ namespace MessagePack.Decoders
{
internal static readonly IMapHeaderDecoder Instance = new Map32Header();
- Map32Header()
+ private Map32Header()
{
}
- public uint Read(byte[] bytes, int offset, out int readSize)
+ public uint Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 5;
- unchecked
- {
- return (uint)((bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | bytes[offset + 4]);
- }
+ return MessagePackBinary.Read(ref byteSequence, 5, span => unchecked((uint)((span[1] << 24) | (span[2] << 16) | (span[3] << 8) | span[4])));
}
}
@@ -3459,35 +2105,36 @@ namespace MessagePack.Decoders
{
internal static readonly IMapHeaderDecoder Instance = new InvalidMapHeader();
- InvalidMapHeader()
+ private InvalidMapHeader()
{
}
- public uint Read(byte[] bytes, int offset, out int readSize)
+ public uint Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IArrayHeaderDecoder
{
- uint Read(byte[] bytes, int offset, out int readSize);
+ uint Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixArrayHeader : IArrayHeaderDecoder
{
internal static readonly IArrayHeaderDecoder Instance = new FixArrayHeader();
- FixArrayHeader()
+ private FixArrayHeader()
{
}
- public uint Read(byte[] bytes, int offset, out int readSize)
+ public uint Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return (uint)(bytes[offset] & 0xF);
+ uint result = (uint)(byteSequence.First.Span[0] & 0xF);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -3495,18 +2142,14 @@ namespace MessagePack.Decoders
{
internal static readonly IArrayHeaderDecoder Instance = new Array16Header();
- Array16Header()
+ private Array16Header()
{
}
- public uint Read(byte[] bytes, int offset, out int readSize)
+ public uint Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 3;
- unchecked
- {
- return (uint)((bytes[offset + 1] << 8) | (bytes[offset + 2]));
- }
+ return MessagePackBinary.Read(ref byteSequence, 3, span => unchecked((uint)((span[1] << 8) | (span[2]))));
}
}
@@ -3514,18 +2157,14 @@ namespace MessagePack.Decoders
{
internal static readonly IArrayHeaderDecoder Instance = new Array32Header();
- Array32Header()
+ private Array32Header()
{
}
- public uint Read(byte[] bytes, int offset, out int readSize)
+ public uint Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 5;
- unchecked
- {
- return (uint)((bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | bytes[offset + 4]);
- }
+ return MessagePackBinary.Read(ref byteSequence, 5, span => unchecked((uint)((span[1] << 24) | (span[2] << 16) | (span[3] << 8) | span[4])));
}
}
@@ -3533,14 +2172,14 @@ namespace MessagePack.Decoders
{
internal static readonly IArrayHeaderDecoder Instance = new InvalidArrayHeader();
- InvalidArrayHeader()
+ private InvalidArrayHeader()
{
}
- public uint Read(byte[] bytes, int offset, out int readSize)
+ public uint Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
@@ -3553,7 +2192,7 @@ namespace MessagePack.Decoders
{
internal static IBooleanDecoder Instance = new True();
- True() { }
+ private True() { }
public bool Read()
{
@@ -3565,7 +2204,7 @@ namespace MessagePack.Decoders
{
internal static IBooleanDecoder Instance = new False();
- False() { }
+ private False() { }
public bool Read()
{
@@ -3577,7 +2216,7 @@ namespace MessagePack.Decoders
{
internal static IBooleanDecoder Instance = new InvalidBoolean();
- InvalidBoolean() { }
+ private InvalidBoolean() { }
public bool Read()
{
@@ -3587,22 +2226,23 @@ namespace MessagePack.Decoders
internal interface IByteDecoder
{
- byte Read(byte[] bytes, int offset, out int readSize);
+ byte Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixByte : IByteDecoder
{
internal static readonly IByteDecoder Instance = new FixByte();
- FixByte()
+ private FixByte()
{
}
- public byte Read(byte[] bytes, int offset, out int readSize)
+ public byte Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return bytes[offset];
+ byte result = byteSequence.First.Span[0];
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -3610,15 +2250,14 @@ namespace MessagePack.Decoders
{
internal static readonly IByteDecoder Instance = new UInt8Byte();
- UInt8Byte()
+ private UInt8Byte()
{
}
- public byte Read(byte[] bytes, int offset, out int readSize)
+ public byte Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 2;
- return bytes[offset + 1];
+ return MessagePackBinary.Read(ref byteSequence, 2, span => span[1]);
}
}
@@ -3626,34 +2265,34 @@ namespace MessagePack.Decoders
{
internal static readonly IByteDecoder Instance = new InvalidByte();
- InvalidByte()
+ private InvalidByte()
{
}
- public byte Read(byte[] bytes, int offset, out int readSize)
+ public byte Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IBytesDecoder
{
- byte[] Read(byte[] bytes, int offset, out int readSize);
+ byte[] Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class NilBytes : IBytesDecoder
{
internal static readonly IBytesDecoder Instance = new NilBytes();
- NilBytes()
+ private NilBytes()
{
}
- public byte[] Read(byte[] bytes, int offset, out int readSize)
+ public byte[] Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
+ byteSequence = byteSequence.Slice(1);
return null;
}
}
@@ -3662,18 +2301,17 @@ namespace MessagePack.Decoders
{
internal static readonly IBytesDecoder Instance = new Bin8Bytes();
- Bin8Bytes()
+ private Bin8Bytes()
{
}
- public byte[] Read(byte[] bytes, int offset, out int readSize)
+ public byte[] Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = bytes[offset + 1];
+ var length = MessagePackBinary.Read(ref byteSequence, 2, span => span[1]);
var newBytes = new byte[length];
- Buffer.BlockCopy(bytes, offset + 2, newBytes, 0, length);
-
- readSize = length + 2;
+ byteSequence.Slice(0, length).CopyTo(newBytes);
+ byteSequence = byteSequence.Slice(length);
return newBytes;
}
}
@@ -3682,18 +2320,17 @@ namespace MessagePack.Decoders
{
internal static readonly IBytesDecoder Instance = new Bin16Bytes();
- Bin16Bytes()
+ private Bin16Bytes()
{
}
- public byte[] Read(byte[] bytes, int offset, out int readSize)
+ public byte[] Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]);
+ var length = MessagePackBinary.Read(ref byteSequence, 3, span => (span[1] << 8) + (span[2]));
var newBytes = new byte[length];
- Buffer.BlockCopy(bytes, offset + 3, newBytes, 0, length);
-
- readSize = length + 3;
+ byteSequence.Slice(0, length).CopyTo(newBytes);
+ byteSequence = byteSequence.Slice(length);
return newBytes;
}
}
@@ -3702,18 +2339,17 @@ namespace MessagePack.Decoders
{
internal static readonly IBytesDecoder Instance = new Bin32Bytes();
- Bin32Bytes()
+ private Bin32Bytes()
{
}
- public byte[] Read(byte[] bytes, int offset, out int readSize)
+ public byte[] Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = (bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | (bytes[offset + 4]);
+ var length = MessagePackBinary.Read(ref byteSequence, 5, span => (span[1] << 24) | (span[2] << 16) | (span[3] << 8) | (span[4]));
var newBytes = new byte[length];
- Buffer.BlockCopy(bytes, offset + 5, newBytes, 0, length);
-
- readSize = length + 5;
+ byteSequence.Slice(0, length).CopyTo(newBytes);
+ byteSequence = byteSequence.Slice(length);
return newBytes;
}
}
@@ -3722,34 +2358,34 @@ namespace MessagePack.Decoders
{
internal static readonly IBytesDecoder Instance = new InvalidBytes();
- InvalidBytes()
+ private InvalidBytes()
{
}
- public byte[] Read(byte[] bytes, int offset, out int readSize)
+ public byte[] Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IBytesSegmentDecoder
{
- ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize);
+ ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class NilBytesSegment : IBytesSegmentDecoder
{
internal static readonly IBytesSegmentDecoder Instance = new NilBytesSegment();
- NilBytesSegment()
+ private NilBytesSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
+ byteSequence = byteSequence.Slice(1);
return default(ArraySegment<byte>);
}
}
@@ -3758,17 +2394,40 @@ namespace MessagePack.Decoders
{
internal static readonly IBytesSegmentDecoder Instance = new Bin8BytesSegment();
- Bin8BytesSegment()
+ private Bin8BytesSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = bytes[offset + 1];
+ return Read(ref byteSequence, 2, span => span[1]);
+ }
- readSize = length + 2;
- return new ArraySegment<byte>(bytes, offset + 2, length);
+ internal delegate int SegmentLengthReader(ReadOnlySpan<byte> span);
+
+ internal static ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence, int lengthOfLengthHeader, SegmentLengthReader readLength)
+ {
+ Span<byte> lengthHeaderSpan = stackalloc byte[lengthOfLengthHeader];
+ byteSequence.Slice(0, lengthOfLengthHeader).CopyTo(lengthHeaderSpan);
+ byteSequence = byteSequence.Slice(lengthOfLengthHeader);
+
+ int length = readLength(lengthHeaderSpan);
+ ArraySegment<byte> result;
+ if (byteSequence.First.Length >= length && MemoryMarshal.TryGetArray(byteSequence.First, out ArraySegment<byte> segment))
+ {
+ // Everything we need to return happens to already be in a single array. Return a segment into that same array.
+ result = new ArraySegment<byte>(segment.Array, segment.Offset, length);
+ }
+ else
+ {
+ // We need to create a new array in order to return a continguous segment in our result.
+ result = new ArraySegment<byte>(new byte[length]);
+ byteSequence.Slice(0, length).CopyTo(result.Array);
+ }
+
+ byteSequence = byteSequence.Slice(length);
+ return result;
}
}
@@ -3776,34 +2435,30 @@ namespace MessagePack.Decoders
{
internal static readonly IBytesSegmentDecoder Instance = new Bin16BytesSegment();
- Bin16BytesSegment()
+ private Bin16BytesSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]);
-
- readSize = length + 3;
- return new ArraySegment<byte>(bytes, offset + 3, length);
+ return Bin8BytesSegment.Read(ref byteSequence, 3, span => (span[1] << 8) + (span[2]));
}
+
}
internal sealed class Bin32BytesSegment : IBytesSegmentDecoder
{
internal static readonly IBytesSegmentDecoder Instance = new Bin32BytesSegment();
- Bin32BytesSegment()
+ private Bin32BytesSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = (bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | (bytes[offset + 4]);
- readSize = length + 5;
- return new ArraySegment<byte>(bytes, offset + 5, length);
+ return Bin8BytesSegment.Read(ref byteSequence, 5, span => (span[1] << 24) | (span[2] << 16) | (span[3] << 8) | (span[4]));
}
}
@@ -3811,35 +2466,36 @@ namespace MessagePack.Decoders
{
internal static readonly IBytesSegmentDecoder Instance = new InvalidBytesSegment();
- InvalidBytesSegment()
+ private InvalidBytesSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface ISByteDecoder
{
- sbyte Read(byte[] bytes, int offset, out int readSize);
+ sbyte Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixSByte : ISByteDecoder
{
internal static readonly ISByteDecoder Instance = new FixSByte();
- FixSByte()
+ private FixSByte()
{
}
- public sbyte Read(byte[] bytes, int offset, out int readSize)
+ public sbyte Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return unchecked((sbyte)bytes[offset]);
+ sbyte result = unchecked((sbyte)byteSequence.First.Span[0]);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -3847,15 +2503,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISByteDecoder Instance = new Int8SByte();
- Int8SByte()
+ private Int8SByte()
{
}
- public sbyte Read(byte[] bytes, int offset, out int readSize)
+ public sbyte Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 2;
- return unchecked((sbyte)(bytes[offset + 1]));
+ return MessagePackBinary.Read(ref byteSequence, 2, span => (sbyte)span[1]);
}
}
@@ -3863,34 +2518,34 @@ namespace MessagePack.Decoders
{
internal static readonly ISByteDecoder Instance = new InvalidSByte();
- InvalidSByte()
+ private InvalidSByte()
{
}
- public sbyte Read(byte[] bytes, int offset, out int readSize)
+ public sbyte Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface ISingleDecoder
{
- float Read(byte[] bytes, int offset, out int readSize);
+ float Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixNegativeFloat : ISingleDecoder
{
internal static readonly ISingleDecoder Instance = new FixNegativeFloat();
- FixNegativeFloat()
+ private FixNegativeFloat()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- return FixSByte.Instance.Read(bytes, offset, out readSize);
+ return FixSByte.Instance.Read(ref byteSequence);
}
}
@@ -3898,14 +2553,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new FixFloat();
- FixFloat()
+ private FixFloat()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- return FixByte.Instance.Read(bytes, offset, out readSize);
+ return FixByte.Instance.Read(ref byteSequence);
}
}
@@ -3913,14 +2568,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new Int8Single();
- Int8Single()
+ private Int8Single()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- return Int8SByte.Instance.Read(bytes, offset, out readSize);
+ return Int8SByte.Instance.Read(ref byteSequence);
}
}
@@ -3928,14 +2583,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new Int16Single();
- Int16Single()
+ private Int16Single()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- return Int16Int16.Instance.Read(bytes, offset, out readSize);
+ return Int16Int16.Instance.Read(ref byteSequence);
}
}
@@ -3943,14 +2598,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new Int32Single();
- Int32Single()
+ private Int32Single()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- return Int32Int32.Instance.Read(bytes, offset, out readSize);
+ return Int32Int32.Instance.Read(ref byteSequence);
}
}
@@ -3958,14 +2613,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new Int64Single();
- Int64Single()
+ private Int64Single()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- return Int64Int64.Instance.Read(bytes, offset, out readSize);
+ return Int64Int64.Instance.Read(ref byteSequence);
}
}
@@ -3974,14 +2629,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new UInt8Single();
- UInt8Single()
+ private UInt8Single()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- return UInt8Byte.Instance.Read(bytes, offset, out readSize);
+ return UInt8Byte.Instance.Read(ref byteSequence);
}
}
@@ -3989,14 +2644,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new UInt16Single();
- UInt16Single()
+ private UInt16Single()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- return UInt16UInt16.Instance.Read(bytes, offset, out readSize);
+ return UInt16UInt16.Instance.Read(ref byteSequence);
}
}
@@ -4004,14 +2659,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new UInt32Single();
- UInt32Single()
+ private UInt32Single()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- return UInt32UInt32.Instance.Read(bytes, offset, out readSize);
+ return UInt32UInt32.Instance.Read(ref byteSequence);
}
}
@@ -4019,14 +2674,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new UInt64Single();
- UInt64Single()
+ private UInt64Single()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- return UInt64UInt64.Instance.Read(bytes, offset, out readSize);
+ return UInt64UInt64.Instance.Read(ref byteSequence);
}
}
@@ -4034,15 +2689,14 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new Float32Single();
- Float32Single()
+ private Float32Single()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 5;
- return new Float32Bits(bytes, offset + 1).Value;
+ return MessagePackBinary.Read(ref byteSequence, 5, span => new Float32Bits(span.Slice(1)).Value);
}
}
@@ -4050,34 +2704,34 @@ namespace MessagePack.Decoders
{
internal static readonly ISingleDecoder Instance = new InvalidSingle();
- InvalidSingle()
+ private InvalidSingle()
{
}
- public Single Read(byte[] bytes, int offset, out int readSize)
+ public Single Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IDoubleDecoder
{
- double Read(byte[] bytes, int offset, out int readSize);
+ double Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixNegativeDouble : IDoubleDecoder
{
internal static readonly IDoubleDecoder Instance = new FixNegativeDouble();
- FixNegativeDouble()
+ private FixNegativeDouble()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- return FixSByte.Instance.Read(bytes, offset, out readSize);
+ return FixSByte.Instance.Read(ref byteSequence);
}
}
@@ -4085,14 +2739,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new FixDouble();
- FixDouble()
+ private FixDouble()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- return FixByte.Instance.Read(bytes, offset, out readSize);
+ return FixByte.Instance.Read(ref byteSequence);
}
}
@@ -4100,14 +2754,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new Int8Double();
- Int8Double()
+ private Int8Double()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- return Int8SByte.Instance.Read(bytes, offset, out readSize);
+ return Int8SByte.Instance.Read(ref byteSequence);
}
}
@@ -4115,14 +2769,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new Int16Double();
- Int16Double()
+ private Int16Double()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- return Int16Int16.Instance.Read(bytes, offset, out readSize);
+ return Int16Int16.Instance.Read(ref byteSequence);
}
}
@@ -4130,14 +2784,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new Int32Double();
- Int32Double()
+ private Int32Double()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- return Int32Int32.Instance.Read(bytes, offset, out readSize);
+ return Int32Int32.Instance.Read(ref byteSequence);
}
}
@@ -4145,14 +2799,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new Int64Double();
- Int64Double()
+ private Int64Double()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- return Int64Int64.Instance.Read(bytes, offset, out readSize);
+ return Int64Int64.Instance.Read(ref byteSequence);
}
}
@@ -4161,14 +2815,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new UInt8Double();
- UInt8Double()
+ private UInt8Double()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- return UInt8Byte.Instance.Read(bytes, offset, out readSize);
+ return UInt8Byte.Instance.Read(ref byteSequence);
}
}
@@ -4176,14 +2830,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new UInt16Double();
- UInt16Double()
+ private UInt16Double()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- return UInt16UInt16.Instance.Read(bytes, offset, out readSize);
+ return UInt16UInt16.Instance.Read(ref byteSequence);
}
}
@@ -4191,14 +2845,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new UInt32Double();
- UInt32Double()
+ private UInt32Double()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- return UInt32UInt32.Instance.Read(bytes, offset, out readSize);
+ return UInt32UInt32.Instance.Read(ref byteSequence);
}
}
@@ -4206,14 +2860,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new UInt64Double();
- UInt64Double()
+ private UInt64Double()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- return UInt64UInt64.Instance.Read(bytes, offset, out readSize);
+ return UInt64UInt64.Instance.Read(ref byteSequence);
}
}
@@ -4221,15 +2875,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new Float32Double();
- Float32Double()
+ private Float32Double()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 5;
- return new Float32Bits(bytes, offset + 1).Value;
+ return MessagePackBinary.Read(ref byteSequence, 5, span => new Float32Bits(span.Slice(1)).Value);
}
}
@@ -4237,15 +2890,14 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new Float64Double();
- Float64Double()
+ private Float64Double()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 9;
- return new Float64Bits(bytes, offset + 1).Value;
+ return MessagePackBinary.Read(ref byteSequence, 9, span => new Float64Bits(span.Slice(1)).Value);
}
}
@@ -4253,35 +2905,36 @@ namespace MessagePack.Decoders
{
internal static readonly IDoubleDecoder Instance = new InvalidDouble();
- InvalidDouble()
+ private InvalidDouble()
{
}
- public Double Read(byte[] bytes, int offset, out int readSize)
+ public Double Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IInt16Decoder
{
- Int16 Read(byte[] bytes, int offset, out int readSize);
+ Int16 Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixNegativeInt16 : IInt16Decoder
{
internal static readonly IInt16Decoder Instance = new FixNegativeInt16();
- FixNegativeInt16()
+ private FixNegativeInt16()
{
}
- public Int16 Read(byte[] bytes, int offset, out int readSize)
+ public Int16 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return unchecked((short)(sbyte)bytes[offset]);
+ Int16 result = unchecked((sbyte)byteSequence.First.Span[0]);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -4289,15 +2942,16 @@ namespace MessagePack.Decoders
{
internal static readonly IInt16Decoder Instance = new FixInt16();
- FixInt16()
+ private FixInt16()
{
}
- public Int16 Read(byte[] bytes, int offset, out int readSize)
+ public Int16 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return unchecked((short)bytes[offset]);
+ var result = unchecked(byteSequence.First.Span[0]);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -4305,15 +2959,14 @@ namespace MessagePack.Decoders
{
internal static readonly IInt16Decoder Instance = new UInt8Int16();
- UInt8Int16()
+ private UInt8Int16()
{
}
- public Int16 Read(byte[] bytes, int offset, out int readSize)
+ public Int16 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 2;
- return unchecked((short)(byte)(bytes[offset + 1]));
+ return MessagePackBinary.Read(ref byteSequence, 2, span => unchecked(span[1]));
}
}
@@ -4321,15 +2974,14 @@ namespace MessagePack.Decoders
{
internal static readonly IInt16Decoder Instance = new UInt16Int16();
- UInt16Int16()
+ private UInt16Int16()
{
}
- public Int16 Read(byte[] bytes, int offset, out int readSize)
+ public Int16 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 3;
- return checked((Int16)((bytes[offset + 1] << 8) + (bytes[offset + 2])));
+ return MessagePackBinary.Read(ref byteSequence, 5, span => checked((Int16)((span[1] << 8) + (span[2]))));
}
}
@@ -4337,15 +2989,14 @@ namespace MessagePack.Decoders
{
internal static readonly IInt16Decoder Instance = new Int8Int16();
- Int8Int16()
+ private Int8Int16()
{
}
- public Int16 Read(byte[] bytes, int offset, out int readSize)
+ public Int16 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 2;
- return unchecked((short)(sbyte)(bytes[offset + 1]));
+ return MessagePackBinary.Read(ref byteSequence, 2, span => unchecked((sbyte)(span[1])));
}
}
@@ -4353,18 +3004,14 @@ namespace MessagePack.Decoders
{
internal static readonly IInt16Decoder Instance = new Int16Int16();
- Int16Int16()
+ private Int16Int16()
{
}
- public Int16 Read(byte[] bytes, int offset, out int readSize)
+ public Int16 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 3;
- unchecked
- {
- return (short)((bytes[offset + 1] << 8) | (bytes[offset + 2]));
- }
+ return MessagePackBinary.Read(ref byteSequence, 3, span => unchecked((short)((span[1] << 8) | (span[2]))));
}
}
@@ -4372,35 +3019,36 @@ namespace MessagePack.Decoders
{
internal static readonly IInt16Decoder Instance = new InvalidInt16();
- InvalidInt16()
+ private InvalidInt16()
{
}
- public Int16 Read(byte[] bytes, int offset, out int readSize)
+ public Int16 Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IInt32Decoder
{
- Int32 Read(byte[] bytes, int offset, out int readSize);
+ Int32 Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixNegativeInt32 : IInt32Decoder
{
internal static readonly IInt32Decoder Instance = new FixNegativeInt32();
- FixNegativeInt32()
+ private FixNegativeInt32()
{
}
- public Int32 Read(byte[] bytes, int offset, out int readSize)
+ public Int32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return unchecked((int)(sbyte)bytes[offset]);
+ var result = unchecked((sbyte)byteSequence.First.Span[0]);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -4408,15 +3056,16 @@ namespace MessagePack.Decoders
{
internal static readonly IInt32Decoder Instance = new FixInt32();
- FixInt32()
+ private FixInt32()
{
}
- public Int32 Read(byte[] bytes, int offset, out int readSize)
+ public Int32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return unchecked((int)bytes[offset]);
+ var result = unchecked(byteSequence.First.Span[0]);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -4424,30 +3073,28 @@ namespace MessagePack.Decoders
{
internal static readonly IInt32Decoder Instance = new UInt8Int32();
- UInt8Int32()
+ private UInt8Int32()
{
}
- public Int32 Read(byte[] bytes, int offset, out int readSize)
+ public Int32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 2;
- return unchecked((int)(byte)(bytes[offset + 1]));
+ return MessagePackBinary.Read(ref byteSequence, 2, span => unchecked(span[1]));
}
}
internal sealed class UInt16Int32 : IInt32Decoder
{
internal static readonly IInt32Decoder Instance = new UInt16Int32();
- UInt16Int32()
+ private UInt16Int32()
{
}
- public Int32 Read(byte[] bytes, int offset, out int readSize)
+ public Int32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 3;
- return (Int32)((bytes[offset + 1] << 8) | (bytes[offset + 2]));
+ return MessagePackBinary.Read(ref byteSequence, 3, span => (span[1] << 8) | (span[2]));
}
}
@@ -4455,18 +3102,14 @@ namespace MessagePack.Decoders
{
internal static readonly IInt32Decoder Instance = new UInt32Int32();
- UInt32Int32()
+ private UInt32Int32()
{
}
- public Int32 Read(byte[] bytes, int offset, out int readSize)
+ public Int32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 5;
- checked
- {
- return (Int32)((UInt32)(bytes[offset + 1] << 24) | (UInt32)(bytes[offset + 2] << 16) | (UInt32)(bytes[offset + 3] << 8) | (UInt32)bytes[offset + 4]);
- }
+ return MessagePackBinary.Read(ref byteSequence, 5, span => checked((Int32)((UInt32)(span[1] << 24) | (UInt32)(span[2] << 16) | (UInt32)(span[3] << 8) | span[4])));
}
}
@@ -4474,15 +3117,14 @@ namespace MessagePack.Decoders
{
internal static readonly IInt32Decoder Instance = new Int8Int32();
- Int8Int32()
+ private Int8Int32()
{
}
- public Int32 Read(byte[] bytes, int offset, out int readSize)
+ public Int32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 2;
- return unchecked((int)(sbyte)(bytes[offset + 1]));
+ return MessagePackBinary.Read(ref byteSequence, 2, span => unchecked((sbyte)(span[1])));
}
}
@@ -4490,18 +3132,14 @@ namespace MessagePack.Decoders
{
internal static readonly IInt32Decoder Instance = new Int16Int32();
- Int16Int32()
+ private Int16Int32()
{
}
- public Int32 Read(byte[] bytes, int offset, out int readSize)
+ public Int32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 3;
- unchecked
- {
- return (int)(short)((bytes[offset + 1] << 8) | (bytes[offset + 2]));
- }
+ return MessagePackBinary.Read(ref byteSequence, 3, span => unchecked((short)((span[1] << 8) | (span[2]))));
}
}
@@ -4509,18 +3147,14 @@ namespace MessagePack.Decoders
{
internal static readonly IInt32Decoder Instance = new Int32Int32();
- Int32Int32()
+ private Int32Int32()
{
}
- public Int32 Read(byte[] bytes, int offset, out int readSize)
+ public Int32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 5;
- unchecked
- {
- return (int)((bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | bytes[offset + 4]);
- }
+ return MessagePackBinary.Read(ref byteSequence, 5, span => unchecked((span[1] << 24) | (span[2] << 16) | (span[3] << 8) | span[4]));
}
}
@@ -4528,34 +3162,35 @@ namespace MessagePack.Decoders
{
internal static readonly IInt32Decoder Instance = new InvalidInt32();
- InvalidInt32()
+ private InvalidInt32()
{
}
- public Int32 Read(byte[] bytes, int offset, out int readSize)
+ public Int32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IInt64Decoder
{
- Int64 Read(byte[] bytes, int offset, out int readSize);
+ Int64 Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixNegativeInt64 : IInt64Decoder
{
internal static readonly IInt64Decoder Instance = new FixNegativeInt64();
- FixNegativeInt64()
+ private FixNegativeInt64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return unchecked((long)(sbyte)bytes[offset]);
+ var result = unchecked((sbyte)byteSequence.First.Span[0]);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -4563,15 +3198,16 @@ namespace MessagePack.Decoders
{
internal static readonly IInt64Decoder Instance = new FixInt64();
- FixInt64()
+ private FixInt64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return unchecked((long)bytes[offset]);
+ var result = unchecked(byteSequence.First.Span[0]);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -4579,30 +3215,30 @@ namespace MessagePack.Decoders
{
internal static readonly IInt64Decoder Instance = new UInt8Int64();
- UInt8Int64()
+ private UInt8Int64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 2;
- return unchecked((int)(byte)(bytes[offset + 1]));
+ return unchecked(span[1]);
}
}
internal sealed class UInt16Int64 : IInt64Decoder
{
internal static readonly IInt64Decoder Instance = new UInt16Int64();
- UInt16Int64()
+ private UInt16Int64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 3;
- return (Int64)((bytes[offset + 1] << 8) | (bytes[offset + 2]));
+ return (span[1] << 8) | (span[2]);
}
}
@@ -4610,15 +3246,15 @@ namespace MessagePack.Decoders
{
internal static readonly IInt64Decoder Instance = new UInt32Int64();
- UInt32Int64()
+ private UInt32Int64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 5;
- return unchecked((Int64)((uint)(bytes[offset + 1] << 24) | ((uint)bytes[offset + 2] << 16) | ((uint)bytes[offset + 3] << 8) | (uint)bytes[offset + 4]));
+ return unchecked((uint)(span[1] << 24) | ((uint)span[2] << 16) | ((uint)span[3] << 8) | span[4]);
}
}
@@ -4626,18 +3262,18 @@ namespace MessagePack.Decoders
{
internal static readonly IInt64Decoder Instance = new UInt64Int64();
- UInt64Int64()
+ private UInt64Int64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 9;
checked
{
- return (Int64)bytes[offset + 1] << 56 | (Int64)bytes[offset + 2] << 48 | (Int64)bytes[offset + 3] << 40 | (Int64)bytes[offset + 4] << 32
- | (Int64)bytes[offset + 5] << 24 | (Int64)bytes[offset + 6] << 16 | (Int64)bytes[offset + 7] << 8 | (Int64)bytes[offset + 8];
+ return (Int64)span[1] << 56 | (Int64)span[2] << 48 | (Int64)span[3] << 40 | (Int64)span[4] << 32
+ | (Int64)span[5] << 24 | (Int64)span[6] << 16 | (Int64)span[7] << 8 | span[8];
}
}
}
@@ -4647,15 +3283,15 @@ namespace MessagePack.Decoders
{
internal static readonly IInt64Decoder Instance = new Int8Int64();
- Int8Int64()
+ private Int8Int64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 2;
- return unchecked((long)(sbyte)(bytes[offset + 1]));
+ return unchecked((sbyte)(span[1]));
}
}
@@ -4663,17 +3299,17 @@ namespace MessagePack.Decoders
{
internal static readonly IInt64Decoder Instance = new Int16Int64();
- Int16Int64()
+ private Int16Int64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 3;
unchecked
{
- return (long)(short)((bytes[offset + 1] << 8) | (bytes[offset + 2]));
+ return (short)((span[1] << 8) | (span[2]));
}
}
}
@@ -4682,17 +3318,17 @@ namespace MessagePack.Decoders
{
internal static readonly IInt64Decoder Instance = new Int32Int64();
- Int32Int64()
+ private Int32Int64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 5;
unchecked
{
- return (long)((long)(bytes[offset + 1] << 24) + (long)(bytes[offset + 2] << 16) + (long)(bytes[offset + 3] << 8) + (long)bytes[offset + 4]);
+ return (span[1] << 24) + (long)(span[2] << 16) + (span[3] << 8) + span[4];
}
}
}
@@ -4701,18 +3337,18 @@ namespace MessagePack.Decoders
{
internal static readonly IInt64Decoder Instance = new Int64Int64();
- Int64Int64()
+ private Int64Int64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 9;
unchecked
{
- return (long)bytes[offset + 1] << 56 | (long)bytes[offset + 2] << 48 | (long)bytes[offset + 3] << 40 | (long)bytes[offset + 4] << 32
- | (long)bytes[offset + 5] << 24 | (long)bytes[offset + 6] << 16 | (long)bytes[offset + 7] << 8 | (long)bytes[offset + 8];
+ return (long)span[1] << 56 | (long)span[2] << 48 | (long)span[3] << 40 | (long)span[4] << 32
+ | (long)span[5] << 24 | (long)span[6] << 16 | (long)span[7] << 8 | span[8];
}
}
}
@@ -4721,35 +3357,36 @@ namespace MessagePack.Decoders
{
internal static readonly IInt64Decoder Instance = new InvalidInt64();
- InvalidInt64()
+ private InvalidInt64()
{
}
- public Int64 Read(byte[] bytes, int offset, out int readSize)
+ public Int64 Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IUInt16Decoder
{
- UInt16 Read(byte[] bytes, int offset, out int readSize);
+ UInt16 Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixUInt16 : IUInt16Decoder
{
internal static readonly IUInt16Decoder Instance = new FixUInt16();
- FixUInt16()
+ private FixUInt16()
{
}
- public UInt16 Read(byte[] bytes, int offset, out int readSize)
+ public UInt16 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return unchecked((UInt16)bytes[offset]);
+ var result = unchecked(byteSequence.First.Span[0]);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -4757,15 +3394,15 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt16Decoder Instance = new UInt8UInt16();
- UInt8UInt16()
+ private UInt8UInt16()
{
}
- public UInt16 Read(byte[] bytes, int offset, out int readSize)
+ public UInt16 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 2;
- return unchecked((UInt16)(bytes[offset + 1]));
+ return unchecked(span[1]);
}
}
@@ -4773,17 +3410,17 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt16Decoder Instance = new UInt16UInt16();
- UInt16UInt16()
+ private UInt16UInt16()
{
}
- public UInt16 Read(byte[] bytes, int offset, out int readSize)
+ public UInt16 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 3;
unchecked
{
- return (UInt16)((bytes[offset + 1] << 8) | (bytes[offset + 2]));
+ return (UInt16)((span[1] << 8) | (span[2]));
}
}
}
@@ -4792,35 +3429,36 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt16Decoder Instance = new InvalidUInt16();
- InvalidUInt16()
+ private InvalidUInt16()
{
}
- public UInt16 Read(byte[] bytes, int offset, out int readSize)
+ public UInt16 Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IUInt32Decoder
{
- UInt32 Read(byte[] bytes, int offset, out int readSize);
+ UInt32 Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixUInt32 : IUInt32Decoder
{
internal static readonly IUInt32Decoder Instance = new FixUInt32();
- FixUInt32()
+ private FixUInt32()
{
}
- public UInt32 Read(byte[] bytes, int offset, out int readSize)
+ public UInt32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return unchecked((UInt32)bytes[offset]);
+ var result = unchecked(byteSequence.First.Span[0]);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -4828,15 +3466,15 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt32Decoder Instance = new UInt8UInt32();
- UInt8UInt32()
+ private UInt8UInt32()
{
}
- public UInt32 Read(byte[] bytes, int offset, out int readSize)
+ public UInt32 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 2;
- return unchecked((UInt32)(bytes[offset + 1]));
+ return unchecked(span[1]);
}
}
@@ -4844,17 +3482,17 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt32Decoder Instance = new UInt16UInt32();
- UInt16UInt32()
+ private UInt16UInt32()
{
}
- public UInt32 Read(byte[] bytes, int offset, out int readSize)
+ public UInt32 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 3;
unchecked
{
- return (UInt32)((bytes[offset + 1] << 8) | (bytes[offset + 2]));
+ return (UInt32)((span[1] << 8) | (span[2]));
}
}
}
@@ -4863,17 +3501,17 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt32Decoder Instance = new UInt32UInt32();
- UInt32UInt32()
+ private UInt32UInt32()
{
}
- public UInt32 Read(byte[] bytes, int offset, out int readSize)
+ public UInt32 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 5;
unchecked
{
- return (UInt32)((UInt32)(bytes[offset + 1] << 24) | (UInt32)(bytes[offset + 2] << 16) | (UInt32)(bytes[offset + 3] << 8) | (UInt32)bytes[offset + 4]);
+ return (UInt32)(span[1] << 24) | (UInt32)(span[2] << 16) | (UInt32)(span[3] << 8) | span[4];
}
}
}
@@ -4882,35 +3520,36 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt32Decoder Instance = new InvalidUInt32();
- InvalidUInt32()
+ private InvalidUInt32()
{
}
- public UInt32 Read(byte[] bytes, int offset, out int readSize)
+ public UInt32 Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IUInt64Decoder
{
- UInt64 Read(byte[] bytes, int offset, out int readSize);
+ UInt64 Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixUInt64 : IUInt64Decoder
{
internal static readonly IUInt64Decoder Instance = new FixUInt64();
- FixUInt64()
+ private FixUInt64()
{
}
- public UInt64 Read(byte[] bytes, int offset, out int readSize)
+ public UInt64 Read(ref ReadOnlySequence<byte> byteSequence)
{
- readSize = 1;
- return unchecked((UInt64)bytes[offset]);
+ var result = unchecked(byteSequence.First.Span[0]);
+ byteSequence = byteSequence.Slice(1);
+ return result;
}
}
@@ -4918,15 +3557,15 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt64Decoder Instance = new UInt8UInt64();
- UInt8UInt64()
+ private UInt8UInt64()
{
}
- public UInt64 Read(byte[] bytes, int offset, out int readSize)
+ public UInt64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 2;
- return unchecked((UInt64)(bytes[offset + 1]));
+ return unchecked(span[1]);
}
}
@@ -4934,17 +3573,17 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt64Decoder Instance = new UInt16UInt64();
- UInt16UInt64()
+ private UInt16UInt64()
{
}
- public UInt64 Read(byte[] bytes, int offset, out int readSize)
+ public UInt64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 3;
unchecked
{
- return (UInt64)((bytes[offset + 1] << 8) | (bytes[offset + 2]));
+ return (UInt64)((span[1] << 8) | (span[2]));
}
}
}
@@ -4953,17 +3592,17 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt64Decoder Instance = new UInt32UInt64();
- UInt32UInt64()
+ private UInt32UInt64()
{
}
- public UInt64 Read(byte[] bytes, int offset, out int readSize)
+ public UInt64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 5;
unchecked
{
- return (UInt64)(((UInt64)bytes[offset + 1] << 24) + (ulong)(bytes[offset + 2] << 16) + (UInt64)(bytes[offset + 3] << 8) + (UInt64)bytes[offset + 4]);
+ return ((UInt64)span[1] << 24) + (ulong)(span[2] << 16) + (UInt64)(span[3] << 8) + span[4];
}
}
}
@@ -4972,18 +3611,18 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt64Decoder Instance = new UInt64UInt64();
- UInt64UInt64()
+ private UInt64UInt64()
{
}
- public UInt64 Read(byte[] bytes, int offset, out int readSize)
+ public UInt64 Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 9;
unchecked
{
- return (UInt64)bytes[offset + 1] << 56 | (UInt64)bytes[offset + 2] << 48 | (UInt64)bytes[offset + 3] << 40 | (UInt64)bytes[offset + 4] << 32
- | (UInt64)bytes[offset + 5] << 24 | (UInt64)bytes[offset + 6] << 16 | (UInt64)bytes[offset + 7] << 8 | (UInt64)bytes[offset + 8];
+ return (UInt64)span[1] << 56 | (UInt64)span[2] << 48 | (UInt64)span[3] << 40 | (UInt64)span[4] << 32
+ | (UInt64)span[5] << 24 | (UInt64)span[6] << 16 | (UInt64)span[7] << 8 | span[8];
}
}
}
@@ -4992,32 +3631,32 @@ namespace MessagePack.Decoders
{
internal static readonly IUInt64Decoder Instance = new InvalidUInt64();
- InvalidUInt64()
+ private InvalidUInt64()
{
}
- public UInt64 Read(byte[] bytes, int offset, out int readSize)
+ public UInt64 Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IStringDecoder
{
- String Read(byte[] bytes, int offset, out int readSize);
+ String Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class NilString : IStringDecoder
{
internal static readonly IStringDecoder Instance = new NilString();
- NilString()
+ private NilString()
{
}
- public String Read(byte[] bytes, int offset, out int readSize)
+ public String Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 1;
return null;
@@ -5028,14 +3667,14 @@ namespace MessagePack.Decoders
{
internal static readonly IStringDecoder Instance = new FixString();
- FixString()
+ private FixString()
{
}
- public String Read(byte[] bytes, int offset, out int readSize)
+ public String Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = bytes[offset] & 0x1F;
+ var length = span[0] & 0x1F;
readSize = length + 1;
return StringEncoding.UTF8.GetString(bytes, offset + 1, length);
}
@@ -5045,14 +3684,14 @@ namespace MessagePack.Decoders
{
internal static readonly IStringDecoder Instance = new Str8String();
- Str8String()
+ private Str8String()
{
}
- public String Read(byte[] bytes, int offset, out int readSize)
+ public String Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = (int)bytes[offset + 1];
+ var length = (int)span[1];
readSize = length + 2;
return StringEncoding.UTF8.GetString(bytes, offset + 2, length);
}
@@ -5062,16 +3701,16 @@ namespace MessagePack.Decoders
{
internal static readonly IStringDecoder Instance = new Str16String();
- Str16String()
+ private Str16String()
{
}
- public String Read(byte[] bytes, int offset, out int readSize)
+ public String Read(ref ReadOnlySequence<byte> byteSequence)
{
unchecked
{
- var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]);
+ var length = (span[1] << 8) + (span[2]);
readSize = length + 3;
return StringEncoding.UTF8.GetString(bytes, offset + 3, length);
}
@@ -5082,16 +3721,16 @@ namespace MessagePack.Decoders
{
internal static readonly IStringDecoder Instance = new Str32String();
- Str32String()
+ private Str32String()
{
}
- public String Read(byte[] bytes, int offset, out int readSize)
+ public String Read(ref ReadOnlySequence<byte> byteSequence)
{
unchecked
{
- var length = (int)((uint)(bytes[offset + 1] << 24) | (uint)(bytes[offset + 2] << 16) | (uint)(bytes[offset + 3] << 8) | (uint)bytes[offset + 4]);
+ var length = (int)((uint)(span[1] << 24) | (uint)(span[2] << 16) | (uint)(span[3] << 8) | span[4]);
readSize = length + 5;
return StringEncoding.UTF8.GetString(bytes, offset + 5, length);
}
@@ -5102,32 +3741,32 @@ namespace MessagePack.Decoders
{
internal static readonly IStringDecoder Instance = new InvalidString();
- InvalidString()
+ private InvalidString()
{
}
- public String Read(byte[] bytes, int offset, out int readSize)
+ public String Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IStringSegmentDecoder
{
- ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize);
+ ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class NilStringSegment : IStringSegmentDecoder
{
internal static readonly IStringSegmentDecoder Instance = new NilStringSegment();
- NilStringSegment()
+ private NilStringSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 1;
return new ArraySegment<byte>(bytes, offset, 1);
@@ -5138,14 +3777,14 @@ namespace MessagePack.Decoders
{
internal static readonly IStringSegmentDecoder Instance = new FixStringSegment();
- FixStringSegment()
+ private FixStringSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = bytes[offset] & 0x1F;
+ var length = span[0] & 0x1F;
readSize = length + 1;
return new ArraySegment<byte>(bytes, offset + 1, length);
}
@@ -5155,14 +3794,14 @@ namespace MessagePack.Decoders
{
internal static readonly IStringSegmentDecoder Instance = new Str8StringSegment();
- Str8StringSegment()
+ private Str8StringSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = (int)bytes[offset + 1];
+ var length = (int)span[1];
readSize = length + 2;
return new ArraySegment<byte>(bytes, offset + 2, length);
}
@@ -5172,16 +3811,16 @@ namespace MessagePack.Decoders
{
internal static readonly IStringSegmentDecoder Instance = new Str16StringSegment();
- Str16StringSegment()
+ private Str16StringSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
unchecked
{
- var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]);
+ var length = (span[1] << 8) + (span[2]);
readSize = length + 3;
return new ArraySegment<byte>(bytes, offset + 3, length);
}
@@ -5192,16 +3831,16 @@ namespace MessagePack.Decoders
{
internal static readonly IStringSegmentDecoder Instance = new Str32StringSegment();
- Str32StringSegment()
+ private Str32StringSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
unchecked
{
- var length = (int)((uint)(bytes[offset + 1] << 24) | (uint)(bytes[offset + 2] << 16) | (uint)(bytes[offset + 3] << 8) | (uint)bytes[offset + 4]);
+ var length = (int)((uint)(span[1] << 24) | (uint)(span[2] << 16) | (uint)(span[3] << 8) | span[4]);
readSize = length + 5;
return new ArraySegment<byte>(bytes, offset + 5, length);
}
@@ -5212,36 +3851,36 @@ namespace MessagePack.Decoders
{
internal static readonly IStringSegmentDecoder Instance = new InvalidStringSegment();
- InvalidStringSegment()
+ private InvalidStringSegment()
{
}
- public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize)
+ public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IExtDecoder
{
- ExtensionResult Read(byte[] bytes, int offset, out int readSize);
+ ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixExt1 : IExtDecoder
{
internal static readonly IExtDecoder Instance = new FixExt1();
- FixExt1()
+ private FixExt1()
{
}
- public ExtensionResult Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 3;
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
- var body = new byte[1] { bytes[offset + 2] }; // make new bytes is overhead?
+ var typeCode = unchecked((sbyte)span[1]);
+ var body = new byte[1] { span[2] }; // make new bytes is overhead?
return new ExtensionResult(typeCode, body);
}
}
@@ -5250,19 +3889,19 @@ namespace MessagePack.Decoders
{
internal static readonly IExtDecoder Instance = new FixExt2();
- FixExt2()
+ private FixExt2()
{
}
- public ExtensionResult Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 4;
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
var body = new byte[2]
{
- bytes[offset + 2],
- bytes[offset + 3],
+ span[2],
+ span[3],
};
return new ExtensionResult(typeCode, body);
}
@@ -5272,21 +3911,21 @@ namespace MessagePack.Decoders
{
internal static readonly IExtDecoder Instance = new FixExt4();
- FixExt4()
+ private FixExt4()
{
}
- public ExtensionResult Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 6;
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
var body = new byte[4]
{
- bytes[offset + 2],
- bytes[offset + 3],
- bytes[offset + 4],
- bytes[offset + 5],
+ span[2],
+ span[3],
+ span[4],
+ span[5],
};
return new ExtensionResult(typeCode, body);
}
@@ -5296,25 +3935,25 @@ namespace MessagePack.Decoders
{
internal static readonly IExtDecoder Instance = new FixExt8();
- FixExt8()
+ private FixExt8()
{
}
- public ExtensionResult Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 10;
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
var body = new byte[8]
{
- bytes[offset + 2],
- bytes[offset + 3],
- bytes[offset + 4],
- bytes[offset + 5],
- bytes[offset + 6],
- bytes[offset + 7],
- bytes[offset + 8],
- bytes[offset + 9],
+ span[2],
+ span[3],
+ span[4],
+ span[5],
+ span[6],
+ span[7],
+ span[8],
+ span[9],
};
return new ExtensionResult(typeCode, body);
}
@@ -5324,33 +3963,33 @@ namespace MessagePack.Decoders
{
internal static readonly IExtDecoder Instance = new FixExt16();
- FixExt16()
+ private FixExt16()
{
}
- public ExtensionResult Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 18;
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
var body = new byte[16]
{
- bytes[offset + 2],
- bytes[offset + 3],
- bytes[offset + 4],
- bytes[offset + 5],
- bytes[offset + 6],
- bytes[offset + 7],
- bytes[offset + 8],
- bytes[offset + 9],
- bytes[offset + 10],
- bytes[offset + 11],
- bytes[offset + 12],
- bytes[offset + 13],
- bytes[offset + 14],
- bytes[offset + 15],
- bytes[offset + 16],
- bytes[offset + 17]
+ span[2],
+ span[3],
+ span[4],
+ span[5],
+ span[6],
+ span[7],
+ span[8],
+ span[9],
+ span[10],
+ span[11],
+ span[12],
+ span[13],
+ span[14],
+ span[15],
+ span[16],
+ span[17]
};
return new ExtensionResult(typeCode, body);
}
@@ -5360,21 +3999,21 @@ namespace MessagePack.Decoders
{
internal static readonly IExtDecoder Instance = new Ext8();
- Ext8()
+ private Ext8()
{
}
- public ExtensionResult Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence)
{
unchecked
{
- var length = bytes[offset + 1];
- var typeCode = unchecked((sbyte)bytes[offset + 2]);
+ var length = span[1];
+ var typeCode = unchecked((sbyte)span[2]);
var body = new byte[length];
- readSize = (int)length + 3;
- Buffer.BlockCopy(bytes, offset + 3, body, 0, (int)length);
+ readSize = length + 3;
+ Buffer.BlockCopy(bytes, offset + 3, body, 0, length);
return new ExtensionResult(typeCode, body);
}
}
@@ -5384,21 +4023,21 @@ namespace MessagePack.Decoders
{
internal static readonly IExtDecoder Instance = new Ext16();
- Ext16()
+ private Ext16()
{
}
- public ExtensionResult Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence)
{
unchecked
{
- var length = (int)((UInt16)(bytes[offset + 1] << 8) | (UInt16)bytes[offset + 2]);
- var typeCode = unchecked((sbyte)bytes[offset + 3]);
+ var length = (UInt16)(span[1] << 8) | span[2];
+ var typeCode = unchecked((sbyte)span[3]);
var body = new byte[length];
readSize = length + 4;
- Buffer.BlockCopy(bytes, offset + 4, body, 0, (int)length);
+ Buffer.BlockCopy(bytes, offset + 4, body, 0, length);
return new ExtensionResult(typeCode, body);
}
}
@@ -5408,17 +4047,17 @@ namespace MessagePack.Decoders
{
internal static readonly IExtDecoder Instance = new Ext32();
- Ext32()
+ private Ext32()
{
}
- public ExtensionResult Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence)
{
unchecked
{
- var length = (UInt32)((UInt32)(bytes[offset + 1] << 24) | (UInt32)(bytes[offset + 2] << 16) | (UInt32)(bytes[offset + 3] << 8) | (UInt32)bytes[offset + 4]);
- var typeCode = unchecked((sbyte)bytes[offset + 5]);
+ var length = (UInt32)(span[1] << 24) | (UInt32)(span[2] << 16) | (UInt32)(span[3] << 8) | span[4];
+ var typeCode = unchecked((sbyte)span[5]);
var body = new byte[length];
checked
@@ -5435,14 +4074,14 @@ namespace MessagePack.Decoders
{
internal static readonly IExtDecoder Instance = new InvalidExt();
- InvalidExt()
+ private InvalidExt()
{
}
- public ExtensionResult Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
@@ -5453,22 +4092,22 @@ namespace MessagePack.Decoders
internal interface IExtHeaderDecoder
{
- ExtensionHeader Read(byte[] bytes, int offset, out int readSize);
+ ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixExt1Header : IExtHeaderDecoder
{
internal static readonly IExtHeaderDecoder Instance = new FixExt1Header();
- FixExt1Header()
+ private FixExt1Header()
{
}
- public ExtensionHeader Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 2;
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
return new ExtensionHeader(typeCode, 1);
}
}
@@ -5477,15 +4116,15 @@ namespace MessagePack.Decoders
{
internal static readonly IExtHeaderDecoder Instance = new FixExt2Header();
- FixExt2Header()
+ private FixExt2Header()
{
}
- public ExtensionHeader Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 2;
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
return new ExtensionHeader(typeCode, 2);
}
}
@@ -5494,15 +4133,15 @@ namespace MessagePack.Decoders
{
internal static readonly IExtHeaderDecoder Instance = new FixExt4Header();
- FixExt4Header()
+ private FixExt4Header()
{
}
- public ExtensionHeader Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 2;
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
return new ExtensionHeader(typeCode, 4);
}
}
@@ -5511,15 +4150,15 @@ namespace MessagePack.Decoders
{
internal static readonly IExtHeaderDecoder Instance = new FixExt8Header();
- FixExt8Header()
+ private FixExt8Header()
{
}
- public ExtensionHeader Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 2;
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
return new ExtensionHeader(typeCode, 8);
}
}
@@ -5528,15 +4167,15 @@ namespace MessagePack.Decoders
{
internal static readonly IExtHeaderDecoder Instance = new FixExt16Header();
- FixExt16Header()
+ private FixExt16Header()
{
}
- public ExtensionHeader Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence)
{
readSize = 2;
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
return new ExtensionHeader(typeCode, 16);
}
}
@@ -5545,17 +4184,17 @@ namespace MessagePack.Decoders
{
internal static readonly IExtHeaderDecoder Instance = new Ext8Header();
- Ext8Header()
+ private Ext8Header()
{
}
- public ExtensionHeader Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence)
{
unchecked
{
- var length = bytes[offset + 1];
- var typeCode = unchecked((sbyte)bytes[offset + 2]);
+ var length = span[1];
+ var typeCode = unchecked((sbyte)span[2]);
readSize = 3;
return new ExtensionHeader(typeCode, length);
@@ -5567,17 +4206,17 @@ namespace MessagePack.Decoders
{
internal static readonly IExtHeaderDecoder Instance = new Ext16Header();
- Ext16Header()
+ private Ext16Header()
{
}
- public ExtensionHeader Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence)
{
unchecked
{
- var length = (UInt32)((UInt16)(bytes[offset + 1] << 8) | (UInt16)bytes[offset + 2]);
- var typeCode = unchecked((sbyte)bytes[offset + 3]);
+ var length = (UInt32)((UInt16)(span[1] << 8) | span[2]);
+ var typeCode = unchecked((sbyte)span[3]);
readSize = 4;
return new ExtensionHeader(typeCode, length);
@@ -5589,17 +4228,17 @@ namespace MessagePack.Decoders
{
internal static readonly IExtHeaderDecoder Instance = new Ext32Header();
- Ext32Header()
+ private Ext32Header()
{
}
- public ExtensionHeader Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence)
{
unchecked
{
- var length = (UInt32)((UInt32)(bytes[offset + 1] << 24) | (UInt32)(bytes[offset + 2] << 16) | (UInt32)(bytes[offset + 3] << 8) | (UInt32)bytes[offset + 4]);
- var typeCode = unchecked((sbyte)bytes[offset + 5]);
+ var length = (UInt32)(span[1] << 24) | (UInt32)(span[2] << 16) | (UInt32)(span[3] << 8) | span[4];
+ var typeCode = unchecked((sbyte)span[5]);
readSize = 6;
return new ExtensionHeader(typeCode, length);
@@ -5611,34 +4250,34 @@ namespace MessagePack.Decoders
{
internal static readonly IExtHeaderDecoder Instance = new InvalidExtHeader();
- InvalidExtHeader()
+ private InvalidExtHeader()
{
}
- public ExtensionHeader Read(byte[] bytes, int offset, out int readSize)
+ public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IDateTimeDecoder
{
- DateTime Read(byte[] bytes, int offset, out int readSize);
+ DateTime Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class FixExt4DateTime : IDateTimeDecoder
{
internal static readonly IDateTimeDecoder Instance = new FixExt4DateTime();
- FixExt4DateTime()
+ private FixExt4DateTime()
{
}
- public DateTime Read(byte[] bytes, int offset, out int readSize)
+ public DateTime Read(ref ReadOnlySequence<byte> byteSequence)
{
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
if (typeCode != ReservedMessagePackExtensionTypeCode.DateTime)
{
throw new InvalidOperationException(string.Format("typeCode is invalid. typeCode:{0}", typeCode));
@@ -5646,7 +4285,7 @@ namespace MessagePack.Decoders
unchecked
{
- var seconds = (UInt32)((UInt32)(bytes[offset + 2] << 24) | (UInt32)(bytes[offset + 3] << 16) | (UInt32)(bytes[offset + 4] << 8) | (UInt32)bytes[offset + 5]);
+ var seconds = (UInt32)(span[2] << 24) | (UInt32)(span[3] << 16) | (UInt32)(span[4] << 8) | span[5];
readSize = 6;
return DateTimeConstants.UnixEpoch.AddSeconds(seconds);
@@ -5658,21 +4297,21 @@ namespace MessagePack.Decoders
{
internal static readonly IDateTimeDecoder Instance = new FixExt8DateTime();
- FixExt8DateTime()
+ private FixExt8DateTime()
{
}
- public DateTime Read(byte[] bytes, int offset, out int readSize)
+ public DateTime Read(ref ReadOnlySequence<byte> byteSequence)
{
- var typeCode = unchecked((sbyte)bytes[offset + 1]);
+ var typeCode = unchecked((sbyte)span[1]);
if (typeCode != ReservedMessagePackExtensionTypeCode.DateTime)
{
throw new InvalidOperationException(string.Format("typeCode is invalid. typeCode:{0}", typeCode));
}
- var data64 = (UInt64)bytes[offset + 2] << 56 | (UInt64)bytes[offset + 3] << 48 | (UInt64)bytes[offset + 4] << 40 | (UInt64)bytes[offset + 5] << 32
- | (UInt64)bytes[offset + 6] << 24 | (UInt64)bytes[offset + 7] << 16 | (UInt64)bytes[offset + 8] << 8 | (UInt64)bytes[offset + 9];
+ var data64 = (UInt64)span[2] << 56 | (UInt64)span[3] << 48 | (UInt64)span[4] << 40 | (UInt64)span[5] << 32
+ | (UInt64)span[6] << 24 | (UInt64)span[7] << 16 | (UInt64)span[8] << 8 | span[9];
var nanoseconds = (long)(data64 >> 34);
var seconds = data64 & 0x00000003ffffffffL;
@@ -5686,25 +4325,25 @@ namespace MessagePack.Decoders
{
internal static readonly IDateTimeDecoder Instance = new Ext8DateTime();
- Ext8DateTime()
+ private Ext8DateTime()
{
}
- public DateTime Read(byte[] bytes, int offset, out int readSize)
+ public DateTime Read(ref ReadOnlySequence<byte> byteSequence)
{
- var length = checked((byte)bytes[offset + 1]);
- var typeCode = unchecked((sbyte)bytes[offset + 2]);
+ var length = checked(span[1]);
+ var typeCode = unchecked((sbyte)span[2]);
if (length != 12 || typeCode != ReservedMessagePackExtensionTypeCode.DateTime)
{
throw new InvalidOperationException(string.Format("typeCode is invalid. typeCode:{0}", typeCode));
}
- var nanoseconds = (UInt32)((UInt32)(bytes[offset + 3] << 24) | (UInt32)(bytes[offset + 4] << 16) | (UInt32)(bytes[offset + 5] << 8) | (UInt32)bytes[offset + 6]);
+ var nanoseconds = (UInt32)(span[3] << 24) | (UInt32)(span[4] << 16) | (UInt32)(span[5] << 8) | span[6];
unchecked
{
- var seconds = (long)bytes[offset + 7] << 56 | (long)bytes[offset + 8] << 48 | (long)bytes[offset + 9] << 40 | (long)bytes[offset + 10] << 32
- | (long)bytes[offset + 11] << 24 | (long)bytes[offset + 12] << 16 | (long)bytes[offset + 13] << 8 | (long)bytes[offset + 14];
+ var seconds = (long)span[7] << 56 | (long)span[8] << 48 | (long)span[9] << 40 | (long)span[10] << 32
+ | (long)span[11] << 24 | (long)span[12] << 16 | (long)span[13] << 8 | span[14];
readSize = 15;
return DateTimeConstants.UnixEpoch.AddSeconds(seconds).AddTicks(nanoseconds / DateTimeConstants.NanosecondsPerTick);
@@ -5716,26 +4355,27 @@ namespace MessagePack.Decoders
{
internal static readonly IDateTimeDecoder Instance = new InvalidDateTime();
- InvalidDateTime()
+ private InvalidDateTime()
{
}
- public DateTime Read(byte[] bytes, int offset, out int readSize)
+ public DateTime Read(ref ReadOnlySequence<byte> byteSequence)
{
- throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
+ throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0])));
}
}
internal interface IReadNextDecoder
{
- int Read(byte[] bytes, int offset);
+ void Read(ref ReadOnlySequence<byte> byteSequence);
}
internal sealed class ReadNext1 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNext1();
- ReadNext1()
+
+ private ReadNext1()
{
}
@@ -5745,7 +4385,8 @@ namespace MessagePack.Decoders
internal sealed class ReadNext2 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNext2();
- ReadNext2()
+
+ private ReadNext2()
{
}
@@ -5755,7 +4396,8 @@ namespace MessagePack.Decoders
internal sealed class ReadNext3 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNext3();
- ReadNext3()
+
+ private ReadNext3()
{
}
@@ -5764,7 +4406,8 @@ namespace MessagePack.Decoders
internal sealed class ReadNext4 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNext4();
- ReadNext4()
+
+ private ReadNext4()
{
}
@@ -5773,7 +4416,8 @@ namespace MessagePack.Decoders
internal sealed class ReadNext5 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNext5();
- ReadNext5()
+
+ private ReadNext5()
{
}
@@ -5782,7 +4426,8 @@ namespace MessagePack.Decoders
internal sealed class ReadNext6 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNext6();
- ReadNext6()
+
+ private ReadNext6()
{
}
@@ -5792,7 +4437,8 @@ namespace MessagePack.Decoders
internal sealed class ReadNext9 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNext9();
- ReadNext9()
+
+ private ReadNext9()
{
}
@@ -5801,7 +4447,8 @@ namespace MessagePack.Decoders
internal sealed class ReadNext10 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNext10();
- ReadNext10()
+
+ private ReadNext10()
{
}
@@ -5810,7 +4457,8 @@ namespace MessagePack.Decoders
internal sealed class ReadNext18 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNext18();
- ReadNext18()
+
+ private ReadNext18()
{
}
@@ -5820,7 +4468,8 @@ namespace MessagePack.Decoders
internal sealed class ReadNextMap : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextMap();
- ReadNextMap()
+
+ private ReadNextMap()
{
}
@@ -5828,7 +4477,7 @@ namespace MessagePack.Decoders
{
var startOffset = offset;
int readSize;
- var length = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
+ var length = MessagePackBinary.ReadMapHeader(ref byteSequence);
offset += readSize;
for (int i = 0; i < length; i++)
{
@@ -5842,7 +4491,8 @@ namespace MessagePack.Decoders
internal sealed class ReadNextArray : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextArray();
- ReadNextArray()
+
+ private ReadNextArray()
{
}
@@ -5850,7 +4500,7 @@ namespace MessagePack.Decoders
{
var startOffset = offset;
int readSize;
- var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
+ var length = MessagePackBinary.ReadArrayHeader(ref byteSequence);
offset += readSize;
for (int i = 0; i < length; i++)
{
@@ -5863,13 +4513,14 @@ namespace MessagePack.Decoders
internal sealed class ReadNextFixStr : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextFixStr();
- ReadNextFixStr()
+
+ private ReadNextFixStr()
{
}
public int Read(byte[] bytes, int offset)
{
- var length = bytes[offset] & 0x1F;
+ var length = span[0] & 0x1F;
return length + 1;
}
}
@@ -5877,13 +4528,14 @@ namespace MessagePack.Decoders
internal sealed class ReadNextStr8 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextStr8();
- ReadNextStr8()
+
+ private ReadNextStr8()
{
}
public int Read(byte[] bytes, int offset)
{
- var length = (int)bytes[offset + 1];
+ var length = (int)span[1];
return length + 2;
}
}
@@ -5891,14 +4543,15 @@ namespace MessagePack.Decoders
internal sealed class ReadNextStr16 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextStr16();
- ReadNextStr16()
+
+ private ReadNextStr16()
{
}
public int Read(byte[] bytes, int offset)
{
- var length = (bytes[offset + 1] << 8) | (bytes[offset + 2]);
+ var length = (span[1] << 8) | (span[2]);
return length + 3;
}
}
@@ -5906,13 +4559,14 @@ namespace MessagePack.Decoders
internal sealed class ReadNextStr32 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextStr32();
- ReadNextStr32()
+
+ private ReadNextStr32()
{
}
public int Read(byte[] bytes, int offset)
{
- var length = (int)((uint)(bytes[offset + 1] << 24) | (uint)(bytes[offset + 2] << 16) | (uint)(bytes[offset + 3] << 8) | (uint)bytes[offset + 4]);
+ var length = (int)((uint)(span[1] << 24) | (uint)(span[2] << 16) | (uint)(span[3] << 8) | span[4]);
return length + 5;
}
}
@@ -5920,13 +4574,14 @@ namespace MessagePack.Decoders
internal sealed class ReadNextBin8 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextBin8();
- ReadNextBin8()
+
+ private ReadNextBin8()
{
}
public int Read(byte[] bytes, int offset)
{
- var length = bytes[offset + 1];
+ var length = span[1];
return length + 2;
}
}
@@ -5934,14 +4589,15 @@ namespace MessagePack.Decoders
internal sealed class ReadNextBin16 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextBin16();
- ReadNextBin16()
+
+ private ReadNextBin16()
{
}
public int Read(byte[] bytes, int offset)
{
- var length = (bytes[offset + 1] << 8) | (bytes[offset + 2]);
+ var length = (span[1] << 8) | (span[2]);
return length + 3;
}
}
@@ -5949,13 +4605,14 @@ namespace MessagePack.Decoders
internal sealed class ReadNextBin32 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextBin32();
- ReadNextBin32()
+
+ private ReadNextBin32()
{
}
public int Read(byte[] bytes, int offset)
{
- var length = (bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | (bytes[offset + 4]);
+ var length = (span[1] << 24) | (span[2] << 16) | (span[3] << 8) | (span[4]);
return length + 5;
}
}
@@ -5963,27 +4620,29 @@ namespace MessagePack.Decoders
internal sealed class ReadNextExt8 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextExt8();
- ReadNextExt8()
+
+ private ReadNextExt8()
{
}
public int Read(byte[] bytes, int offset)
{
- var length = bytes[offset + 1];
- return (int)length + 3;
+ var length = span[1];
+ return length + 3;
}
}
internal sealed class ReadNextExt16 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextExt16();
- ReadNextExt16()
+
+ private ReadNextExt16()
{
}
public int Read(byte[] bytes, int offset)
{
- var length = (int)((UInt16)(bytes[offset + 1] << 8) | (UInt16)bytes[offset + 2]);
+ var length = (UInt16)(span[1] << 8) | span[2];
return length + 4;
}
}
@@ -5991,14 +4650,15 @@ namespace MessagePack.Decoders
internal sealed class ReadNextExt32 : IReadNextDecoder
{
internal static readonly IReadNextDecoder Instance = new ReadNextExt32();
- ReadNextExt32()
+
+ private ReadNextExt32()
{
}
public int Read(byte[] bytes, int offset)
{
- var length = (UInt32)((UInt32)(bytes[offset + 1] << 24) | (UInt32)(bytes[offset + 2] << 16) | (UInt32)(bytes[offset + 3] << 8) | (UInt32)bytes[offset + 4]);
+ var length = (UInt32)(span[1] << 24) | (UInt32)(span[2] << 16) | (UInt32)(span[3] << 8) | span[4];
return (int)length + 6;
}
}
-} \ No newline at end of file
+}