StreamExtensions
Extension methods for working with Streams.
            
                using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Azure.Storage
{
    internal static class StreamExtensions
    {
        private const int DefaultCopyBufferSize = 81920;
        public static Stream WithNoDispose(this Stream stream)
        {
            if (!(stream is NonDisposingStream))
                return new NonDisposingStream(stream);
            return stream;
        }
        public static long? GetLengthOrDefault(this Stream content)
        {
            try {
                if (content.CanSeek)
                    return content.Length - content.Position;
            } catch (NotSupportedException) {
            }
            return null;
        }
        [AsyncStateMachine(typeof(<ReadInternal>d__3))]
        public static Task<int> ReadInternal(this Stream stream, byte[] buffer, int offset, int count, bool async, CancellationToken cancellationToken)
        {
            <ReadInternal>d__3 stateMachine = default(<ReadInternal>d__3);
            stateMachine.<>t__builder = AsyncTaskMethodBuilder<int>.Create();
            stateMachine.stream = stream;
            stateMachine.buffer = buffer;
            stateMachine.offset = offset;
            stateMachine.count = count;
            stateMachine.async = async;
            stateMachine.cancellationToken = cancellationToken;
            stateMachine.<>1__state = -1;
            stateMachine.<>t__builder.Start(ref stateMachine);
            return stateMachine.<>t__builder.Task;
        }
        [AsyncStateMachine(typeof(<WriteInternal>d__4))]
        public static Task WriteInternal(this Stream stream, byte[] buffer, int offset, int count, bool async, CancellationToken cancellationToken)
        {
            <WriteInternal>d__4 stateMachine = default(<WriteInternal>d__4);
            stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
            stateMachine.stream = stream;
            stateMachine.buffer = buffer;
            stateMachine.offset = offset;
            stateMachine.count = count;
            stateMachine.async = async;
            stateMachine.cancellationToken = cancellationToken;
            stateMachine.<>1__state = -1;
            stateMachine.<>t__builder.Start(ref stateMachine);
            return stateMachine.<>t__builder.Task;
        }
        public static Task CopyToInternal(this Stream src, Stream dest, bool async, CancellationToken cancellationToken)
        {
            return src.CopyToInternal(dest, 81920, async, cancellationToken);
        }
        [AsyncStateMachine(typeof(<CopyToInternal>d__6))]
        public static Task CopyToInternal(this Stream src, Stream dest, int bufferSize, bool async, CancellationToken cancellationToken)
        {
            <CopyToInternal>d__6 stateMachine = default(<CopyToInternal>d__6);
            stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
            stateMachine.src = src;
            stateMachine.dest = dest;
            stateMachine.bufferSize = bufferSize;
            stateMachine.async = async;
            stateMachine.cancellationToken = cancellationToken;
            stateMachine.<>1__state = -1;
            stateMachine.<>t__builder.Start(ref stateMachine);
            return stateMachine.<>t__builder.Task;
        }
        [AsyncStateMachine(typeof(<CopyToExactInternal>d__7))]
        public static Task<long> CopyToExactInternal(this Stream src, Stream dst, long count, bool async, CancellationToken cancellationToken)
        {
            <CopyToExactInternal>d__7 stateMachine = default(<CopyToExactInternal>d__7);
            stateMachine.<>t__builder = AsyncTaskMethodBuilder<long>.Create();
            stateMachine.src = src;
            stateMachine.dst = dst;
            stateMachine.count = count;
            stateMachine.async = async;
            stateMachine.cancellationToken = cancellationToken;
            stateMachine.<>1__state = -1;
            stateMachine.<>t__builder.Start(ref stateMachine);
            return stateMachine.<>t__builder.Task;
        }
        [AsyncStateMachine(typeof(<CopyToExactInternal>d__8))]
        public static Task<long> CopyToExactInternal(this Stream src, Stream dst, long count, int copyBufferSize, bool async, CancellationToken cancellationToken)
        {
            <CopyToExactInternal>d__8 stateMachine = default(<CopyToExactInternal>d__8);
            stateMachine.<>t__builder = AsyncTaskMethodBuilder<long>.Create();
            stateMachine.src = src;
            stateMachine.dst = dst;
            stateMachine.count = count;
            stateMachine.copyBufferSize = copyBufferSize;
            stateMachine.async = async;
            stateMachine.cancellationToken = cancellationToken;
            stateMachine.<>1__state = -1;
            stateMachine.<>t__builder.Start(ref stateMachine);
            return stateMachine.<>t__builder.Task;
        }
    }
}