Beispiel #1
0
        public static T Deserialize <T>([NotNull] this ISerializer <T> s, ArraySegment <byte> bytes)
        {
            if (bytes.Array == null)
            {
                ThrowHelpers.ThrowArgumentNullException(nameof(bytes));
#pragma warning disable CS8603   // Possible null reference return.
                return(default); // never reached
Beispiel #2
0
        public static int WritePackedLength(this Span <byte> bytes, long len)
        {
            if (len < 0 || (ulong)len >= 0xC000000000000000UL)
            {
                ThrowHelpers.ThrowArgumentOutOfRangeException(nameof(len));
            }

            if (len > 0x3FFFFFFF)
            {
                BinaryPrimitives.WriteUInt64BigEndian(bytes, unchecked ((ulong)len | 0xC000000000000000UL));
                return(sizeof(ulong));
            }

            if (len > 0x3FFF)
            {
                BinaryPrimitives.WriteUInt32BigEndian(bytes, unchecked ((uint)len | 0x80000000U));
                return(sizeof(uint));
            }

            if (len > 0x3F)
            {
                BinaryPrimitives.WriteUInt16BigEndian(bytes, unchecked ((ushort)(len | 0x4000)));
                return(sizeof(ushort));
            }

            bytes[0] = unchecked ((byte)len);
            return(sizeof(byte));
        }
        public byte[] Deserialize(ReadOnlySpan <byte> bytes)
        {
            if (bytes.Length != Size)
            {
                ThrowHelpers.ThrowArgumentException("Invalid length", nameof(bytes));
            }

            return(bytes.ToArray());
        }
        public byte[] Deserialize(byte[] bytes, int offset, int length)
        {
            if (length != Size)
            {
                ThrowHelpers.ThrowArgumentException("Invalid length", nameof(bytes));
            }

            return(bytes.AsSpan(offset, length).ToArray());
        }
Beispiel #5
0
        public sbyte Deserialize(Stream stream)
        {
            var rv = stream.ReadByte();

            if (rv < 0)
            {
                ThrowHelpers.ThrowArgumentOutOfRangeException(nameof(stream));
            }

            return(unchecked ((sbyte)rv));
        }
        public bool Deserialize(Stream stream)
        {
            var rv = stream.ReadByte();

            if (rv < 0)
            {
                ThrowHelpers.ThrowArgumentOutOfRangeException(nameof(stream));
            }

            return(rv != 0);
        }
        public byte[] Serialize(byte[] obj)
        {
            if (obj == null)
            {
                ThrowHelpers.ThrowArgumentNullException(nameof(obj));
            }

            // ReSharper disable once PossibleNullReferenceException
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            if (obj.Length != Size)
            {
#pragma warning restore CS8602 // Dereference of a possibly null reference.
                ThrowHelpers.ThrowArgumentOutOfRangeException(nameof(obj));
            }

            return(obj.AsSpan().ToArray());
        }
        public byte[] Serialize(T[] obj)
        {
            if (obj == null)
            {
                ThrowHelpers.ThrowArgumentNullException(nameof(obj));
            }

            // ReSharper disable once PossibleNullReferenceException
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            if (obj.Length == 0)
            {
#pragma warning restore CS8602 // Dereference of a possibly null reference.
                return(new byte[1]);
            }

            using var ms = new MemoryStream();
            ms.WritePackedLength(obj.Length);
            var underlyingSize = underlying.Size;
            if (underlyingSize > 0)
            {
                foreach (var o in obj.Select(e => underlying.Serialize(e)))
                {
                    if (o.Length != underlyingSize)
                    {
                        ThrowHelpers.ThrowArgumentOutOfRangeException(nameof(obj));
                    }

                    ms.Write(o, 0, underlyingSize);
                }
            }
            else
            {
                foreach (var o in obj.Select(e => underlying.Serialize(e)))
                {
                    ms.WritePackedLength(o.Length);
                    if (o.Length > 0)
                    {
                        ms.Write(o, 0, o.Length);
                    }
                }
            }

            return(ms.ToArray());
        }
Beispiel #9
0
        public static int WritePackedLength(this Stream stream, long len)
        {
            if (len < 0 || (ulong)len >= 0xC000000000000000UL)
            {
                ThrowHelpers.ThrowArgumentOutOfRangeException(nameof(len));
            }

            Span <byte> lenBuf = stackalloc byte[sizeof(long)];

            lenBuf = lenBuf.Slice(0, WritePackedLength(lenBuf, len));
#if NETSTANDARD2_1
            stream.Write(lenBuf);
#else
            foreach (var b in lenBuf)
            {
                stream.WriteByte(b);
            }
#endif
            return(lenBuf.Length);
        }
        public byte[] Serialize(byte[] obj)
        {
            if (obj == null)
            {
                ThrowHelpers.ThrowArgumentNullException(nameof(obj));
            }

            // ReSharper disable once PossibleNullReferenceException
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            if (obj.Length == 0)
            {
#pragma warning restore CS8602 // Dereference of a possibly null reference.
                return(new byte[sizeof(ushort)]);
            }

            using var ms = new MemoryStream();
            ms.WritePackedLength(obj.Length);
            ms.Write(obj, 0, obj.Length);
            return(ms.ToArray());
        }
        public byte[] Serialize(string obj)
        {
            if (obj == null)
            {
                ThrowHelpers.ThrowArgumentNullException("obj");
            }

            if (string.IsNullOrEmpty(obj))
            {
                return(new byte[1]);
            }

            byte[]      bytes   = encoding.GetBytes(obj);
            Span <byte> packed2 = stackalloc byte[8];

            packed2 = packed2.Slice(0, packed2.WritePackedLength(bytes.Length));
            byte[] rv = new byte[bytes.Length + packed2.Length];
            packed2.CopyTo(rv);
            bytes.AsSpan().CopyTo(rv.AsSpan(packed2.Length));
            return(rv);
        }