예제 #1
0
        /// <summary>
        /// Takes all of the data in the stream and returns it as an array of bytes
        /// </summary>
        /// <param name="input">Input stream</param>
        /// <returns>A byte array</returns>
        public static async Task <byte[]> ReadAllBinaryAsync(this Stream input)
        {
            if (input is null)
            {
                return(Array.Empty <byte>());
            }

            if (input is MemoryStream TempInput)
            {
                return(TempInput.ToArray());
            }

            var Pool   = ArrayPool <byte> .Shared;
            var Buffer = Pool.Rent(4096);

            using var Temp = new MemoryStream();
            while (true)
            {
                var Count = await input.ReadAsync(Buffer.AsMemory(0, Buffer.Length)).ConfigureAwait(false);

                if (Count <= 0)
                {
                    Pool.Return(Buffer);
                    return(Temp.ToArray());
                }
                Temp.Write(Buffer, 0, Count);
            }
        }
예제 #2
0
        /// <summary>
        /// Takes all of the data in the stream and returns it as an array of bytes
        /// </summary>
        /// <param name="input">Input stream</param>
        /// <returns>A byte array</returns>
        public static byte[] ReadAllBinary(this Stream input)
        {
            if (input is null)
            {
                return(Array.Empty <byte>());
            }

            if (input is MemoryStream TempInput)
            {
                return(TempInput.ToArray());
            }

            var Pool   = ArrayPool <byte> .Shared;
            var Buffer = Pool.Rent(4096);

            using var Temp = new MemoryStream();
            while (true)
            {
                var Count = input.Read(Buffer, 0, Buffer.Length);
                if (Count <= 0)
                {
                    Pool.Return(Buffer);
                    return(Temp.ToArray());
                }
                Temp.Write(Buffer, 0, Count);
            }
        }