﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OfficeDevPnP.Core.Extensions
{
    public static class StreamExtensions
    {
        public static MemoryStream ToMemoryStream(this Stream source)
        {
            var stream = source as MemoryStream;
            if (stream != null) return stream;
            MemoryStream target = new MemoryStream();
            const int bufSize = 65535;
            byte[] buf = new byte[bufSize];
            int bytesRead = -1;
            while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
                target.Write(buf, 0, bytesRead);
            return target;
        }
    }
}

﻿using System.IO;

namespace OfficeDevPnP.Core.Extensions
{
    public static class StreamExtensions
    {
        public static MemoryStream ToMemoryStream(this Stream source)
        {
            var stream = source as MemoryStream;
            if (stream != null) return stream;
            var target = new MemoryStream();
            const int bufSize = 65535;
            var buf = new byte[bufSize];
            int bytesRead;
            while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
                target.Write(buf, 0, bytesRead);
            target.Position = 0;
            return target;
        }
    }
}
Set position in MemoryStream to 0, to ensure it can be read from later.
Set position in MemoryStream to 0, to ensure it can be read from later.
