public byte[] ToUtf8ByteArray()
 {
     if (buffer == null)
     {
         return(emptyBytes);
     }
     return(BinaryUtil.FastCloneWithResize(buffer, offset));
 }
Beispiel #2
0
        public static T Deserialize <T>(Stream stream, IJsonFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = DefaultResolver;
            }

#if NETSTANDARD && !NET45
            var ms = stream as MemoryStream;
            if (ms != null)
            {
                if (ms.TryGetBuffer(out var buf2))
                {
                    // when token is number, can not use from pool(can not find end line).
                    var token = new JsonReader(buf2.Array, buf2.Offset).GetCurrentJsonToken();
                    if (token == JsonToken.Number)
                    {
                        var buf3 = new byte[buf2.Count];
                        Buffer.BlockCopy(buf2.Array, buf2.Offset, buf3, 0, buf3.Length);
                        return(Deserialize <T>(buf3, 0, resolver));
                    }

                    return(Deserialize <T>(buf2.Array, buf2.Offset, resolver));
                }
            }
#endif
            {
                var buf     = MemoryPool.Rent();
                var poolBuf = buf;
                try
                {
                    var len = FillFromStream(stream, ref buf);

                    // when token is number, can not use from pool(can not find end line).
                    var token = new JsonReader(buf).GetCurrentJsonToken();
                    if (token == JsonToken.Number)
                    {
                        buf = BinaryUtil.FastCloneWithResize(buf, len);
                    }

                    return(Deserialize <T>(buf, resolver));
                }
                finally
                {
                    MemoryPool.Return(poolBuf);
                }
            }
        }
Beispiel #3
0
        public static async System.Threading.Tasks.Task <T> DeserializeAsync <T>(Stream stream, IJsonFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = DefaultResolver;
            }

            var buffer = MemoryPool.Rent();
            var buf    = buffer;

            try
            {
                int length = 0;
                int read;
                while ((read = await stream.ReadAsync(buf, length, buf.Length - length).ConfigureAwait(false)) > 0)
                {
                    length += read;
                    if (length == buf.Length)
                    {
                        BinaryUtil.FastResize(ref buf, length * 2);
                    }
                }

                // when token is number, can not use from pool(can not find end line).
                var token = new JsonReader(buf).GetCurrentJsonToken();
                if (token == JsonToken.Number)
                {
                    buf = BinaryUtil.FastCloneWithResize(buf, length);
                }

                return(Deserialize <T>(buf, resolver));
            }
            finally
            {
                MemoryPool.Return(buffer);
            }
        }