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
    {
        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__2))]
        public static Task<int> ReadInternal(this Stream stream, byte[] buffer, int offset, int count, bool async, CancellationToken cancellationToken)
        {
            <ReadInternal>d__2 stateMachine = default(<ReadInternal>d__2);
            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__3))]
        public static Task WriteInternal(this Stream stream, byte[] buffer, int offset, int count, bool async, CancellationToken cancellationToken)
        {
            <WriteInternal>d__3 stateMachine = default(<WriteInternal>d__3);
            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__5))]
        public static Task CopyToInternal(this Stream src, Stream dest, int bufferSize, bool async, CancellationToken cancellationToken)
        {
            <CopyToInternal>d__5 stateMachine = default(<CopyToInternal>d__5);
            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;
        }
    }
}