static int FillFromStream(Stream input, ref byte[] buffer)
        {
            int length = 0;
            int read;

            while ((read = input.Read(buffer, length, buffer.Length - length)) > 0)
            {
                length += read;
                if (length == buffer.Length)
                {
                    ByteArrayUtil.FastResize(ref buffer, length * 2);
                }
            }

            return(length);
        }
        public static async System.Threading.Tasks.Task <T> DeserializeAsync <T>(Stream stream, IJsonFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = JsonSerializer.DefaultResolver;
            }

            var buffer = BufferPool.Default.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)
                    {
                        ByteArrayUtil.FastResize(ref buf, length * 2);
                    }
                }

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

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