Beispiel #1
0
        private static object Deserialize(MemoryStream ms, bool fromPool, Type type,
            DeserializeStringSpanDelegate deserializer)
        {
            var bytes = ms.GetBufferAsBytes();
            var utf8 = CharPool.GetBuffer(Encoding.UTF8.GetCharCount(bytes, 0, (int) ms.Length));
            try
            {
                var charsWritten = Encoding.UTF8.GetChars(bytes, 0, (int) ms.Length, utf8, 0);
                var ret = deserializer(type, new ReadOnlySpan<char>(utf8, 0, charsWritten));
                return ret;
            }
            finally
            {
                CharPool.ReleaseBufferToPool(ref utf8);

                if (fromPool)
                    ms.Dispose();
            }
        }
        private static object Deserialize(MemoryStream memoryStream, bool fromPool, Type type, DeserializeStringSpanDelegate deserializer)
        {
            var bytes = memoryStream.GetBufferAsSpan().WithoutBom();
            var chars = CharPool.GetBuffer(Encoding.UTF8.GetCharCount(bytes));

            try
            {
                var charsWritten = Encoding.UTF8.GetChars(bytes, chars);
                ReadOnlySpan <char> charsSpan = chars;
                var ret = deserializer(type, charsSpan.Slice(0, charsWritten));
                return(ret);
            }
            finally
            {
                CharPool.ReleaseBufferToPool(ref chars);

                if (fromPool)
                {
                    memoryStream.Dispose();
                }
            }
        }